Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

java acts strange

 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
try these codes
//example 1
int i ;
while(true){
if(false) break;
i = 10;
}
System.out.println(i);
/***** wont give error bcoz i will be initialised
in this case statement not reached is not given ok i acn give a reasonm for this that compiler just checks wwhether break is not there or not.

// example 2
int i ;
while(true){
if(true) break;
i = 10; // line 1
}
System.out.println(i);
/****** will give error not initialised bcoz line 1 not reached
but why it is not giving statement not reached error also at line 1
// example 3
int i ;
while(true){
break;
i = 10; // line 1
}
System.out.println(i);
// whereas this code gives stmt not reaced error at line 1 according to java it should replace the code of example 2 like example 3.

and all this doesnt work with final booleans why???/
please send the reason why java acts like this and also please tell me in which case the final will work
Cherry
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In example 2 . The if condition is checked only at run time. So the compiler cannot be sure if i = 10 is unreachable or not
In example 3 . You are saying while some condition exists
break and then assign a value to i . If the condition is true the break statement will surely execute so i = 10 is unreachable. If the condition is false then the whole block will not execute. In either case i = 10 is unreachable.
Rgds
Sahir http://www.geocities.com/sahirshah/
 
Cherry Mathew
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but why I have read that java repalces the code where constants are used and accroding to that only break should be present
Cherry
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Cherry,
In any Selection or Iteration if you did't define your code within a block {} only first line after the Selection or Iteration will get executed and rest all will get ignored by the compiler. This is what happened in your second example i.e.
in IF condition you have not openedd the curly brace i.e.y. compiler has taken only break and ignored the initialisation part of Variable i and i.e.y. error messages are different. If the compiler finds a block code i.e. curly brace{} it will compile it and you would have got STATEMENT NOT REACHED error.
 
Cherry Mathew
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Srinivas
i didnt get ur reason please give the code
I think braces wont make any difference
Cherry
 
V Srinivasan
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Cherry,
OK. The fact is java take cares for assigning default value only to in the case of statics, instances(if not initialised) and refence(to null). And here compiler checks and insists to initialise the int i, because it is not static,instance,refernce. But the code has't given chance to read the line 1. OK. If you enclose curly braces for if selection, you will get two errors one is STATEMENT NOT REACHED another is INT i NOT INITIALISED. Hope this will clear you.
Regards,
 
LOOK! OVER THERE! (yoink) your tiny ad is now my tiny ad.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!