Page 1 of 1
About Question enthuware.ocajp.i.v7.2.1327 :
Posted: Wed Oct 03, 2012 4:29 am
by ETS User
Why does this program compile when it's a rule that unreachable statements inside a program causes a compiler error. The line System.out.println() inside the for loop is unreachable.
Re: About Question enthuware.ocajp.i.v7.2.1327 :
Posted: Fri Oct 05, 2012 12:47 am
by Guest
Can anybody answer this?
Re: About Question enthuware.ocajp.i.v7.2.1327 :
Posted: Fri Oct 05, 2012 5:57 am
by admin
ETS User wrote:Why does this program compile when it's a rule that unreachable statements inside a program causes a compiler error. The line System.out.println() inside the for loop is unreachable.
It is true that unreachable statements cause compilation error. But only if the compiler can determine that a particular statement is unreachable at compile time. In this case, the compiler doesn't know about the values that the variables will have at run time so it doesn't know that the statement is unreachable.
Compiler can about the values for sure only when it sees final variables or literals because they don't change at runtime.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1327 :
Posted: Thu May 15, 2014 2:31 pm
by gabtoth
Can You provide an example when the loop is executed? There must be such case, if the compiler does not say that code is unreachable. Or ?
Re: About Question enthuware.ocajp.i.v7.2.1327 :
Posted: Thu May 15, 2014 7:34 pm
by admin
I don't understand your question. In the given code, the loop will not execute. We know that because we know the values of the variables. Compiler doesn't take the values of the variables into account when determining whether a part of code is reachable or not. In this case, compiler thinks it is (even though we know that it is not.).
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1327 :
Posted: Sat May 17, 2014 4:02 pm
by gabtoth
Can we say, that loop is indeed unreachable, but the compiler is not smart enough figure it out ?
Re: About Question enthuware.ocajp.i.v7.2.1327 :
Posted: Sat May 17, 2014 8:22 pm
by admin
I would not say the compiler is not "smart enough". As I wrote before, compiler does not take values (unless they are compile time constants) into consideration to determine if a part of code will be executed ever or not. It is how the Java language is designed. So the compiler works as required. It is not dumb.
Re: About Question enthuware.ocajp.i.v7.2.1327 :
Posted: Thu Aug 07, 2014 5:43 am
by hamada.yamasaki
The rule for final doesn't apply to Object
Note: Just for sake of completion Person is a static class like
static class Person {
public static final String s = "hello";
public static final int i = 0;
public static final boolean b = true;
}
X is not initialized
e.g 1
int x;
final Person var = new Person();
if (var != null) {
x = 1;
}
System.out.println(x);
----------------------------------------------------------
e.g 2
if (Person.s.equals("hello")) {
x = 1;
}
System.out.println(x);
-----------------------------------------------------------------
Will compile fine
e.g. 1
if (Person.b) {
x = 1;
}
------------------------------------------------------------------
e.g. 2
if (Person.i >= 0) {
x = 1;
}
----------------------------------------------------------------
Could you plz explain why e.g.1 and e.g.2 are treated differently although the conditions are true?
Re: About Question enthuware.ocajp.i.v7.2.1327 :
Posted: Thu Aug 07, 2014 6:41 am
by admin
Person.s.equals("hello") will not work because its value is not known at compile time (equals is a method that is executed at run time, not compile time). So the compiler doesn't know that x will really be initialized.
Values of Person.b and Person.i>=1 are known at compile time so the compiler knows that x will be initialized.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1327 :
Posted: Thu Aug 07, 2014 7:53 am
by hamada.yamasaki
Thanks, could you plz also explain
why e.g1 i.e
final Person var = new Person();
if (var != null) {
x = 1;
}
System.out.println(x);
doesn't work? var is initialized
Re: About Question enthuware.ocajp.i.v7.2.1327 :
Posted: Thu Aug 07, 2014 9:08 am
by admin
Let me first mention here that what you are asking is beyond the requirements for the OCAJP exam.
Now, the answer is that the if condition must be a compile time constant expression. A compile time constant expression is defined by the Java Language Specification here:
http://docs.oracle.com/javase/specs/jls ... #jls-15.28
It clearly says:
A compile-time constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following:
Thus, a comparison with any other object except String will not work.
That is why it doesn't compile.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1327 :
Posted: Tue Jan 27, 2015 12:21 pm
by gparLondon
public class TestClass{
public static void main(String args[]){
int i;
int j;
for (i = 1, j = 0; j < i; ++j, i++){
System.out.println(i + " " + j);
}
System.out.println(i + " " + j);
}
}
I expected the Out put 1 0 and 2 1 , as for the fist time i=1,j=0, 0<1 is "true"
it will print 1 and 0 then, j=1, i=1 as it is post incremented i=1 is used then in will get incremented, so 1<1 which turns "false", now the loop gets terminated and i which is 2 and j which is 1 gets printed, to my surprise this is going for infinite loop why?
Thanks,
GPAR
Re: About Question enthuware.ocajp.i.v7.2.1327 :
Posted: Tue Jan 27, 2015 9:27 pm
by admin
pre or postfix increment operation doesn't make a difference here because it is an independent statement. You are not assigning or comparing the value of i in the same statement in which you are incrementing it. (The three sections of the for loop - initialization, condition, and increment - are independent of each other.)
int k = ++i; //this is one statement so pre/post makes a difference.
i++; //or ++i;
k = i; //This is a different statement. So pre/post are same.
Re: About Question enthuware.ocajp.i.v7.2.1327 :
Posted: Wed Jan 28, 2015 1:58 am
by gparLondon
I got it, Thanks very very much.
With regards,
GPAR
Re: About Question enthuware.ocajp.i.v7.2.1327 :
Posted: Wed Dec 14, 2016 2:37 pm
by guestuser
Quick question about variable initialization in this question:
Wouldn't the variables i and j be local variables? I thought local variables had to be initialized since they do not have a default value?
Re: About Question enthuware.ocajp.i.v7.2.1327 :
Posted: Wed Dec 14, 2016 11:17 pm
by admin
guestuser wrote:Quick question about variable initialization in this question:
Wouldn't the variables i and j be local variables? I thought local variables had to be initialized since they do not have a default value?
The rule is that local variables must have a value before they are used. So yes, local variables must be initialized. But only if you are going to use them. And you can also initialize them later (i.e. not necessarily right where you declared them), just before you use them, as in this case. The for loop is initializing them just before they are being used.
HTH,
Paul.