• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is the difference between these two?

 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Rajeev Rnair
Ranch Hand
Posts: 310
1
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but why does the compiler complain even if we are providing true in the for block in second case?
 
Rajeev Rnair
Ranch Hand
Posts: 310
1
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 310
1
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah
got it quite right now
thanks everyone

have a nice time to you all
 
Rajeev Rnair
Ranch Hand
Posts: 310
1
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic