Page 1 of 1

About Question enthuware.ocajp.i.v8.2.944 :

Posted: Wed Apr 08, 2015 3:05 am
by alkour
Hi,

I think, there is some small typo in the explanation to A answer.

For example, the following is valid: int b = 0, c = 0;
int a = b = c = 100;

Even the following is valid:
int b , c; // Not initializing b and c here.
int a = b = c = 100; // declaring a and initializing c, b, and a at the same time.
Meanwhile
int a = b = c = 100;
is not valid.

Re: About Question enthuware.ocajp.i.v8.2.944 :

Posted: Wed Apr 08, 2015 3:37 am
by admin
It is talking about 2 lines of code in each example. Not about individual lines. That is why it says, "The following is valid" and not "The following are valid".

The line int a = b = c = 100; on its own is not valid. But both the lines together are valid. That is what the comments are trying to explain -
int b , c; // Not initializing b and c here.
int a = b = c = 100; // declaring a and initializing c, b, and a at the same time.

The second line is valid if the first line exists.

HTH,
Paul

Re: About Question enthuware.ocajp.i.v8.2.944 :

Posted: Wed Apr 08, 2015 4:24 am
by alkour
Thanks a lot.
I have got the point.

Re: About Question enthuware.ocajp.i.v8.2.944 :

Posted: Tue Feb 07, 2017 5:19 am
by Deleted User 3513
Java does not allow chained initialization in declaration so option 1 and 5 are not valid.
Is this applicable anywhere in the program? I tried doing int a=b=c=100; in methods, classes, initializers, etc. and it returns an error something like it cannot find symbol. Is there an exception to this rule?

Re: About Question enthuware.ocajp.i.v8.2.944 :

Posted: Tue Feb 07, 2017 9:11 pm
by admin
This rule is about chained initialization combined with declaration. i.e. where you declare as well as initialize at the same time. For example, int a = b = c =10; Here you are trying to declare a, b, c and initialize also.
But this is valid:
int a = 0, b=0, c = 0; //This is valid because it is not chained.
a = b = c = 10; //This is valid because although it is chained but there is no declaration, only initialization.

Re: About Question enthuware.ocajp.i.v8.2.944 :

Posted: Thu Dec 07, 2017 4:48 am
by yassine
in previous questions it is said that "java evaluate operation from left to right" .taking that into account .I thought that :for this operation :
int b = 0, c = 0;
int a = b = c = 100;
System.out.println(a+" "+b+" "+c)
will be executed as :
a=b, then b=c, finally c=100;
which will print :
0 0 100
why is that is not true ?

Re: About Question enthuware.ocajp.i.v8.2.944 :

Posted: Thu Dec 07, 2017 8:03 am
by admin
You need to keep 2 things in mind while evaluating expressions - evaluation order and operator precedence. When you evaluate from left to right, the first operand of the first = is a and the second operand is b = c = 100;
Now, = has least precedence and that is why, the second operand i.e. b = c = 100; will be evaluated before executing the assignment operator.

The same logic applies to b = c = 100;

Re: About Question enthuware.ocajp.i.v8.2.944 :

Posted: Thu Dec 07, 2017 8:53 am
by yassine
thank you for responding, that's make sense

Re: About Question enthuware.ocajp.i.v8.2.944 :

Posted: Fri Dec 08, 2017 12:00 am
by admin
Sorry, I misspoke above. The reason why a = b = c = 100; is evaluated as a = ( b = (c = 100) ); is because of "associativity" and not evaluation order. JLS section 15.26 clearly says that the = operator is right associative right-associative (they group right-to-left). Thus, a=b=c means a=(b=c) , which assigns the value of c to b and then assigns the value of b to a .

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v8.2.944 :

Posted: Sat Jun 23, 2018 4:45 pm
by flex567
>When you evaluate from left to right, the first operand of the first = is a and the second operand is b = c = 100;
Why do we evaluate from left to right?

Re: About Question enthuware.ocajp.i.v8.2.944 :

Posted: Sat Jun 23, 2018 9:21 pm
by admin
flex567 wrote:
Sat Jun 23, 2018 4:45 pm
>When you evaluate from left to right, the first operand of the first = is a and the second operand is b = c = 100;
Why do we evaluate from left to right?
Rule of Java language. Which book are you following? It should have explained this in the Operators and Evaluation chapter.

Re: About Question enthuware.ocajp.i.v8.2.944 :

Posted: Sun Jun 24, 2018 5:18 am
by flex567
I mainly use OCA Oracle Certified Associate Java SE 8 [2014]

From the book(p.52):
>Unless overridden with parentheses, Java operators follow order of operation, listed in Table 2.1, by decreasing order of operator precedence. If two operators have the same level of precedence, then Java guarantees left-to-right evaluation.

In our case the statement contains operators with the same level of precedence because of that we have left-to-right evaluation?

Re: About Question enthuware.ocajp.i.v8.2.944 :

Posted: Sun Jun 24, 2018 5:42 am
by admin
Order of evaluation is always left to right. There are three thing that determine how an expression is evaluated - Precedence, Order of evaluation, and associativity. All three are different. It is not possible to explain them in a post here but google them to know more.

Re: About Question enthuware.ocajp.i.v8.2.944 :

Posted: Fri Jul 26, 2019 1:35 am
by SL____
I'm sorry, but regarding first one i don't get it.
"Which of the following are valid code snippets appearing in a method:"
I suppose
1) each option is individual
2) since it is a code snippets inside a method, around this code and around this method could be any code. I would undestand it has to be out of String literal, but why should i think no other a and b ints declaration must be?

Thats how i read the question:
"Which lines of code inserted individually in a method could be valid?"

What did i miss?

Re: About Question enthuware.ocajp.i.v8.2.944 :

Posted: Fri Jul 26, 2019 3:22 am
by admin
You interpreted correctly.
Are you able to put the code given in option 1 in a method and compile?

Re: About Question enthuware.ocajp.i.v8.2.944 :

Posted: Mon Jul 29, 2019 12:00 pm
by SL____
Yes, it was discussed earlier, given "a" and "b" declared already (grey zone context) i'm able to compile.

Re: About Question enthuware.ocajp.i.v8.2.944 :

Posted: Mon Jul 29, 2019 12:47 pm
by admin
It is a possible interpretation but from exam perspective, not a valid one. Specially when option 2, which shows the possibility that you are talking about, is present.