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