• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

non rechable statment?

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi 'All
here is the code
class Test {
void notReachable() {
int i;
if(false)
i=6;
// System.out.println(i);


}
public static void main(String[] args) {
Test t=new Test();
t.notReachable();
}
}
In the above code false is a boolean constant always equals to false that means the following statement will be never reached ...then why the compiler is not giving any not reachable error?
thnx
rishi
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the Java language spec has a special rule allowing the "if (false)" construct and saying that it doesn't make any code unreachable. The purpose is to easily allow turning debugging booleans on and off.
 
Rishi Wright
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
then how come below code is giving an error???
class Test {
public static void main(String[] args) {
Test t=new Test();
System.out.println(t.notReachable());
}
String notReachable() {
int i;
if(false)
return "String";


}
}
thnx
rishi
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rishi,
The method is expecting return statement. Either you must provide else part or include semicolon after if statement, to make compiler happy..

class Test {
public static void main(String[] args) {
Test t=new Test();
System.out.println(t.notReachable());
}
String notReachable() {
int i;
if(false)
return "String";

/*else
return "Str";
*/

}
}
String notReachable() {
int i;
if(false)
/*
; */

return "String";
}
Thanks,
Venu Gopal.
[ August 13, 2003: Message edited by: venu gopal ]
 
Squanch that. And squanch this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic