wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what is the difference between these two? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what is the difference between these two?" Watch "what is the difference between these two?" New topic
Author

what is the difference between these two?

Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

this is code from Mana's topic
I wanted to know why following happens , According to me both are similar statements

this code compiles just fine
but

this code does not do it
it gives error that System.out.println is unreachable statement
why does the compiler does not give the same in the first case?
and why does first case compiles fine?

SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2013
If you find any post useful, click the "plus one" sign on the right
Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308

maybe because int i (in first example) is NOT a compile time constant according to compiler
so " i>= 0 " could be true or false according to value of i

see the below code!
// this gives a compile error " unreachable code"


This is pretty much same as


SCJP6, SCWCD5, OCP-JBCD5, OCE-JWSD6 OCE-JPAD6 , OCM-JEA5 1,OCM-JEA5 2,3 - Brainbench certifications: J2EE, Java2, Java2-NonGUI, JSP, SQL2000 Admin, SQL2000 Programming , Brainbench certified Java Programmer, Computer Programmer, Web Developer, Database Administrator
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

but why does the compiler complain even if we are providing true in the for block in second case?
Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308

Prasad Kharkar wrote:but why does the compiler complain even if we are providing true in the for block in second case?
because it is guaranteed to throw a RuntimeException. so it complains whatever comes next line is unreachable code!
Ravishanker kumar
Ranch Hand

Joined: Jul 20, 2006
Posts: 53
In second case
for( int i = 0; true; i++) throw new RuntimeException();
System.out.println("someText");

you are providing a condition as 'true' which is keyword and compiler can recognize that it is always true. So its infinite loop and statement after that is unreachable.

in first case,
for( int i = 0; i>=0; i++) throw new RuntimeException();
System.out.println("someText");


condition expression will be evaluate at runtime . Compiler can not evaluate runtime expression. that's why no compilation error.
Please correct me if I am wrong.
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

means..
from above two cases
1. In the first case, the compiler complains because we are NOT providing a constant in the for loop
2. In the case where we write final int i = 0, the compiler is complaining because variable cannot be modified, not because of unreachable code, why is it? I mean this contradicts your answer
Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308

Prasad Kharkar wrote:means..
from above two cases
1. In the first case, the compiler complains because we are NOT providing a constant in the for loop
2. In the case where we write final int i = 0, the compiler is complaining because variable cannot be modified, not because of unreachable code, why is it? I mean this contradicts your answer


first case compiler DOESNT complain anything . this is your FIRST example



second case compiler is complaining "unreachable code" because i made "final int i" ; so RuntimeException() is for sure




third case: with respect to final var, i am avoiding i++; compiler is complaining "unreachable code"
becuase of "final int i" RuntimeException() is for sure



fourth case; which is your Example 2; becuase of for(;true;) compiler is complaining "unreachable code"
RuntimeException() is for sure



hope this is it clear enough!
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

yeah
got it quite right now
thanks everyone

have a nice time to you all
Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308

Prasad Kharkar wrote:yeah
got it quite right now
thanks everyone

have a nice time to you all

you are welcome friend!
enjoy the day & weekend!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is the difference between these two?
 
Similar Threads
help for loop variation
Why it does not cimpile without finally ?
The strange ways of compile time constants
SCJP 6, 15.04.2010 12:30 CET
checked exceptions!