aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why is the code complining Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why is the code complining" Watch "why is the code complining" New topic
Author

why is the code complining

Abdul Latif
Ranch Hand

Joined: Jul 18, 2001
Posts: 89
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

Joined: Apr 03, 2001
Posts: 281
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

Joined: Jan 31, 2001
Posts: 90
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why is the code complining
 
Similar Threads
declaration ??
Local variables in switch statement
why does 'break' traverse one more time
Assertion Question
Break and continue inside the case statement