if(true) is this is a bug?
if(true){
System.out.println("I am executing");
}else{
System.out.println("Sorry I never get a chance");
}
Why java is not giving a compile time err on the 4 line
System.out.println("Sorry I never get a chance");
Unreachable code?
> Why java is not giving a compile time err on the 4
> line
> System.out.println("Sorry I never get a chance");
>
> Unreachable code?
I guess the compiler doesn't check the literal values and their consistency. Plus, it can't read your mind.
while (true) {
if (condition) {
break;
}
doStuff();
}
might not be beautiful, but valid. Doesn't really apply to ifs, though, so I see your point.
if(condition){
return;
}else{
return;
}
System.out.println("It is not allowed");
Then why i'm getting the compile err here in the 6th line?
> Why java is not giving a compile time err on the 4
> line
There's a limit to how much execution path analysis the compiler is prepared to do. I think it only complains when a certain section of code is conditionally unreachable. This for example would be if you put in a return somewhere in the code and it's no chance this statement can be passed no matter how many conditional statements you change. Now say this gives you a compiler error,
return;
// conditionally unreachable
then this won't because by changing one condition the statement can be passed
if (true)
return;
// conditionally reachable
uj_a at 2007-7-8 1:21:05 >

> Then why i'm getting the compile err here in the 6th> line?It's because the print statement is conditionally unreachable. No matter if condition is true or false, println cannot be reached.
uj_a at 2007-7-8 1:21:05 >

Eclipse does not give error on
if(true)
{
System.out.println("true");
}
else
{
System.out.println("false");
}
But the unreachable code warning is generated. Also, if you compile the file, the line of code in .class file is
System.out.println("true");
only.
> if(true){
> System.out.println("I am executing");
> }else{
> System.out.println("Sorry I never get a chance");
> }
>
>
> Why java is not giving a compile time err on the 4 line
> System.out.println("Sorry I never get a chance");
>
> Unreachable code?
Because it's a debugging idiom and the compiler 'knows' about that idiom:class Foo {
private static final boolean DEBUG= true;
...
public void someMethod() {
if (DEBUG)
// so and so ...
else
// such and so ...
}
}
However, the compiler can't really distinguish between final variables and
literals in the compilation phase where it's checking unreachable code, so
it simply doesn't complain when it analyzes your example ... A next phase
of the compiler *does* catch unreachable code following that if-else
statement though, i.e. if all control flow paths end with an unconditional
return, following code will be unreachable ...
kind regards,
Jos
> Why java is not giving a compile time err on the 4 line
> System.out.println("Sorry I never get a chance");
>
> Unreachable code?
The JLS defines the characteristics of unreachable code, and specifies that all compilers must perform the same analysis: http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#236365
The key part of this is (empasis mine):
"Except for the special treatment of while, do, and for statements whose condition expression has the constant value true, the values of expressions are not taken into account in the flow analysis."