• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unreachable code

 
Kishan Kumar
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Folks,
I am having a question on Unreachable code.
if (2<1)<br /> //<br /> else<br /> //<br /> Here the if part can never be true and the // following if can<br /> never be reached, or it is a unreachable code. why does it not show a compilation error. Also if a final variable id put in place of constant say<br /> final int i = 2;<br /> if(i > 3)
//
This // is also unreachable right? why it does not give a error.

------------------
Regards,
V. Kishan Kumar
 
Ramesh Donnipadu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kishan,
I think java compiler does not perform rigorous analysis of code. Try this.
if (true) {
System.out.println("Always true");
}
else {
System.out.println("Sometimes false");
}
 
Kishan Kumar
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramesh,
Even that is not giving a error, but logically speaking any code
that is unreahable should not be accepted. In case of exceptions
it promptly gives the unreachable code error. ie, if the superclass exception is caught ahead of subclass exception it gives this unreachable code error.
Likewise if(2<1) is known at compiletime itself and this sentence
does not have a meaning at all, but in Java's point of view why is it legal. Is it a bug that deserves enough for Sun people to
know?. or is there any concrete reason for this to happen.
Final variables I can understand that at compile time the
value may not be known as their values are initialised in constructors and static initializers.
Any answers...
------------------
Regards,
V. Kishan Kumar
 
Ying Xing
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kishan Kumar,
You can find the answer from JLS 2nd 14.20 ( http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#236365 )

Regards,
yingxing
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic