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

from JLS

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,
I have a got a small query,
this comes from JLS , says..

A while statement can complete normally iff at least one of the following is true:
The while statement is reachable and the condition expression is not a constant expression with value true.
There is a reachable break statement that exits the while statement.
now se this code.
final boolean b=true;
public void add()
{
while(b)
{
System.out.println("end less with a constant");
}
System.out.println(" no error");
}
actually I was expecting an error stating the print statement is unreachable.But it compiles.
Am I not providing a constant value here.
may be I am missing out on some thing,Can someone clarify what.
Thanks
Vijay Pillai
 
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
 
Vijay pillai
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello Cherry,
this is taken from JLS:
<---<br /> ACTUAL: An if-then statement can complete normally iff it is reachable. The then-statement is reachable iff the if-then statement is reachable. <br /> ACTUAL: An if-then-else statement can complete normally iff the then-statement can complete normally or the else-statement can complete normally. The then-statement is reachable iff the if-then-else statement is reachable. The else-statement is reachable iff the if-then-else statement is reachable. <br /> As an example, the following statement results in a compile-time error: <br /> while (false) { x=3; }<br /> <br /> because the statement x=3; is not reachable; but the superficially similar case: <br /> if (false) { x=3; }<br /> <br /> does not result in a compile-time error. An optimizing compiler may realize that the statement x=3; will never be executed and may choose to omit the code for that statement from the generated class file, but the statement x=3; is not regarded as "unreachable" in the technical sense specified here. <br /> The rationale for this differing treatment is to allow programmers to define "flag variables" such as:<br /> <br /> static final boolean DEBUG = false;<br /> <br /> and then write code such as: <br /> if (DEBUG) { x=3; }<br /> <br /> The idea is that it should be possible to change the value of DEBUG from false to true or from true to false and then compile the code correctly with no other changes to the program text. <br /> This ability to "conditionally -->

when we look at ur code it make sense.
in the first eg:
here as u said the compiler checks if the while has a break or not,it finds one and hence compiles,but actually it does print the value of "i" when run,cause it gets into an infinite loop.
in the 2nd eg:
things are slightly different though,here also the compiler finds a break inside and actually breaks at runtime making it come out of the loop,hence the compiler complains "i was not initialised".
here it doesnt give statement not reached due to the same reason,The compiler finds a break in loop.
in eg 3:
here the breake inside while is not reached hence the compiler does find a break inside the while and hence the s.o.p(i); is not reacheble.
but my question still remains,while does it behave differently with constants.
can anyone explain.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vijay,
When you use a constant the compiler optimizes your code.
For example,

The compiler looks at the variable b, finds it marked as final and therefore knows it's value will never change .. so it substitutes the value for b (true) in the while loop.
If b were not a final, it would substitute op-codes to look up the value of b. In this case, the value of b could be changed to false, in which case the variable x would never be initialized.
Hope that helps.

------------------
Jane Griscti
Sun Certified Java 2 Programmer
 
Vijay pillai
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Jane,
thanks for the response Jane but I still have some doubts about the same,
According to what I understand from JLS it says,
A while statement can complete normally iff at least one of the following is true:
1.The while statement is reachable and the condition expression is not a constant expression with value true.
2.There is a reachable break statement that exits the while statement.
which means if the codition statement is either (true) or a constant.the println statement following the loop wil be considered unreachable.
for eg:
public static void main(Str[])
{
final boolean b=true;
while(b)
{
System.out.println("hi");
}
System.out.println(" not unreacheable");
//but this code is a compile time error.
while(true)
{
System.out.println("hi man");
}
System.out.println("this is unreachable");
}
can u pls help me with this issue.
Vijay Pillai
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vijay,
I could not locate your statement in the JLS. Which section is it in?
JLS §14.11 says the following:


A while statement is executed by first evaluating the Expression. If evaluation of the
Expression completes abruptly for some reason, the while statement completes abruptly for
the same reason. Otherwise, execution continues by making a choice based on the resulting
value:
If the value is true, then the contained Statement is executed. Then there is a choice:
If execution of the Statement completes normally, then the entire while statement
is executed again, beginning by re-evaluating the Expression.
If execution of the Statement completes abruptly, see �14.11.1 below.
If the value of the Expression is false, no further action is taken and the while
statement completes normally.


There is no reference to constant expressions.
Ok ... found your reference in JLS § 14.20 Unreachable Statements


A while statement can complete normally iff at least one of the following is true:
The while statement is reachable and the condition expression is not a constant
expression with value true.
There is a reachable break statement that exits the while statement.
The contained statement is reachable iff the while statement is reachable and the
condition expression is not a constant expression whose value is false.


The wording is strange but I think this is what they are saying:
A while statement can complete normally if and only if it is reachable.


A statement is reachable if the statement is reachable AND the value of the expression is not a constant expression whose value is false

I think they used the wording about a constant being 'true' to point out that the statement must be reachable on it's own terms, regardless of wether or not the expression is a 'constant value of true'
This is all in terms of 'flow analysis'. If for example, the statement in the 'while' block is preceded by a 'break' then it would not be reachable, even if the expression was a constant value of 'true'.
Hope that helps.
------------------
Jane Griscti
Sun Certified Java 2 Programmer
 
Vijay pillai
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Jane,
So i assume that what they actually meant here was a constant value of true/false not a final variable with a constant value of true/false.
Did I get it right?
Vijay Pillai
 
Cherry Mathew
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
My doubts remain


ACTUAL: An if-then statement can complete normally iff it is reachable. The then-statement is reachable iff the if-then statement is reachable.
ACTUAL: An if-then-else statement can complete normally iff the then-statement can complete normally or the else-statement can complete normally. The then-statement is reachable iff the if-then-else statement is reachable. The else-statement is reachable iff the if-then-else statement is reachable.
As an example, the following statement results in a compile-time error:
while (false) { x=3; }

because the statement x=3; is not reachable; but the superficially similar case:
if (false) { x=3; }

does not result in a compile-time error. An optimizing compiler may realize that the statement x=3; will never be executed and may choose to omit the code for that statement from the generated class file, but the statement x=3; is not regarded as "unreachable" in the technical sense specified here.
The rationale for this differing treatment is to allow programmers to define "flag variables" such as:

static final boolean DEBUG = false;

and then write code such as:
if (DEBUG) { x=3; }

The idea is that it should be possible to change the value of DEBUG from false to true or from true to false and then compile the code correctly with no other changes to the program text.
This ability to "conditionally -->
[/qoute]

Ok i agree that u have to use the constant expression like above but i think it answers the doubt about why final booleans wont work.
But the same case that applies to if applies to while also why wont it apply although not as useful.

[qoute]
in the 2nd eg:
things are slightly different though,here also the compiler finds a break inside and actually breaks at runtime making it come out of the loop,hence the compiler complains "i was not initialised".
here it doesnt give statement not reached due to the same reason,The compiler finds a break in loop.
[/qoute]

here in this case my doubt was why stmt notreached is not coming in line 1 since it will be never executed

[qoute]
in eg 3:
here the breake inside while is not reached hence the compiler does find a break inside the while and hence the s.o.p(i); is not reacheble.
[/qoute]

here s.o.p(i) is reached but line marked line 1 is not reached

Someone please answer
Cherry
[This message has been edited by Cherry Mathew (edited February 09, 2001).]
[This message has been edited by Cherry Mathew (edited February 09, 2001).]

 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vijay,
It applies to either a constant value 'true' or a final variable with the value 'true'; they are treated the same.
Here's is some code I played around with to see what can happen with 'while' loops, final variables and literal boolean values 'true' and 'false'

Also, keep in mind that in determining wether or not a statement is reachable the compiler does not take the value of expressions into account except for <code>while</code>, <code>do</code>, and <code>for</code> loops.
Hope that helps.
------------------
Jane Griscti
Sun Certified Java 2 Programmer
[This message has been edited by Jane Griscti (edited February 10, 2001).]
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Cherry,

But the same case that applies to if applies to while also why wont it apply although not as useful.

JLS §14.20 states:


The idea is that there must be some possible execution path from the beginning of the constructor, method, instance initializer or static initializer that contains the statement to the statement itself. The analysis takes into account the structure of statements. Except for the special treatment of while, do, and for statements whose condition expression has the constant value true, the values of expressions are not taken into account in the flow analysis.


In determining wether or not a statement is reachable expressions are not evaluated; only the structure of the code, except in the case of <code>while, do</code> and <code>for</code> loops. The value of a final variable or literal is disregarded by the compiler when analyzing an <code>if</code> statement but is taken into account when analyzing a <code>while</code> statement.

<pre>
// example 2
int i ;
while(true){
if(true) break;
i = 10; // line 1
}
System.out.println(i);
</pre>
here in this case my doubt was why stmt not reached is not coming in line 1 since it will be never executed

As far as the compiler is concerned, the statement is reachable. It does not analyze the outcome of <code>if(true)</code>. As far as it's concerned, Line 1 is potentially reachable. If you remove the <code>if(true)</code> and just leave the <code>break</code> statment (as in your third example); the compiler will produce an 'unreachable statement' error as in this case it is sure Line 1 will never be reached; with the <code>if</code> statement it may not be reached.

but my question still remains,while does it behave differently with constants.

It behaves differently because that's the way the language is designed Why? To allow programmer's to use <code>if</code> constructs for debugging purposes.
Hope that helps.
------------------
Jane Griscti
Sun Certified Java 2 Programmer
 
Vijay pillai
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello Jane,
Thanks for the response from u,U r really trying to help and i very well appreciate that.
now about the explanation u gave in the previous one addressed to me.
I din't quite get what u were pointing at,as that code still behaves the way in which we started the discussion from,or am i getting it wrong.
pls correct me here.following is the code u posted I have made certain changes to it so that u can understand what I am pointing at.

class TestWhileWithFinalConstant
{
final static boolean expr =true;
// instance initializer block
{
//check this
while(expr )//--this if changed to true instead of expr
//will not compile.
{
int i = 0;
//a break with out a runtime condition placed here will
//make the statement followed unreachable,but then that has
//nothing to do with final constants.
System.out.println("In instance initializer");
i++;

}
System.out.println("this will not reach if the while(true), but reaches with while(expr)");
}
// method block
public void method()
{
// this loop does not produce a compile error
// even though it is an infinite loop
while( expr )//assuming expr is false ,when this is
//changed to "false"
//changed to "false"
//instead of expr will produce a compile time error .
{
System.out.println("In while loop");
}
}
public static void main(String str[])
{
new TestWhileWithFinalConstant();
}
}
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vijay,
Are you asking why expressions in while loops with the value 'true' are handled differently than while loops using the literal value 'true'?
What I'm trying to show is that they are not.

In the above code you can substitute 'true' for 'expr' and the code will still have a clean compile.
If you remove the 'break', you get the compile error: 'statement unreachable' for the System.printlin.out statement following the while loop. It is unreachable because, without the 'break', there is no way for the loop itself to end, so the code outside the loop will never be reached.
If you comment out the last line and leave the 'break' commented out


you get a compile error: initializer must be able to complete normally. This is because the 'while' creates an infinite loop; which would hang your code.
Apologies if I've misunderstood your question.
[This message has been edited by Jane Griscti (edited February 11, 2001).]
 
Cherry Mathew
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Jane, Vijay for your response
 
I'm so happy! And I wish to make this tiny ad happy too:
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!