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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Ron McLeod
• Liutauras Vilda
• Paul Clapham
• paul wheaton
Sheriffs:
• Tim Cooke
• Devaka Cooray
• Rob Spoor
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Tim Moores
• Carey Brown
• Mikalai Zaikin
Bartenders:

# Arrays

Greenhorn
Posts: 12
• Number of slices to send:
Optional 'thank-you' note:
Q1>
public class lost {
public static void main (String [] args){
int [] array = new int[10];
int i=5;
array[i++]=++i+i++;
System.out.println(array[5]+" "+array[6]+" "+i);
}
}
When i tried to execute this code,i got answers to be as 14 0 8.
Could anybody answer how it could be 14 0 8.
because i think that array[i++]=++i+i++;for
int i = 5 is
array[5]=6+5=11.
array[6]=?
i=?

Trailboss
Posts: 23731
• Number of slices to send:
Optional 'thank-you' note:
When you init an int array, all values are initialized to zero.
i ends up being 8.
I have no idea where 14 is coming from.

Ranch Hand
Posts: 117
• Number of slices to send:
Optional 'thank-you' note:
I think the value of the expression (when i equals 6)
++i + i++
(7) + (7)
is 14 and now the value of i is 8.
Here the mechanism of pre and post increment plays a role.

Ranch Hand
Posts: 18944
• Number of slices to send:
Optional 'thank-you' note:
I think the i value is calculated as
int i=5;
array[i++]=++i+i++;
The following associativity & Order of precedence are applied to the above expression. Order of precedence is as listed below:
Symbol - Associativity - precedence
++ (post increment) - Left to Right
++ (pre increment) - right to left
+ - Left to Right
First i++ of the expression array[i++]is evaluated - here i value of the array is 5, and is post incremented to 6.
Next, i++ of the expression ++i+i++ is evaluated as i = 6 and is postinremented to 7.
Next, ++i of the expression ++i+i++ is evaluated as i = 8, so finally 8 + 6 = 14.

Greenhorn
Posts: 7
• Number of slices to send:
Optional 'thank-you' note:
The assignment Operator is an exception for arrays.
In this case i value in array[i++] gets incremented after the assignment operator i=6 now.
next ++i of the expression gets incremented (value is 7 ) now.
then i++ which is ( 7+7 )
The result is 14.
Think a bit ul find it logical

Ranch Hand
Posts: 37
• Number of slices to send:
Optional 'thank-you' note:
I am still confused here.
array[i++] = array[6]
But I think after this operation i is still 5. so how come i = 6?

Greenhorn
Posts: 29
• Number of slices to send:
Optional 'thank-you' note:
Hi Tracy,

Originally posted by Tracy Qi:
I am still confused here.
array[i++] = array[6]
But I think after this operation i is still 5. so how come i = 6?

[] is evaluted first then post-increment ++. That means,
array[i++] yealds array[5] and i = 6.
After this the evaluation of the expression on the right side of the equal sign is performed, and I hope that you are clear on that.
Bala Arul.

[This message has been edited by Bala Arul (edited May 03, 2001).]

 I AM MIGHTY! Especially when I hold this tiny ad: a bit of art, as a gift, that will fit in a stocking https://gardener-gift.com