public class test { public static void main(String args[]) { //prob// int i, j=1; i = (j>1)?2:1; switch(i) { case 0: System.out.println(0); break; case 1: System.out.println(1); case 2: System.out.println(2); break; case 3: System.out.println(3); break; } } } here //prob the variable is not intilise to some value , though it is a local variable the code gets complied can u explain me why
Originally posted by Abdul Latif: public class test { public static void main(String args[]) { //prob// int i, j=1; i = (j>1)?2:1; switch(i) { case 0: System.out.println(0); break; case 1: System.out.println(1); case 2: System.out.println(2); break; case 3: System.out.println(3); break; } } } here //prob the variable is not intilise to some value , though it is a local variable the code gets complied can u explain me why
If you are asking if it gets compiled with the commented out part commented out- it doesn't on my machine. If you are wondering why it gets compiled when the comment tag is removed, it compiles because i does get initiliazed before it is ever used. The expression i = (j>1)?2:1; will set i to either 1 or 2 ( in this case it sets i to 1 ).
Abdul, This code gets compile because "(j>1)? : " have higher precedence than = sign. system goes left to right and resolve the expression first than assign the value to i.