• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

about statement unreachable

 
Ranch Hand
Posts: 188
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



When i change while (condition) to while(false) .... i get a compile time error of statement unreachable .... however even i++ is unreachable in if (false){ i++;} .. but it does not give a complite time error.

So can anyone tell me the situations in which we wouid get a compile time error of statement unreachable .

Thanks
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well your code compiles just fine [1.5] and the result is "5".
<code>
public class XYZ {
static boolean condition = true;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 0 ;
if (false){
i++;
}
while (condition)
{
i++ ;
if (i>4)
break ;
}
System.out.println(i) ;
}
}
</code>
 
Sudarshan Sreenivasan
Ranch Hand
Posts: 188
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this code as it is compiles fine ... but when you change while(condition) to while(false) it gives statement unreachble

Wanted some one to help out on that

Thanks
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by sid sree:
So can anyone tell me the situations in which we wouid get a compile time error of statement unreachable.

For the details have a look at 14.21 Unreachable Statements in the Java Language Specification
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone put it in simple terms, the solution that is explained in the specs.
Why is there a special treat for if() and not for looping constructs?
Thanks
Deepak
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepak, the answer is in the specs:


As an example, the following statement results in a compile-time error:

because the statement x=3; is not reachable; but the superficially similar case:

does not result in a compile-time error. An optimizing compiler may realize that the statement x=3; will never be executed and may choose to omit the code for that statement from the generated class file, but the statement x=3; is not regarded as "unreachable" in the technical sense specified here.

The rationale for this differing treatment is to allow programmers to define "flag variables" such as:

and then write code such as:


The idea is that it should be possible to change the value of DEBUG from false to true or from true to false and then compile the code correctly with no other changes to the program text.


[ July 13, 2007: Message edited by: Sergio Tridente ]
 
I've never won anything before. Not even a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic