About Question enthuware.ocajp.i.v7.2.1327 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

About Question enthuware.ocajp.i.v7.2.1327 :

Post 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.

Guest

Re: About Question enthuware.ocajp.i.v7.2.1327 :

Post by Guest »

Can anybody answer this?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1327 :

Post 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.
If you like our products and services, please help us by posting your review here.

gabtoth
Posts: 4
Joined: Wed Apr 23, 2014 9:09 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1327 :

Post 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 ?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1327 :

Post 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.
If you like our products and services, please help us by posting your review here.

gabtoth
Posts: 4
Joined: Wed Apr 23, 2014 9:09 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1327 :

Post by gabtoth »

Can we say, that loop is indeed unreachable, but the compiler is not smart enough figure it out ?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1327 :

Post 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.
If you like our products and services, please help us by posting your review here.

hamada.yamasaki
Posts: 17
Joined: Fri Oct 11, 2013 10:31 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1327 :

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1327 :

Post 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.
If you like our products and services, please help us by posting your review here.

hamada.yamasaki
Posts: 17
Joined: Fri Oct 11, 2013 10:31 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1327 :

Post 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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1327 :

Post 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.
If you like our products and services, please help us by posting your review here.

gparLondon
Posts: 63
Joined: Fri Oct 31, 2014 6:31 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1327 :

Post 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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1327 :

Post 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.
If you like our products and services, please help us by posting your review here.

gparLondon
Posts: 63
Joined: Fri Oct 31, 2014 6:31 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1327 :

Post by gparLondon »

I got it, Thanks very very much.

With regards,
GPAR

guestuser
Posts: 1
Joined: Wed Dec 14, 2016 2:34 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1327 :

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1327 :

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 38 guests