• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

A simple Query

 
Ranch Hand
Posts: 1400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody explain me the logic behind the output of this code...!!!
public class Test{
public static void main(String args[]){
int i = 0;
i = i++;
System.out.println(i);
}
}
 
author
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"varun"
Thank you for your participation here at the Ranch. However, your name doesn't follow the JavaRanch guidelines. Please take a moment and change your "Publicly Displayed Name" by modifying your profile after reviewing the guidelines at http://www.javaranch.com/name.jsp

Thanks.
Sean
 
Sean MacLean
author
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This questions seems better suited to Java In General (Beginner) so I'm moving it there.
Sean
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The out put of this code will be 0. Because the increment is done after the assignment.
 
Ranch Hand
Posts: 1479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bosun Bello:
The out put of this code will be 0. Because the increment is done after the assignment.


More interesting is the question of why i never actually gets incremented below :
public class Test{
public static void main(String args[]){
int i = 0;
i = i++;
i = i++;
System.out.println(i);
}
}
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It gets incremented, but you are overwriting its value afterwards.
You are assigning i the value of an expression. To do this, the expression *first* gets evaluated.
So, i gets incremented - the value of i++ is the value of i before the increment.
Now that the expression has been evaluated, we can assign the result of the evaluation to i. So we are assigning the value of i before the increment to i.
It looks like the incrementation never happend, but it has.
Does that help?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic