aspose file tools*
The moose likes Java in General and the fly likes index value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "index value" Watch "index value" New topic
Author

index value

amit patidar
Greenhorn

Joined: Jul 10, 2009
Posts: 12
Hi All,

in the program below

1 public static void main(String args[]){
2
3 int array[] = new int[5];
4 int index = 0;
5 array[index] = index = 3;
6 System.out.println(array[0]);
7
}

The output of the program is 3 at line 6.

At line 5 , the value of index is 0 in array[index] , why not it is 3

Please explain
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39855
    
  28
Is this a question from a mock exam? If so, make sure (please) to tell us where it is from.

Probably because Java evaluates expressions from left to right. It starts with array[index], at which point index is 0. Then it tries to assign that value, and assigns it to the value of the expression to the right of the first =.
amit patidar
Greenhorn

Joined: Jul 10, 2009
Posts: 12
Campbell ,

Thank you

and the question is not from any mock exam
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39855
    
  28
You're welcome

Now, everybody else, tell me whether I got it wrong!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14349
    
  22

Please use code tags when you post source code.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: index value