• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Arrays

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 23999
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Where does a nanny get ground to air missles? Protect this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic