This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Unreachable code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Unreachable code" Watch "Unreachable code" New topic

Unreachable code

Kishan Kumar
Ranch Hand

Joined: Sep 26, 2000
Posts: 130
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.

V. Kishan Kumar

Regards,<BR>V. Kishan Kumar
Ramesh Donnipadu
Ranch Hand

Joined: Sep 16, 2000
Posts: 100
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

Joined: Sep 26, 2000
Posts: 130
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...
V. Kishan Kumar
Ying Xing

Joined: Sep 23, 2000
Posts: 24
Hi Kishan Kumar,
You can find the answer from JLS 2nd 14.20 ( )

I agree. Here's the link:
subject: Unreachable code
It's not a secret anymore!