• 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

While

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class While1
{
public static void main(String[] args)
{
while(false)
{
System.out.pritln("value");
}
}
}

Why this is giving complie time error
 
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because System.out does not have a "pritln" method. And possibly because some of the code (inside of the while loop) can never be reached. C doesn't care about that, but Java does.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check the spelling of

System.out.pritln("value");


it should be System.out.println("value"); even then you will get unreachable code that's because of while(false) statement.

Thanks
Veeresh
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I modify the code like this, it is get compiling. This is somewhat strange. Can anybody explain how this compiles while the preceding throws a copile error?

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


this code compile normally . Because there aren't problem in while be false only the code below not executed.
 
lowercase baba
Posts: 12871
62
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because the compiler isn't smart enough to know that this will ALWAYS be false. 'b' is a variable, so it COULD change. however, 'false' will always and forever be false.
 
Sabber bhatia
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what do you mean by system.out does not have println method
can you give me little briefing on this

Thanks
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sabber,

System.out has a println() method, but no pritln() method, so this was just a typo.

Regarding the while-loop, while(false) will _not_ cause a compiler error, even though the code inside the while-loop is de facto unreachable. Another example:

causes a compiler error, for the System.out.println() statement is unreachable.


also causes a compiler error, but a different one: myMethod() mut return a value of type int.

compiles just fine. This makes sense, eg for DEBUG boolean constants:

needs to compile, since otherwise setting a constant like DEBUG from true to false will cause compile errors, and probably a lot of them.

Another part is that an optimizer might throw de facto unreachable code out of the AST, so it does not exist in the final byte code, but it's not an error to have such statements.
 
fred rosenberger
lowercase baba
Posts: 12871
62
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guido,

are you SURE it won't cause a compiler error?


C:\slop>type While1.java

C:\slop>javac While1.java
While1.java:6: unreachable statement
{
^
1 error


[ April 17, 2008: Message edited by: Fred Rosenberger ]
 
Thirugnanam Saravanan
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Regarding the while-loop, while(false) will _not_ cause a compiler error, even though the code inside the while-loop is de facto unreachable



while(false) will always cause a compile error(unreachable statement),but if(false) statement is allowed in Java.
 
Guido Sautter
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for clarification, didn't know that subtle difference ... sorry.
 
Don't play dumb with me! But you can try this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic