Win a copy of High Performance Python for Data Analytics this week in the Python forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Paul Clapham
• Ron McLeod
• Bear Bibeault
• Liutauras Vilda
Sheriffs:
• Jeanne Boyarsky
• Tim Cooke
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Jj Roberts
• Carey Brown
Bartenders:
• salvin francis
• Frits Walraven
• Piet Souris

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

Greenhorn
Posts: 4
• • Number of slices to send:
Optional 'thank-you' note:
• • 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

Ranch Hand
Posts: 5093
• • Number of slices to send:
Optional 'thank-you' note:
• • Simply do the math:
a + a = 10 + 10 = 20
a = 20
a++ = 20 + 1 = 21

aftababbasi
Greenhorn
Posts: 4
• • Number of slices to send:
Optional 'thank-you' note:
• • Sorry Jeroen,
I dont understand. but u mentioned math formula i know this but friend I get 20 not get 21 result understand

Ranch Hand
Posts: 3143
• • Number of slices to send:
Optional 'thank-you' note:
• • 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

Ranch Hand
Posts: 208
• • Number of slices to send:
Optional 'thank-you' note:
• • 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

Sheriff Posts: 5782
• • Number of slices to send:
Optional 'thank-you' note:
• • aftababbasi
PROPER NAMES ARE NOW REQUIRED!!
Javaranch appreciates your cooperation to comply with the official naming policy.
Ajith

Ranch Hand
Posts: 264
• • Number of slices to send:
Optional 'thank-you' note:
• • 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.

Edy Yu
Ranch Hand
Posts: 264
• • Number of slices to send:
Optional 'thank-you' note:
• • JLS 15.7.2 says: "Evaluate operands before operation".

Ranch Hand
Posts: 167
• • Number of slices to send:
Optional 'thank-you' note:
• • 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.

Ranch Hand
Posts: 3141
• • Number of slices to send:
Optional 'thank-you' note:
• • 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

Rob Levo
Ranch Hand
Posts: 167
• • Number of slices to send:
Optional 'thank-you' note:
• • Thanks Jane. I get it now.

Ranch Hand
Posts: 62
• • Number of slices to send:
Optional 'thank-you' note:
• • 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
Posts: 21
• • Number of slices to send:
Optional 'thank-you' note:
• • 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]

Ranch Hand
Posts: 103   • • Number of slices to send:
Optional 'thank-you' note:
• • 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?

asim wagan
Ranch Hand
Posts: 62
• • Number of slices to send:
Optional 'thank-you' note:
• • 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. Greenhorn
Posts: 27
• • Number of slices to send:
Optional 'thank-you' note:
• • 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).]

Jane Griscti
Ranch Hand
Posts: 3141
• • Number of slices to send:
Optional 'thank-you' note:
• • 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

Ranch Hand
Posts: 1953
• • Number of slices to send:
Optional 'thank-you' note:
• • Read this one, entertaining, and easy to understand: http://members.spree.com/education/javachina/Cert/FAQ_SCJP3.htm#mis_Q3

Ranch Hand
Posts: 158
• • Number of slices to send:
Optional 'thank-you' note:
• • ??
[This message has been edited by Sahir Shah (edited March 27, 2001).]

Greenhorn
Posts: 21
• • Number of slices to send:
Optional 'thank-you' note:
• • Hai Jane
Thanks a lot!
now it is crystal clear for me

Thanks a lot
bye With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.