This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Increment Operator

 
Karthick Thiagarajan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys can any one clear my doubt?

public class Test
{
public static void main(String[] args) {
int i= 1;
i=i++;
i=i++;
i=i++;
i++;
System.out.println("the value of i=" +i);

}

Answer is 2


why the incremented values are not assigning to 'i'?

thanks in advance..
 
alok Jain
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
suppose you have a expression like
int x= 1 , y = 2 , z = 0;
z = x++ + ++y ;

now what happens is :
1) first the expression on the right hand side is evaluated as below :
a) first all the prefix operators are applied to the operands .
here in the above example since y has prefix operator y becomes 3 .

2) Now the expression is evaluated and the result of the expression is kept in a temporary location.
3) If the operands have any postfix operator that is evaluated for that operand .

4) Now the assignment to the LEFT HAND SIDE takes place . The LEFT HAND SIDE is given the value evaluated in the step 2 mentioned above .

..If you apply above rules correctly you should be able to get that the value of i as 2 .
let us see what happens for
int i = 1;
i= i++;

if you follow my rules above then
Step 1) Not applicable
Step 2) the value of the expression is 1.
Step 3) the value of i becomes 2
Step 4) the value of the expression evaluated to 1 is now assigned to i ( overwriting the value 2 )
 
Abdullah Mamun
Ranch Hand
Posts: 99
Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to read this excellent article regarding this topic.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karthick and Aalok,

JavaRanch has a policy on display names. Your names are not according to this policy. Your name should consist of a first name, space and a second name.

Please take a moment to carefully read the policy, and then change your name so that it is according to the policy. You can change your name by editing your profile.

Please note that we take the naming policy seriously - which means that we might close your account if you don't change your name.

Jesper Young - Bartender
 
fred rosenberger
lowercase baba
Bartender
Posts: 12086
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is asked quite often. In fact, we have a FAQ on it.
 
raghu nagabandi
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the output is : 5

please chech in the system once.
 
Karthick Thiagarajan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic