• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

arrays pre decrement strange behaviour

 
Raju Sri
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
C. Nimo
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Giuoco Krag
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Serena Zhou
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question:

Shouldn't it be evaluated from right to left? In this case:

Thanks in advance!
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Corey!

[ April 01, 2004: Message edited by: Jing Zhou ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic