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

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

Moderator: admin

Post Reply
jinwchun
Posts: 1
Joined: Wed Sep 04, 2013 10:05 am
Contact:

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

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

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

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

Post by admin »


anathema
Posts: 3
Joined: Sat Jun 20, 2015 12:31 pm
Contact:

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

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

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

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

Post by admin »

Well, what do you think 5 + 9/3; would print if the rules of precedence were not followed?

sir_Anduin@yahoo.de
Posts: 62
Joined: Fri Aug 07, 2015 2:16 pm
Contact:

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

Post 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 :)

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

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

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

sir_Anduin@yahoo.de
Posts: 62
Joined: Fri Aug 07, 2015 2:16 pm
Contact:

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

Post by sir_Anduin@yahoo.de »

thanks, that was enough explanation for me :)

uvsmtid
Posts: 6
Joined: Sat Jun 18, 2016 2:11 am
Contact:

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

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

karlose2006
Posts: 2
Joined: Wed Sep 18, 2019 11:14 pm
Contact:

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

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

futurecap
Posts: 7
Joined: Wed Aug 12, 2020 4:44 am
Contact:

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

Post by futurecap »

I think, this information is useful here:

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

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

dameest
Posts: 16
Joined: Sat Nov 30, 2024 5:27 am
Contact:

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

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

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests