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

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

Moderator: admin

Post Reply
herbertscbr
Posts: 7
Joined: Wed Aug 22, 2018 9:57 am
Contact:

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

Post by herbertscbr »

Comparing question 39 with question 34:

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) ;
}
}

Why does it work from left to right in question 34, and from rigth to left in question 39? I thought it wouldn't work, because "j" wasn't initialized yet.
int i, j, k;
i = j = k = 9;
System.out.println(i);

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

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

Post by admin »

An expression is always evaluated from left to right but the associativity of the = operator is different from other operators. = operator is right associative. That is why i = j = k = 9; is grouped as i = (j = (k = 9));

Further, evaluation of an expression is a two step process. In the first step, the evaluation order from left to right is used to resolve array references that are to be used in expression evaluation. That is why, even though the second expression iA = i = 30; this is also grouped as iA = (i = 30); but the existing value of i i.e. 0 is substituted in iA to determine the element of the array. After that, i = 30 is evaluated and then the value of the expression i=30 is assigned to iA[0].
If you like our products and services, please help us by posting your review here.

herbertscbr
Posts: 7
Joined: Wed Aug 22, 2018 9:57 am
Contact:

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

Post by herbertscbr »

ok

iA = i = 30 ; -> iA[0] = 30;

herbertscbr
Posts: 7
Joined: Wed Aug 22, 2018 9:57 am
Contact:

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

Post by herbertscbr »

correct code:
int i = 0 ;
int[] iA = {10, 20} ;
iA ['i'] = i = 30 ;//HERE
System.out.println(""+ iA[ 0 ] + " " + iA[ 1 ] + " "+i) ;

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

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

Post by admin »

I am not sure whether you are asking a question or just commenting. Could you please be more clear?
Paul.
If you like our products and services, please help us by posting your review here.

aPerson
Posts: 15
Joined: Fri Aug 12, 2022 10:19 am
Contact:

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

Post by aPerson »

Hi, I've got some questions to make sure I understood this correctly:
1. "...For example, assuming all the variables are declared appropriately beforehand, a = b = c = d; is valid. ..."
At least "d" must be initialized for this to be correct right?

2. In your first reply to herbertschr, you are talking about his case "iA = i = 30", but did you mean "iA = i = 30"?

3. "...However, chaining to use a value of a variable at the time of declaration is not allowed. For example, int a = b = c = 100; ..."
Here b and c are not even declared right? So this example doesn't work because b and c don't exist if I'm not mistaken.
"int a = int b = int c = 100;" would be an example of using chaining on variables at the time of declaration?

aPerson
Posts: 15
Joined: Fri Aug 12, 2022 10:19 am
Contact:

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

Post by aPerson »

Ah, I know what happened here in my 2. question and in your explanation too: the brackets and the i in the iA array disappeared when we posted it, because it is interpreted as italics. That's why everything is in italics after that and the text makes no sense. Forget the 2. question then :)
Last edited by aPerson on Wed Dec 07, 2022 10:00 am, edited 2 times in total.

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

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

Post by admin »

1. Yes.
2. ---
3. Right, int a = b = c = 100; is invalid because b and c haven't been declared before this line. One could construe this statement as a declaration of b and c as well ( similar to the valid statement int a, b, c=100; ) but it is not valid. int a = int b = int c = 100; is also syntactically incorrect.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest