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.
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
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
Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
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.
<i><br />Sun Certified Programmer for Java 2 Platform (SCJP)<br />Sun Certified Developer for Java 2 Platform (SCJD)<br />Sun Certified Web Component Developer for Java2 Platform, Enterprise Edition (SCWCD)<br />Sun Certified Business Component Developer for Java2 Platform, Enterprise Edition (SCBCD)<br />Sun Certified Enterprise Architect for J2EE (SCEA)<br />IBM Certified Enterprise Developer, WebSphere Studio V5.0<br /></i>
Edy Yu
Ranch Hand
Joined: Nov 21, 2000
Posts: 264
posted
0
Let me add something: JLS 15.7.2 says: "Evaluate operands before operation".
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.
'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:
Evaluate 'a', which is 10.
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.
Now do the '+', initial value of 'a' + initial value of 'a' = 10 + 10 = 20
The right hand side of the expression completes, with a value of '20'.
Assign the result, 20, to 'a' which OVERWRITES the old value of 11.
Hope that helps. ------------------ Jane The cure for boredom is curiosity. There is no cure for curiosity. -- Dorothy Parker
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.
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:
Evaluate 'a', which is 10.
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.
Now do the '+', initial value of 'a' + initial value of 'a' = 10 + 10 = 20
The right hand side of the expression completes, with a value of '20'.
Assign the result, 20, to 'a' which OVERWRITES the old value of 11.
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
posted
0
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.
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
posted
0
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