aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Precedence/Associativity Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Precedence/Associativity" Watch "Precedence/Associativity" New topic
Author

Precedence/Associativity

Gautam Jha
Greenhorn

Joined: Sep 23, 2002
Posts: 10
I thought the following 2 questions where interesting and decided to share it with you all. I am sure these must have been discussed here is past. Apologies to the old timer, I mean to "rach hand-ers" (is that the highest level?).
Q1. What the value of i at line 4?
i = 0;
i = i++;
i = i++;
System.out.println("i "+i); //4
Q2.
int i = 0;
int myArray[] = new int[5];
myArray[i] = i = 3;
Which array index is getting updated?
Enjoy!!
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
q2 is interesting. Left most operand gets the value first. I did not think that way...
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by Barkat Mardhani:
q2 is interesting. Left most operand gets the value first. I did not think that way...

If the left operand is an array access expression then the index expression is evaluated before the right hand operands.
Please see
Section 15.26.1 of the Java Language Specification.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865

The answer is "a. Prints: [a,0],[b,1],[c,2],2,2,2".
The following is the remark associated with the answer.

The question contains a series of simple assignment operators where all of the operands are array access expressions. Starting from the left, the array index expression is evaluated before the right hand operand of the simple assignment operator. Evaluation of the array index expression causes method m to be invoked. The input parameter is the postfix increment expression with the operand i. Since the postfix increment expression returns the original value of the operand, the value zero is passed to method m along with a String containing the character a. Method m returns the value that was passed in as an input parameter. In this case, the return value is zero and method m prints [a,0]. As a side effect of the postfix increment expression, the value of variable i is one after the array index expression is evaluated. The right hand operand of the first simple assignment operator is evaluated next. The right hand operand is another array access expression similar to the first. The only difference is that the value of variable i is now one greater than it was at the time when the previous array access expression was evaluated. This time, method m returns one and prints [b,1]. The final array access expression is evaluated next. Method m returns 2 and prints [c,2].
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Thanks Dan...
Gautam Jha
Greenhorn

Joined: Sep 23, 2002
Posts: 10
Great question Dan!!
BTW, when I tried the first question I asked (credit goes to Vel Pariasamy and "the notes" for both the questions), I could not believe the answer (i=0) and it puzzled me for a long time.I thought it was an error. Good thing I tried it out by writing a small program.
I think the way I understood postfix (and the way its taught almost every where) is at fault.
Postfix to me meant (earlier): use the current value and
than increment the value of operand. But that does not work here. If that was the case we should have had value 2 for i and not 0. So something fundamental was at fault. The way postfix works is, value of operand is incremented but the return value is one less.
So k++ translates to
k = k+1;
return (k-1);
This has been losely translated to "Use the current value and then increment", which works most of the times but not all.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865

The answer is "b. Prints: 1,0"
The following is the remark associated with the answer.

The statement contains an addition operation. The left operand is a postfix operation and it is evaluated first. The result of the postfix operation is zero, but variable i is incremented as a side effect. The right hand operand of the addition operation is the result of method m. As a side effect, method m prints the current value of variable i which is one. Method m then returns the value zero. The result of the addition operation is zero and that is the value that is assigned to variable i.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Originally posted by G Jha:
Apologies to the old timer, I mean to "rach hand-ers" (is that the highest level?).

See this
link.
Bishal P
Ranch Hand

Joined: Sep 06, 2002
Posts: 43
GUess what? I am also a ranch hand now...


_ _____ _ <br />Used to be a Java Programmer but now I work on Microsoft Technologies - Word, Excel and Outlook!
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by G Jha:
Great question Dan!!
BTW, when I tried the first question I asked (credit goes to Vel Pariasamy and "the notes" for both the questions), I could not believe the answer (i=0) and it puzzled me for a long time.I thought it was an error. Good thing I tried it out by writing a small program.
I think the way I understood postfix (and the way its taught almost every where) is at fault.
Postfix to me meant (earlier): use the current value and
than increment the value of operand. But that does not work here. If that was the case we should have had value 2 for i and not 0. So something fundamental was at fault. The way postfix works is, value of operand is incremented but the return value is one less.
So k++ translates to
k = k+1;
return (k-1);
This has been losely translated to "Use the current value and then increment", which works most of the times but not all.

The apparent magic is a result of storing the original value of the operand on the stack before the increment operation.
i++ translates to the following.
  • Store the original value of i on the stack.
  • Increment i.
  • Pop the original value of i off of the stack and return it as the result of the postfix expression.
  •  
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
     
    subject: Precedence/Associativity