File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why is the code complining

 
Abdul Latif
Ranch Hand
Posts: 89
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rick Reumann
Ranch Hand
Posts: 281
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ).
 
Wasim Ahmed
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic