| Author |
arrays pre decrement strange behaviour
|
Raju Sri
Ranch Hand
Joined: Mar 10, 2004
Posts: 108
|
|
Hi all, Pls check the below question . public class Friends { public static void main(String[] args) { // First set int index1 = 2; String[] friends1 = {"Micky","Pluto","Donald"}; String str1 = friends1[--index1] = friends1[--index1]+ " is my best friend."; System.out.println("str1="+str1); // Second set int index2 = 2; String[] friends2 = {"Micky","Pluto","Donald"}; String str2 = friends2[--index2] += " is my best friend."; System.out.println("str2="+str2); } } The output is : str1=Micky is my best friend. str2=Pluto is my best friend. Can somebody pls explain me why the output is so diffent with str1 and str2 eventhough we are doing same operation. From my understanding if we declare like below if we declare int i=5, then i+=5 and i=i+5 are same with only one exception that i+=5 will do implicity convertion like i=(int)i+5 Thanks in advances Cheers Raju
|
SCJP 1.4<br />SCWCD 1.4<br />SCBCD 1.3<br />SCDJWS 1.4
|
 |
C. Nimo
Ranch Hand
Joined: Mar 23, 2004
Posts: 82
|
|
Hi. This isn't the same operation at all. In the first example you decrement the index twice and in the second you only decrement the index once. I'd say the program is behaving exactly as it should under these circumstances. Nimo.
|
 |
Raju Sri
Ranch Hand
Joined: Mar 10, 2004
Posts: 108
|
|
Hi Nimo, Are you saying 1)friends2[--index2] += " is my best friend." 2)friends2[--index2] = friends2[--index2] + " is my best friend." 1 and 2 are not not same ?? . Then what is the meaning of += operator ?? how can you confirm both are different ? Can you pls give me the reason ? Cheers Raju
|
 |
Dan Andrei
Ranch Hand
Joined: Jan 21, 2004
Posts: 92
|
|
NO they are not the same operations: String str1 = friends1[--index1] = friends1[--index1]+ " is my best here first we decrement the index it becomes 1, and then we decrement the index again so its zero String str2 = friends2[--index2] += " is my best friend."; here we decrement the index its one, AND THAT INDEX ITS USED TO "EXPAND" THE ASSIGNMENT to friend[1]=friends[1]+ "...." Hope that helps Dan
|
"Did anyone understand what I have just explained? ... because I did not!"
|
 |
Giuoco Krag
Greenhorn
Joined: Mar 23, 2004
Posts: 10
|
|
|
I think another way to look at this is the "--", "++", "+=", etc... are assignment opperators. They actually assign a new value to the variable when applied. Each time the "--" is used, it re-assigns a new value to index1 or index2.
|
 |
Corey McGlone
Ranch Hand
Joined: Dec 20, 2001
Posts: 3271
|
|
Dan's absolutely correct - thay are not the same operation. Let's evaluate each statement and see what happens. In this case, you final assignment statement evaluates to this: As you can see, the pre-increment operator is being applied twice, which leaves index1 at 0. Now, let's look at the other case: In this case, the assignment statement evaluates to: In this case, the pre-increment operator is only applied once. The += operator operates like this: Note, for one thing, that the compound operators, such as +=, include an implicit cast. In addition, notice that it directly copies whatever is on the left side of the assignment to the first part of the addition. As operands are evaluated from left to right, we have already determined that the left operand of the += operator: is equal to: Therefore, that is what is copied to the right side of the compound operator, not: I hope that helps, Corey
|
SCJP Tipline, etc.
|
 |
Serena Zhou
Ranch Hand
Joined: Dec 13, 2003
Posts: 31
|
|
I have a question: Shouldn't it be evaluated from right to left? In this case: Thanks in advance!
|
 |
Corey McGlone
Ranch Hand
Joined: Dec 20, 2001
Posts: 3271
|
|
Originally posted by Jing Zhou: Shouldn't it be evaluated from right to left?
No, they are evaluated from left to right. From the JLS §15.7 Evaluation Order:
The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right.
Corey
|
 |
sameer kumar
Greenhorn
Joined: Mar 22, 2004
Posts: 13
|
|
dear friends i think u did long disscussion on this but i have one thing on this is that opearator [] has more persedence then = so it will first evalute the operator [] after that goes for assignment am i right can any body tell me
|
 |
Corey McGlone
Ranch Hand
Joined: Dec 20, 2001
Posts: 3271
|
|
Originally posted by sameer kumar: opearator [] has more persedence then = so it will first evalute the operator [] after that goes for assignment
Operands must be evaulated prior to any operators being applied, regardless of precedence. Therefore, given the line: x = someArray[--i]; We must first evaluate --i so that we can evaluate someArray[--i] so that we can evaluate x = someArray[--i]. Corey
|
 |
Serena Zhou
Ranch Hand
Joined: Dec 13, 2003
Posts: 31
|
|
Thanks, Corey!  [ April 01, 2004: Message edited by: Jing Zhou ]
|
 |
 |
|
|
subject: arrays pre decrement strange behaviour
|
|
|