Page 1 of 1
About Question enthuware.ocajp.i.v7.2.1279 :
Posted: Tue Apr 17, 2012 12:29 pm
by ETS User
if you say that
for( int i = 0; false; i++) x = 3; is also a compile time error because x= 3 is unreachable.
,which is in fact true, why it has been marked as correct answer?
and if it is not correct, it means it is only 2 correct answers there, right?
thanks
Re: About Question enthuware.ocajp.i.v7.2.1279 :
Posted: Tue Apr 17, 2012 2:46 pm
by admin
The option that is marked as correct is:
for( int i = 0; i< 0; i++) x = 3;
The code that you quoted from explanation is not the same and is presented as another example of unreachable statement.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1279 :
Posted: Wed Sep 19, 2012 4:46 pm
by Michailangelo
But i<0 is also false. So x=3; is also an unreachable statement.
Re: About Question enthuware.ocajp.i.v7.2.1279 :
Posted: Wed Sep 19, 2012 7:09 pm
by admin
Michailangelo wrote:But i<0 is also false.
Yes, but you know that at run time and not at compile time. Compiler doesn't know the value of i.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1279 :
Posted: Sun Oct 28, 2012 8:20 pm
by Guest
for( int i = 0; i< 0; i++) x = 3;
Netbeans with 7.3 Beta has the warning, "For loops must use braces" for this statement:
for (int i = 0; i< 0; i++) x = 3;
Also, x should be defined in the question or individual answers, otherwise the user may assume that the statements won't compile as x was not defined.
-- Robert
Re: About Question enthuware.ocajp.i.v7.2.1279 :
Posted: Mon Oct 29, 2012 7:37 am
by admin
Guest wrote:for( int i = 0; i< 0; i++) x = 3;
Netbeans with 7.3 Beta has the warning, "For loops must use braces" for this statement:
This is not a test of good coding practices

As long as it is valid, it is fair game
for (int i = 0; i< 0; i++) x = 3;
Also, x should be defined in the question or individual answers, otherwise the user may assume that the statements won't compile as x was not defined.
-- Robert
Assumption that x was not defined will lead the user to the conclusion that none of the given options will compile, which is not true because there are three correct options. So the only logical assumption is that x is declared appropriately. This statement has now been added to avoid the confusion.
thank you!
Re: About Question enthuware.ocajp.i.v7.2.1279 :
Posted: Tue Dec 11, 2012 5:57 pm
by ksnortum
In the explanation it says,
but the option reads
This is confusing.
Re: About Question enthuware.ocajp.i.v7.2.1279 :
Posted: Tue Dec 11, 2012 6:58 pm
by admin
ksnortum wrote:In the explanation it says,
but the option reads
This is confusing.
Hello,
The code given in the option is valid code that is why it is a correct option. The code that you've quoted is only a part of the explanation and it is there to illustrates an invalid way to use the loop.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1279 :
Posted: Thu Jan 10, 2013 8:52 am
by Ambiorix
admin wrote:Michailangelo wrote:But i<0 is also false.
Yes, but you know that at run time and not at compile time. Compiler doesn't know the value of i.
HTH,
Paul.
I'm not clear why the compiler doesn't know the value of i when it's given explicitly in the same statement:
for( int i = 0; i< 0; i++) x = 3
Can you explain this?
Re: About Question enthuware.ocajp.i.v7.2.1279 :
Posted: Thu Jan 10, 2013 12:21 pm
by admin
This is how compilers work. The determination of the actual value of a variable happens only at runtime. That is why it is called a "variable". The compiler takes into account only those values which are constants. So if you use a final variable, then the compiler will consider its value otherwise, the compiler does not assume anything about the value and leaves it to the runtime.
Your doubt is valid though and it is possible to construct a compiler that checks this kind of situation as well. However, java language designers decided against that. The Java Language Specification describes something called "compile time constants" and that determines what the compiler considers and what it ignores. You may want to check it out.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1279 :
Posted: Sun Jan 20, 2013 1:14 am
by icepeanuts
this question is tricky, but fair enough.
Re: About Question enthuware.ocajp.i.v7.2.1279 :
Posted: Thu Mar 07, 2013 1:17 pm
by baptize
admin wrote:This is how compilers work. The determination of the actual value of a variable happens only at runtime. That is why it is called a "variable". The compiler takes into account only those values which are constants. So if you use a final variable, then the compiler will consider its value otherwise, the compiler does not assume anything about the value and leaves it to the runtime.
Your doubt is valid though and it is possible to construct a compiler that checks this kind of situation as well. However, java language designers decided against that. The Java Language Specification describes something called "compile time constants" and that determines what the compiler considers and what it ignores. You may want to check it out.
HTH,
Paul.
so many exception to rules which is hard to keep track of...
If Oracle focus on those exception to rules no one will pass the exam

Re: About Question enthuware.ocajp.i.v7.2.1279 :
Posted: Fri Sep 19, 2014 3:54 am
by thchuong
baptize wrote:
so many exception to rules which is hard to keep track of...
If Oracle focus on those exception to rules no one will pass the exam

=> very true! I hate this!!!

Re: About Question enthuware.ocajp.i.v7.2.1279 :
Posted: Tue Nov 04, 2014 4:57 am
by Kevin_C
I understand that
Code: Select all
for(int i = 0; false; i++){ // unreachable code }
fails, and
doesn't fail, because
i is calculated during runtime.
But, what would happen if
i was final, like so:
Code: Select all
for(final int i = 0; i < 0; i++){ ... }
Would
i still be calculated during runtime, or are finals always calculated during compile time and this will also result in an unreachable code error?
EDIT: Ok, it is indeed like I said above, final is calculated during runtime, so it also gives an unreachable code error. So to change my question: Are all final statements calculated during compile time? Or are there exceptions to this where a final is calculated during runtime?
Re: About Question enthuware.ocajp.i.v7.2.1279 :
Posted: Tue Nov 04, 2014 12:52 pm
by admin
Yes, final values are always considered by the compiler at compile time for determining unreachable code except in case of an if statement. The compiler specifically makes an exception for this:
final static boolean DEBUG = false;
if(DEBUG){
//ideally, this block is unreachable but the compiler allows it.
}
The reason is explained here:
http://docs.oracle.com/javase/specs/jls ... #jls-14.21 (at the end)
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1279 :
Posted: Thu Jan 08, 2015 11:25 pm
by coder007
Kevin_C wrote:
But, what would happen if
i was final, like so:
Code: Select all
for(final int i = 0; i < 0; i++){ ... }
I think this code is not valid?
"final" variable cannot be changed once assigned, but in
for statement variable
i supposed to be incremented...
Re: About Question enthuware.ocajp.i.v7.2.1279 :
Posted: Fri Jan 09, 2015 2:34 am
by admin
It certainly isn't valid:)
Re: About Question enthuware.ocajp.i.v7.2.1279 :
Posted: Sat Jan 17, 2015 10:39 pm
by coder007
To complete this subject, in enhanced FOR statement we can use
final key word:
Code: Select all
for (final String s : stringArr) ....
Easy to confuse these things (at least for me

)
Re: About Question enthuware.ocajp.i.v7.2.1279 :
Posted: Sun Jan 18, 2015 10:17 pm
by admin
Good point:)