Page 1 of 1

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

Posted: Wed Sep 04, 2013 10:15 am
by jinwchun
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?

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

Posted: Thu Sep 05, 2013 6:04 am
by admin

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

Posted: Sat Jul 11, 2015 8:52 pm
by anathema
On the explanation:
Evaluation Respects Parentheses and Precedence
aside from using mathematical operation like:

Code: Select all

int i = 3;
        int j =  3 * (9 + 3);
        System.out.println(j); //results to 36
are there any other examples that this rule apply? I tried using

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);
but it results to i: 1 and z:0. It seems that the evaluation on that if example is still from left to right.

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

Posted: Sun Jul 12, 2015 1:09 am
by admin
Well, what do you think 5 + 9/3; would print if the rules of precedence were not followed?

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

Posted: Fri Aug 07, 2015 3:12 pm
by sir_Anduin@yahoo.de
This is explained in detail here http://docs.oracle.com/javase/specs/jls ... l#jls-15.7
could you point me to the right spot? After reading for a while, I was not able to find the explanation.

Thanks :)

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

Posted: Fri Aug 07, 2015 11:34 pm
by admin
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.

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

Posted: Sat Aug 08, 2015 3:46 am
by sir_Anduin@yahoo.de
thanks, that was enough explanation for me :)

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

Posted: Sun Jun 19, 2016 12:46 am
by uvsmtid
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:
  • 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).
So, if these operators were listed in operator precedence table like in this unofficial Java material,
the explanation would be trivial: follow operator precedence!!!

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

Posted: Tue Apr 07, 2020 6:40 pm
by karlose2006
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 am
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?

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

Posted: Thu Feb 25, 2021 4:34 am
by futurecap
I think, this information is useful here:

"...assignment operators are evaluated right to left."

https://docs.oracle.com/javase/tutorial ... ators.html

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

Posted: Sat Dec 14, 2024 7:41 am
by dameest
futurecap wrote:
Thu Feb 25, 2021 4:34 am
I think, this information is useful here:

"...assignment operators are evaluated right to left."

https://docs.oracle.com/javase/tutorial ... ators.html
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.