• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Another question in arrays

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q2>
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 output as 13 0 8.
Can anybody try to explain this.
 
Trailboss
Posts: 23778
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
I get 10 0 8. So I'm lost too.
 
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 don't know how you got 10 0 8.
I think the right output is 13 0 8.
If you consider the expression,(when i = 6)
i++ + i++
(6) + (7)
you get 13. And i becomes 8.
I hope this answers your question.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When the JVM looks at the [i++] on the left side of the statement. It says ok I am referring to index 5 of this array. The next thing it does is increment i.
Then the JVM looks at the right side of the statement where it finds, Two ++ operators and a + operator.
Since the ++ increment operator has a higher order of precedence than the addition + operator.
Both of the increment operators fires before the addition operator.
email at nirwin@certifyonline.com
 
paul wheaton
Trailboss
Posts: 23778
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
I thought that when ++ was used as a post increment operator, the variable wasn't incremented until after it was used?
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Wheaton:
I thought that when ++ was used as a post increment operator, the variable wasn't incremented until after it was used?
I thought the same so the third unary operator ++ is executed last.


 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, I was under the distinct impression that the post increment operator, increments after the statement has executed. i.e the right hand side is assigned to the left, then the ++ operator does it thing.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The given problem is a good one on operator precedence .
int i = 5 at first
the expression assigns value to array[5] by :
array[i++]=i++ + i++;
array[i++] indicates array[5] after which i gets incremented by 1 .
rhs of exp encounters
i++ + i++
that is 6 ( i again gets incremented by 1 to 7) + 7 ( i again gets incremented by 1 to 8)
i.e array[5] = 6 + 7 = 13
System.out.println(array[5]+" "+array[6]+" "+i);
this prints out array[5] array[6](which is zero - default value) i ( which is 13)
hence the output should be 13 0 8

Originally posted by Meena:
Q2>
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 output as 13 0 8.
Can anybody try to explain this.


 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic