• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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
 
He's giving us the slip! Quick! Grab this tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic