• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Increment Decrement Operators...Yet Again!!

 
Naveen Haimsley
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know this has been discussed umpteen times before.
But can anyone come up with an easier explanation.....preferably in layman terms to make a new bee happy!?

What and how?

int i=0;
i=i++;
System.out.println(i);
 
James X Peterson
Whizlabs Java Support
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Naveen Haimsley,

int x=1;
int i=x++;


As we have used post increment first the value of x is stored in i and the value of x is incremented. so the values of x and i are 2 ,1 respectively.

int x=1;
int i=++x;

As we have used pre increment first the value of x is incremented and the that incremetned value is stored in i. so the values of x and i are 2 ,2 respectively.

Regards,
James
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic