About Question enthuware.ocajp.i.v7.2.971 :
Moderator: admin
-
- Posts: 1
- Joined: Wed Sep 04, 2013 10:05 am
- Contact:
About Question enthuware.ocajp.i.v7.2.971 :
public class TestClass{
public static void main(String args[] ){
int i = 0 ;
int[] iA = {10, 20} ;
iA = i = 30 ;
System.out.println(""+ iA[ 0 ] + " " + iA[ 1 ] + " "+i) ;
}
}
Can anyone explain why iA = i = 30 doesn't get processed as follows?
iA[0] = i = 30 ----> iA[0] = 0 = 30
If the left side operand gets evaluated first, the iA[0] should be assigned i which was initially set 0 before i = 30 gets evaluated, correct?
public static void main(String args[] ){
int i = 0 ;
int[] iA = {10, 20} ;
iA = i = 30 ;
System.out.println(""+ iA[ 0 ] + " " + iA[ 1 ] + " "+i) ;
}
}
Can anyone explain why iA = i = 30 doesn't get processed as follows?
iA[0] = i = 30 ----> iA[0] = 0 = 30
If the left side operand gets evaluated first, the iA[0] should be assigned i which was initially set 0 before i = 30 gets evaluated, correct?
-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.971 :
This is explained in detail here http://docs.oracle.com/javase/specs/jls ... l#jls-15.7
-
- Posts: 3
- Joined: Sat Jun 20, 2015 12:31 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.971 :
On the explanation:
are there any other examples that this rule apply? I tried using
but it results to i: 1 and z:0. It seems that the evaluation on that if example is still from left to right.
aside from using mathematical operation like:Evaluation Respects Parentheses and Precedence
Code: Select all
int i = 3;
int j = 3 * (9 + 3);
System.out.println(j); //results to 36
Code: Select all
int i = 0;
int z = 0;
if(i++ < 5 || (++z <0 && 5 > z++) || 6 < ++i){
System.out.println("Routed here");
}
System.out.println("i: " + i);
System.out.println("z: " + z);
-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.971 :
Well, what do you think 5 + 9/3; would print if the rules of precedence were not followed?
-
- Posts: 62
- Joined: Fri Aug 07, 2015 2:16 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.971 :
could you point me to the right spot? After reading for a while, I was not able to find the explanation.This is explained in detail here http://docs.oracle.com/javase/specs/jls ... l#jls-15.7
Thanks

-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.971 :
Well, the explanation is not about this question but about evaluation order. You need to read the whole chapter on evaluation order to understand the rules of evaluation in Java.
In short, in case of iA[0] = i = 30, to assign a value to iA[0], you have to evaluate the right hand side first, which is i = 30. So 30 will be assigned to i first.
HTH,
Paul.
In short, in case of iA[0] = i = 30, to assign a value to iA[0], you have to evaluate the right hand side first, which is i = 30. So 30 will be assigned to i first.
HTH,
Paul.
-
- Posts: 62
- Joined: Fri Aug 07, 2015 2:16 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.971 :
thanks, that was enough explanation for me 

-
- Posts: 6
- Joined: Sat Jun 18, 2016 2:11 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.971 :
JSL is not concise in this case and requires jumping across sections to consolidate confusing details.
Trivially: array access operator [] takes precedence in expression (it is even higher than ()).
Complication: array access operator [] has two parts: (1) left-hand outer part to reference array, (2) inner part to reference index within array. And the 1st part is evaluated first. That's it - see Example 15.10.4-1. Array Reference Is Evaluated First.
It could have been much easier explained if array access operator [] was included in official Operator Precedence.
NOTE:
the explanation would be trivial: follow operator precedence!!!
Trivially: array access operator [] takes precedence in expression (it is even higher than ()).
Complication: array access operator [] has two parts: (1) left-hand outer part to reference array, (2) inner part to reference index within array. And the 1st part is evaluated first. That's it - see Example 15.10.4-1. Array Reference Is Evaluated First.
It could have been much easier explained if array access operator [] was included in official Operator Precedence.
NOTE:
- This link is to "Oracle Java Tutorial" (not even JSL) - it seems there is no section with such table in JSL!!!
- There is even no mentioning of operator () in this link to explicitly change precedence (just like the absence of array access [] operator).
the explanation would be trivial: follow operator precedence!!!
-
- Posts: 2
- Joined: Wed Sep 18, 2019 11:14 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.971 :
Me tome la molestia de replicar el ejercicio y el resultado efectivamente es la asignacion del valor 30 en la poscision 0
jinwchun wrote: ↑Wed Sep 04, 2013 10:15 ampublic class TestClass{
public static void main(String args[] ){
int i = 0 ;
int[] iA = {10, 20} ;
iA = i = 30 ;
System.out.println(""+ iA[ 0 ] + " " + iA[ 1 ] + " "+i) ;
}
}
Can anyone explain why iA = i = 30 doesn't get processed as follows?
iA[0] = i = 30 ----> iA[0] = 0 = 30
If the left side operand gets evaluated first, the iA[0] should be assigned i which was initially set 0 before i = 30 gets evaluated, correct?
-
- Posts: 7
- Joined: Wed Aug 12, 2020 4:44 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.971 :
I think, this information is useful here:
"...assignment operators are evaluated right to left."
https://docs.oracle.com/javase/tutorial ... ators.html
"...assignment operators are evaluated right to left."
https://docs.oracle.com/javase/tutorial ... ators.html
-
- Posts: 16
- Joined: Sat Nov 30, 2024 5:27 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.971 :
This is what lead me to the wrong answer for this question, because regarding array indexing, which is part of the left of the assignement, it's evaluated before the right operand (the contrary of the quoted statement). So the java documentation is confusing about it, if you don't read the full spec.futurecap wrote: ↑Thu Feb 25, 2021 4:34 amI think, this information is useful here:
"...assignment operators are evaluated right to left."
https://docs.oracle.com/javase/tutorial ... ators.html
Who is online
Users browsing this forum: Bing [Bot], witek_m and 14 guests