Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

array increment  RSS feed

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Sheriff
Posts: 24220
40
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's not 6 + 7, it's still 7 + 7 because the ++i happens before the +
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's 6 + 8.
i++ returns 6, increments to 7
++i increments to 8, returns 8
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm seeing it as:
arr[i++] = ++i + i++; //7 + 7
//i = 8 now
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!