• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

unreachable statement

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int x = 5;

while (false) { x=3; } //unreachable compiler gives error understood.

but in this case

it x =8;
for( int i = 0; i< 0; i++) x = 3; //no error how?? even it is unreachable
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Akshay Rawal wrote:int x = 5;

while (false) { x=3; } //unreachable compiler gives error understood.

but in this case

it x =8;
for( int i = 0; i< 0; i++) x = 3; //no error how?? even it is unreachable



Compiler only operates at compile time -- so it can only determine the value of expressions using compile time constants.

It may be obvious to you that the body of the second loop is unreachable. However, to the compiler, using the rules of the current Java Language Specification, it is not obvious. In fact, the compiler can't tell that the body of the loop is unreachable.

Henry
 
Akshay Rawal
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
according to me if() is an exception for jls
if(false)
x=5;...so are you trying to say that for(;;) is also an exception
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Akshay Rawal wrote:according to me if() is an exception for jls
if(false)
x=5;...so are you trying to say that for(;;) is also an exception




No. There is no such exception for the for-loop. The same requirement also applies for all the loops. In fact, the JLS provides an example of it with the while loop.

valid code from the Java Language Specification wrote: int n = 5;
while (n > 7) k = 2;



That code snippet will be accepted by the compiler (in terms of reachability).

Henry
 
Akshay Rawal
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Compiler only operates at compile time -- so it can only determine the value of expressions using compile time constants.


Henry

..

ok so if i do

int y=7;

while(y>8)
System.out.println(y);

not unreachable because value to y will be assigned at runtime / int y = 7 is not compile time constant

and

final int y=7; //now compile time constant

while(y>8)
System.out.println(y);

i think i got our explanation..please corect me if i am wrong

 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Akshay you got it right. For compile time constants the compiler can determine their value and test conditions (as it does compiler optimizations for constants). For other variables, the compiler doesn't check the value of the variable, that's only done by JVM at runtime...
 
reply
    Bookmark Topic Watch Topic
  • New Topic