wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Increment Operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Increment Operator" Watch "Increment Operator" New topic
Author

Increment Operator

Karthick Thiagarajan
Greenhorn

Joined: Feb 02, 2007
Posts: 14
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..


SCJP1.4 (81%)<br />SCWCD1.5(95%)
alok Jain
Greenhorn

Joined: Aug 24, 2007
Posts: 10
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

Joined: Mar 19, 2007
Posts: 99

You might want to read this excellent article regarding this topic.


MooN
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11152
    
  16

This question is asked quite often. In fact, we have a FAQ on it.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
raghu nagabandi
Ranch Hand

Joined: Aug 14, 2007
Posts: 35
I think the output is : 5

please chech in the system once.
Karthick Thiagarajan
Greenhorn

Joined: Feb 02, 2007
Posts: 14
Thank you guys
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Karthick T, please check your private messages.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Increment Operator
 
Similar Threads
mock
mock question
About Interfaces and Classes
run time error
same classname, different package