Confusing while block
The following code block gives me a compile time error.
while(false)
{
System.out.println("HI");
}
The error says that there is an unreachable statement. BUT the following
code compiles
boolean b=false;
while(b)
{
System.out.println("Hi");
}
All i could think of was this -> In case-1 as false is a literal so the
compiler finds that its unreachable and in case 2 variable b in while
condition block is checked at runtime so there is no compilation error?
No comments:
Post a Comment