about statement unreachable
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
When i change while (condition) to while(false) .... i get a compile time error of statement unreachable .... however even i++ is unreachable in if (false){ i++;} .. but it does not give a complite time error.
So can anyone tell me the situations in which we wouid get a compile time error of statement unreachable .
Thanks
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
<code>
public class XYZ {
static boolean condition = true;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 0 ;
if (false){
i++;
}
while (condition)
{
i++ ;
if (i>4)
break ;
}
System.out.println(i) ;
}
}
</code>
SCJP, SCWCD, SCBCD
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Wanted some one to help out on that
Thanks
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
For the details have a look at 14.21 Unreachable Statements in the Java Language SpecificationOriginally posted by sid sree:
So can anyone tell me the situations in which we wouid get a compile time error of statement unreachable.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Why is there a special treat for if() and not for looping constructs?
Thanks
Deepak
SCJP, SCWCD, SCBCD
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
As an example, the following statement results in a compile-time error:
because the statement x=3; is not reachable; but the superficially similar case:
does not result in a compile-time error. An optimizing compiler may realize that the statement x=3; will never be executed and may choose to omit the code for that statement from the generated class file, but the statement x=3; is not regarded as "unreachable" in the technical sense specified here.
The rationale for this differing treatment is to allow programmers to define "flag variables" such as:
and then write code such as:
The idea is that it should be possible to change the value of DEBUG from false to true or from true to false and then compile the code correctly with no other changes to the program text.
[ July 13, 2007: Message edited by: Sergio Tridente ]
SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
| Hug your destiny! And hug this tiny ad: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |










