• Post Reply Bookmark Topic Watch Topic
  • New Topic
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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

reusult of this a = a + a++;

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simply do the math:
a + a = 10 + 10 = 20
a = 20
a++ = 20 + 1 = 21
 
aftababbasi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
aftababbasi
PROPER NAMES ARE NOW REQUIRED!!
Please Read the JavaRanch naming policy for more details.
Javaranch appreciates your cooperation to comply with the official naming policy.
Ajith
 
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me add something:
JLS 15.7.2 says: "Evaluate operands before operation".
 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jane. I get it now.
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
please clarify
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
Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read this one, entertaining, and easy to understand: http://members.spree.com/education/javachina/Cert/FAQ_SCJP3.htm#mis_Q3
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

??
[This message has been edited by Sahir Shah (edited March 27, 2001).]
 
VIJAYKUMAR VADDEM
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic