This week's book giveaway is in the Jobs Discussion forum.We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!See this thread for details.
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!

# reusult of this a = a + a++;

aftababbasi
Greenhorn

Joined: Nov 22, 2000
Posts: 4
if v run this code of line
int a = 10;
a = a + a++;
System.out.println(a);
so why we get answer of a 20 not 21
tell me

Keep Smilling
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Simply do the math:
a + a = 10 + 10 = 20
a = 20
a++ = 20 + 1 = 21

42
aftababbasi
Greenhorn

Joined: Nov 22, 2000
Posts: 4
Sorry Jeroen,
I dont understand. but u mentioned math formula i know this but friend I get 20 not get 21 result understand
Angela Poynton
Ranch Hand

Joined: Mar 02, 2000
Posts: 3143
a++ uses a post-increment .. this means the value will be used THEN incremented ... that's why you get 20. If oyu want 21 use ++a this will increment first then get the value.
Here's a little code to prove my point, put this in a method and see how it works:

The output should be (I havn't compiled this to check but I thin k I'm right)
The result of a+ a++ = 20
The result of a+ a++ = 22
The result of a+ a++ = 24
The result of a+ a++ = 26
The result of a+ a++ = 28
The result of b+ ++b = 21
The result of b+ ++b = 23
The result of b+ ++b = 25
The result of b+ ++b = 27
The result of b+ ++b = 29

Pounding at a thick stone wall won't move it, sometimes, you need to step back to see the way around.
shree vijay
Ranch Hand

Joined: Sep 18, 2000
Posts: 208
Hi Angela,
the expression is a = a + a++;
the ++ is a postfix. Even then, let us break down like this

a = a + a;
a++;
the result of 20 is quite clear if there is some assignment like this c= a + a++;
but since the result is assigned to 'a' itself, which a is incremented, the value before the assignment , or the one after?
I guess there is only one memory storage location for a .
Thanks,
Shree

Regards,<BR>Shree
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
aftababbasi
PROPER NAMES ARE NOW REQUIRED!!
Javaranch appreciates your cooperation to comply with the official naming policy.
Ajith

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Edy Yu
Ranch Hand

Joined: Nov 21, 2000
Posts: 264
This question is also related to the evaluation order.
Reffering to JLS 15.7.1, you will find a similar example involved in evaluation order. Just keep one thing in mind, in this statement, a ++ has the highest order and the value of (a ++) is 10, not 11.

SCJP, SCJD, SCWCD, SCBCD, SCEA, IBM Certified Enterprise Developer, WebSphere Studio V5.0
Edy Yu
Ranch Hand

Joined: Nov 21, 2000
Posts: 264
JLS 15.7.2 says: "Evaluate operands before operation".
Rob Levo
Ranch Hand

Joined: Oct 01, 2000
Posts: 167
Nobody has answered the question. It looks like the ++ operator never gets done or the result is never being assigned to variable a.
Can someone please provide an explanation of this.
Thanks.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Rob,
If you re-write the code as follows:

You'll see an output of:

'a' IS assigned a value of '11' after the postfix, but since the expression result is also assigned to 'a' in the original version, the postfix result is overwritten.
Java does something like this:

1. Evaluate 'a', which is 10.
2. Postfix operator has highest precedence. Take the initial value of 'a', 10, and keep it in memory for use in the '+' expression. Increment 'a' by '1'; so 'a' = 11.
3. Now do the '+', initial value of 'a' + initial value of 'a' = 10 + 10 = 20
4. The right hand side of the expression completes, with a value of '20'.
5. Assign the result, 20, to 'a' which OVERWRITES the old value of 11.

6. Hope that helps.
------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker

Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Rob Levo
Ranch Hand

Joined: Oct 01, 2000
Posts: 167
Thanks Jane. I get it now.
asim wagan
Ranch Hand

Joined: Nov 14, 2000
Posts: 62
HI! here see how it happens:
a=a+a++;
This can be broken into three steps:
i) Calculation of a+a;//here 20 is calc.
ii) Calculation and assignment of a=a+1 /11 is calc.
iii) Calculation of a=a+a; //here it replaces original.
if that would have been like this : a=a+(++a);
Then everything would have benn performed in steps i,iii,ii
So we would get diffrent values.
Greenhorn

Joined: Nov 18, 2000
Posts: 21
hai Jane
i completely agree with you..
but when i say
int a = 10;
a = a++;
s.o.p(a);
well i didn't get the expected answer
vijay

Originally posted by Jane Griscti:
[B]Hi Rob,
If you re-write the code as follows:

You'll see an output of:

'a' IS assigned a value of '11' after the postfix, but since the expression result is also assigned to 'a' in the original version, the postfix result is overwritten.
Java does something like this:

1. Evaluate 'a', which is 10.
2. Postfix operator has highest precedence. Take the initial value of 'a', 10, and keep it in memory for use in the '+' expression. Increment 'a' by '1'; so 'a' = 11.
3. Now do the '+', initial value of 'a' + initial value of 'a' = 10 + 10 = 20
4. The right hand side of the expression completes, with a value of '20'.
5. Assign the result, 20, to 'a' which OVERWRITES the old value of 11.

6. Hope that helps.
[/B]

Anil Kollur
Ranch Hand

Joined: Mar 25, 2000
Posts: 101

Hi! VIJAY,
I tried this:

public class Test1{
int b = 10;
void incrementB(){
b = b++;
System.out.println("b value is" + b);
}
public static void main(String[] args) {
int a = 10,c = 10;
Test1 t = new Test1();
t.incrementB();
a = a++;
c++;
System.out.println("a value : " + a + " c value : " + c);
}
}

The output is:

b value is 10
a value : 10 c value : 11

why is it so? a and b values r 10 but c value is 11
Can anybody explain? Anna or Ajith?

Everyday is a Learning phase! Hence ask "What have I learnt today?"
asim wagan
Ranch Hand

Joined: Nov 14, 2000
Posts: 62
Guys! I am watching that most of you don't get what is the difference between a++ and a=a++ let me breakdown this for you get the meaning of these statments.
i) let us first talk about a++, a++ is broken down into

a++ -------> a=a+1
ok all of you agree with this. Now see again.
ii) The statement a=a++ is broken into
a=10;
a=a++; ---------> i) As a=10; when it is post incremented
it gets the value as 11 because there
is a term a++.
(i think you all agree ok)
ii) But as this is post increment so the
prev value of a which is 10 is used
and send back to the a. So!!! after
the execution of a=a++ it will return
back to original value.
I hope that you grasp this thing. See my previous posting how it works. Hope you will understand.

kuchana anil
Greenhorn

Joined: Nov 26, 2000
Posts: 27
Hi guys get the operator precedence right.
***************************************** http://www.anilbachi.8m.com home for scjp tutorials, objective wise mock exams, daily scjp newsletter from a man who scored 100%
*****************************************
[This message has been edited by kuchana anil (edited November 27, 2000).]

Visit <A HREF="http://www.anilbachi.8m.com" TARGET=_blank rel="nofollow">http://www.anilbachi.8m.com</A> for scjp resources[tutorial, mock exams, daily newsletter and many more..]
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Vijaykumar,
The following is from JLS&15.14.1:

At run time, if evaluation of the operand expression completes abruptly, then the postfix increment
expression completes abruptly for the same reason and no incrementation occurs. Otherwise, the
value 1 is added to the value of the variable and the sum is stored back into the variable. Before the
addition, binary numeric promotion (�5.6.2) is performed on the value 1 and the value of the variable.
If necessary, the sum is narrowed by a narrowing primitive conversion (�5.1.3) to the type of the
variable before it is stored. The value of the postfix increment expression is the value of the variable
before the new value is stored.

The line in bold is the key. When you write

The postfix operator completes first and 'a' is set to '11' THEN the assignment ( = ) expression completes, and it assigns the original value of 'a' ... the one BEFORE the postfix ... to the variable, which in this case is once again 'a' ... the postfix value is overwritten.
Hope that helps.
------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
Read this one, entertaining, and easy to understand: http://members.spree.com/education/javachina/Cert/FAQ_SCJP3.htm#mis_Q3
Sahir Shah
Ranch Hand

Joined: Nov 05, 2000
Posts: 158

??
[This message has been edited by Sahir Shah (edited March 27, 2001).]

....
Greenhorn

Joined: Nov 18, 2000
Posts: 21
Hai Jane
Thanks a lot!
now it is crystal clear for me

Thanks a lot
bye

I agree. Here's the link: http://aspose.com/file-tools

subject: reusult of this a = a + a++;