what will happen if you compile/run this code? public class Q10 { public static void main(String[] args) { int i = 10; int j = 10; boolean b = false; if(b = i ==j) System.out.println("true"); else System.out.println("false"); } } Answer says True... Please explain!
Hi! Sai In the statement if(b = i ==j) System.out.println("true"); else System.out.println("false");
due to precedence of operators '==' has a higher precedence than '=' and so the statement can be read as b = (i==j); i == j will be true and this gets assigned to the boolean b hence the answer is 'true'. Hope this helps. Latha
Originally posted by Sai Ram9: what will happen if you compile/run this code? public class Q10 { public static void main(String[] args) { int i = 10; int j = 10; boolean b = false; if(b = i ==j) System.out.println("true"); else System.out.println("false"); } } Answer says True... Please explain!