Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array!

 
sc suk
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ravindra Mohan
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Suk,
Please refer to my code below to understand the mechanism as to what happens during post increment.

Hope this clears your doubt.
Ravindra Mohan..
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sc suk:
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.


Could anyone explain why anyone not drunk would write code like this?
 
Ravindra Mohan
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Thomas,
Yeah . Just to test how alert can a person be even after
having drinks.

Could anyone explain why anyone not drunk would write code like this?

Ravindra Mohan.
 
Micah Holroyd
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only people I could see writing that code would be drunks, sadists, or writers of Java certification tests!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic