Page 1 of 1

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

Posted: Tue Oct 30, 2018 7:43 pm
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);

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

Posted: Wed Oct 31, 2018 3:06 am
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].

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

Posted: Wed Oct 31, 2018 3:45 pm
by herbertscbr
ok

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

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

Posted: Mon Nov 19, 2018 3:10 pm
by herbertscbr
correct code:
int i = 0 ;
int[] iA = {10, 20} ;
iA ['i'] = i = 30 ;//HERE
System.out.println(""+ iA[ 0 ] + " " + iA[ 1 ] + " "+i) ;

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

Posted: Mon Nov 19, 2018 9:28 pm
by admin
I am not sure whether you are asking a question or just commenting. Could you please be more clear?
Paul.

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

Posted: Wed Nov 16, 2022 7:44 am
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?

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

Posted: Wed Nov 16, 2022 8:26 am
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 :)

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

Posted: Wed Nov 16, 2022 8:32 am
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.