wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes array increment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "array increment" Watch "array increment" New topic
Author

array increment

prerna boja
Ranch Hand

Joined: Aug 19, 2004
Posts: 67
Hi,

public class Test3{
public static void main(String args[]){
int arr[] = new int[10];
int i = 5;
arr[i++] = ++i+i++;
System.out.print(arr[5]+":"+arr[6]);
}
}


when i try to do this program I am getting the output as 11:13 but the compiler shows as 14:0.

can any please expalin me the program.

Thanks in advance.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

The array reference on the left hand side of the assignment is evaluated first, to arr[5]. After this evaluation, i is incremented to 6.

Then the right hand is evaluated. First i is incremented to 7. This value is remembered, and then the "i++" is evaluated, giving 7. This second 7 is remembered, and then then i is incremented again, and i becomes 8. This value 8 is never used for anything, as i is never referenced again. The two remembered sevens are then added to give 14, which is assigned to the location arr[5]. That's it!


[Jess in Action][AskingGoodQuestions]
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Originally posted by prerna boja:
Hi,

public class Test3{
public static void main(String args[]){
int arr[] = new int[10];
int i = 5;
arr[i++] = ++i+i++;
System.out.print(arr[5]+":"+arr[6]);
}
}


when i try to do this program I am getting the output as 11:13 but the compiler shows as 14:0.

can any please expalin me the program.

Thanks in advance.


Where are you getting te output 11:13?
There is only one array variable being set and that happens to be index 5.
the line
arr[i++] = ++i+i++;
comes out to be (let's see if I can get this right)
arr[5] = 7 + 7;
IF I'm right it happens a such:
first the array access happens at 5 and then increments i to 6
the pre increment ups i to 7 giving you 7 + 7
then the post increment ups i to 8, but that doesn't get used.
I'm pretty sure that if you added a println(i) at the end you would see 8.
prerna boja
Ranch Hand

Joined: Aug 19, 2004
Posts: 67
Hi,

Thank you I got it,but still I have doubt

if i write the same program as

arr[i++] = i++ + ++i;----------------- 1

instead of arr[i++] = ++i + i++;---------------- 2

the out put is same ,but

when i try to solve the 1 equation like

see i=5
so arr[i++]= arr[5] (post increment then i=6)
so now i++ = 6 (ost increemnt then i=7)
then ++i= 7.

so arr[5] = 6 + 7=13 (is this correct?)

but i know that both 1 and 2 are one and the same .

still not getting satified as i feel the outputs for both of them is different.
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
it's not 6 + 7, it's still 7 + 7 because the ++i happens before the +
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
It's 6 + 8.
i++ returns 6, increments to 7
++i increments to 8, returns 8
Patrick Joseph
Greenhorn

Joined: Jan 11, 2005
Posts: 11
I'm seeing it as:
arr[i++] = ++i + i++; //7 + 7
//i = 8 now
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Vlado Zajac:
It's 6 + 8.
i++ returns 6, increments to 7
++i increments to 8, returns 8

If you look back at the code, i was already incremented to 7 in the subscript. So it's exactly like PJ and others say: 7+7 with i==8 in the end.

Layne


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: array increment
 
Similar Threads
Sort the Array in reverse order
how do I print the elements in the Arraylist without the null.
presedence problem
here is a question and i have a very similar code but i need to convert it
How does enhanced for loop behave?