class test { static boolean check; public static void main(String args[]) { int i; if(check == true) i=1; else i=2; if(i=2) i=i+2; else i = i + 4; System.out.println(i); } }
Although the program won't run because of if(i=2) but if your condition become if(i==2) instead of if(i=2) then output=4
The program does not compile because of the statement if(i=2).
The statement "i=2" evaluates to 2. The expression within the if block must evaluate to a Boolean.
public class example { int i[] = {0}; public static void main(String args[]) { int i[] = {1}; change_i(i); System.out.println(i[0]); } public static void change_i(int i[]) { i[0] = 2; i[0] *= 2; } }
public class test { public static void main(String args[]) { String s1 = "abc"; String s2 = "abc"; if(s1 == s2) System.out.println(1); else System.out.println(2); if(s1.equals(s2)) System.out.println(3); else System.out.println(4); } }