It's not a secret anymore!
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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Increment Operator" Watch "Increment Operator" New topic

Increment Operator

Karthick Thiagarajan

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;
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

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.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15094

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 8 API documentation
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

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

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

Joined: Aug 16, 2005
Posts: 15094

Karthick T, please check your private messages.
I agree. Here's the link:
subject: Increment Operator
It's not a secret anymore!