Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array value

 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Why I am getting the output as 30 rather than 2. Based on line #7 , I am thinking a[2] = 2; Please advice
 
Rikesh Desai
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you write your print statement as:
System.out.println(a[4]); then the output will be 2..
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println(a[2]); //How output is 30, It should print 2 know ??
System.out.println(a[4]); // why it is 2 ??

I am thinking a[index] should be equal to a[2] because we assigned a value of 2 to index..Please advice
 
Rikesh Desai
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harikrishna Gorrepati wrote:
I am thinking a[index] should be equal to a[2] because we assigned a value of 2 to index..Please advice


Yes.. and the value of a[2] in the array is??? 30!

Just reiterating your statement again..
I am thinking a[index] should be equal to a[2] because we assigned a value of 2 to index.. and the value of a[2] in the array is 30.. so the value of a[index] is also 30..
You only answered your own query! nice!!!

hoping that was helpful..
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Rikesh..Let me put it in this way. What I am thinking is

Line #7 is --> a[index] = index = 2;

is equal to

index = 2;
a[index] = 2;
a[2] = 2;

So, when we say System.out.println(a[2]); It should print 2 know ?? but it is printing 30 ..why ??

 
Rikesh Desai
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harikrishna Gorrepati wrote:Sorry Rikesh..Let me put it in this way. What I am thinking is

Line #7 is --> a[index] = index = 2;

is equal to

index = 2;
a[index] = 2;
a[2] = 2;

So, when we say System.out.println(a[2]); It should print 2 know ?? but it is printing 30 ..why ??



It goes from left to right..
Line #7 is --> a[index] = index = 2;

1. a[index] is a[4] as the value of index here is 4.
2. then a[4] = index = 2.. this causes the value of index as 2 and the value of a[4] as also 2.
 
Rikesh Desai
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but no change has happened to a[2] here..
so it prints 30.
the only change that happened was to the value of a[4] and to the value of index.
that is why a[4] is printing 2 instead of 50.
 
Trivikram Kamat
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because assignment operator has lowest precedence in Java.

Consider the following code:


The output is 0
Because, in line 1 although i is incremented on the right side of the assignment, the value assigned to i on left side is the one before increment.
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rikesh
 
Trivikram Kamat
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, although index value is changed to 2 in this problem the value is assigned to a[4].
This is because 4 is previous index value.
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trivikram Kamat wrote:So, although index value is changed to 2 in this problem the value is assigned to a[4].
This is because 4 is previous index value.


So, What happened to the incremented value i.e., i++ in your Line 1 of code, Is it lost ?
 
Rohit Ramachandran
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe since expressions are evaluated from left to right,

a[index] = index = 2; actually happens this way.

a[index]= a[4]= index;
index=2;

Trivikram, thanks a lot for your example. Scary stuff. Didn't think it's how it'll end up lol. Could you explain the output though?
 
Trivikram Kamat
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harikrishna Gorrepati wrote:
So, What happened to the incremented value i.e., i++ in your Line 1 of code, Is it lost ?


There's only one variable i
i++ increments value stored in i to 1, but the assignment operator overwrites it to 0.
So, incremented value is overwritten.
 
Trivikram Kamat
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rohit Ramachandran wrote:
Trivikram, thanks a lot for your example. Scary stuff. Didn't think it's how it'll end up lol. Could you explain the output though?


You're welcome
I guess your explanation is correct at higher level. Only a JVM expert can tell what exactly happens in realtime.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic