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


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

Array value

Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Hi,

Why I am getting the output as 30 rather than 2. Based on line #7 , I am thinking a[2] = 2; Please advice


OCPJP 6.0-81% | Preparing for OCWCD
http://www.certpal.com/blogs/cert-articles | http://sites.google.com/site/mostlyjava/scwcd |
Rikesh Desai
Ranch Hand

Joined: Jun 02, 2010
Posts: 83
If you write your print statement as:
System.out.println(a[4]); then the output will be 2..

OCPJP 95%
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
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

Joined: Jun 02, 2010
Posts: 83
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

Joined: Sep 23, 2010
Posts: 422
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

Joined: Jun 02, 2010
Posts: 83
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

Joined: Jun 02, 2010
Posts: 83
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

Joined: Sep 26, 2010
Posts: 155
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.


OCPJP6
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Thanks Rikesh
Trivikram Kamat
Ranch Hand

Joined: Sep 26, 2010
Posts: 155
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

Joined: Sep 23, 2010
Posts: 422
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

Joined: Oct 05, 2010
Posts: 102
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

Joined: Sep 26, 2010
Posts: 155
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

Joined: Sep 26, 2010
Posts: 155
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array value
 
Similar Threads
Exception Question.
operand evaluation order
Confuse
one more question on arrays only
Multiple Assignments from Khalid's book