Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

if loop  RSS feed

 
Arun Prasath
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

why it doesn't show the error at the compile time?

int i;
if(false) System.out.println(i); // unreachable statement
 
Arun Prasath
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it should error that i has not been initialized.
but it doesn't..
why is it so?
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arun Prasath AV:
it should error that i has not been initialized.
but it doesn't..
why is it so?


Because that is an Unreachable statement, and compiler knows at compile time that this statement is not going to execute anyways.

So if still you haven't initialized i that not matters for Compiler unless and until you are not going to use it somewhere else.

Once you write down the statment after loop.. give their a statement like



Now you will get compile time error.
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arun Prasath AV:
it should error that i has not been initialized.
but it doesn't..
why is it so?


Continuing...


Even if you compile this class and see the generated bytecode then You will not be able to see the code of the below statements.



So this proves that compiler knows in advance that this code is not going to executed anytime.
[ September 05, 2006: Message edited by: Ankur Sharma ]
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

First remember, java will check whether the local variable is initialized or not when it is in the one of the execution path of the program. In your case it is not in execution path ( as it is in false condition of if).

Do this:

int i;

if(false)
System.out.println(i);
else
System.out.println(i);

Now you will get erorr in line after else, saying that i may not be initialized. ( See error will not be generated at int i; but at line when it is accessed. it is worth to note).
 
Sandeep Nargund
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

This is not in reply to the discussion posted above but just an observation that I've been making... a lot of people seem to use the term "if loop". This is incorrect as the if-statement is a conditional statement and certainly not a loop. So it would be appropriate to refer to it as the if-statement itself or if-blocks.

Regards,
- Sandeep.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

One is a constant expression, one isn't.
One will compile, one won't.
The rest can be easily inferred by taking a look at the JLS.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!