• 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

working of Operators

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone explain the below ?

case 1: why the O/P is o not 1 ?
int x=0;
x=x++;
System.out.println(x);

case 2: o/p Again 1 here ?
int x=0;
x++;
System.out.println(x);
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi !!!


In the case 1

int x=0
x=x++;
System.out.println(x);

In this case Assignment is done first
ie x gets the value 0 than the postincrement
operator increments x.
As x is assigned value 0 than it gets incremented
to 1 which is not assigned to x
so it prints 0.


Now in the second case

int x=0;
x++;
System.out.println(x);

here no assignment is done
but we are directly incrementing
x using postincrement operator
hence x value becomes 1
and we get output as 1.


I hope it helps!!!
 
sarada chellu
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi dhwani,
As you mentioned it get incremented what do you mean by it (isit x?)
i just want to know where the inceremented value will be stored.
and also x=x++ means x=x=x+1; is this right ?
could please explain in more detailed way?
Since after increment only i am trying to print the x value.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We have an entry in the JavaRanch FAQ about this: Post increment operator and assignment
 
The harder I work, the luckier I get. -Sam Goldwyn So tiny. - this ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic