Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compile time unreachability

 
Geethakrishna Srihari
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code

class Test
{
public static void main(String args[])
{
while(false||true)
{
}
int a=1;
}
}
o/p on compiling - unreachable statement int a=1;

But if we replace the || by |, it works fine? How does it work?
 
Erik Larson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Imagine the code is running. What will the while condition evaluate to? If you enter the loop, what happens next?
 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get the error with | too. According to JLS both | and || versions should not compile. Both (false || true) and (false|true) are compile time constant expressions with value of true.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic