• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Reg. PostFix Operator

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class InitTest{
public static void main( String args[] ) {
int a = 1;
a = a++;
b = b++ + b;
System.out.println(" Value of a : " + a );
System.out.println(" Value of b : " + b );
}
}
Output :
Value of a : 1
Value of b : 3
From the explanation :


If the operator is a compound assignment operator, the evaluation of left hand operand
includes remembering the variable that the left hand operand denotes and fetching and saving
the variable's value for use in the implied combining operation


1. I understood the output for variable b , but fail to understand for the variable a ??
2. Also in any expression if i have a postfix operator at the end of the expression
then the value will be incremented but not used ?
For eg :
int a = 1;
a = a + 2 + a++;
will result in output : a = 4
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Angela,
IMO
when you use postfix increement it has different behaviors when its an expression and when it's a statement without any expression.
What happens is
b=a++; //statement 1
....;//statement 2
two things happen at RHS
value to be used a=1 //1st value
result of post increement is:
value a=2 //2nd value
This second value is available to the program only after statement 1(if its not an expression)
or if there were an expression to be evaluated following a++,i.e
b=a++ +1; //not before
Now coming on to your code:
There is no expression to be evaluated.So the value of increement will be discarded.
See what happens:
a=a++;
first value=1
second value=2
in the process, a has increemented to 2.But the value that could be used for assignment, within the statement is 1 so a again, has become 1.
This is the same behavior that is happening in
a = a + 2 + a++;
I hope that, was able to communicate(clearly), what I understood.
THANKS
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<PRE>
I am still not clear here.
What is the difference between
set1)
a=1;
a = a + 2 + a++;
Output: a =4
set2)
a=1;
a = a++ + 2 + a;
Output: a = 5
Why am I getting two different answers here?
------------------- JLS ------------------
15.7 The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right
15.7.2 Evaluate Operands before Operation
The Java programming language also guarantees that every operand of an operator (except the conditional operators &&, | |, and ? appears to be fully evaluated before any part of the operation itself is performed.
------------------------------------------------
Also, the ++ operator has high precedence over binary operator +
Now my understanding is as follows:
The RHS is evaluated like this:

In Set1,
(a + 2) + a++; // point 15.7 in JLS
= (1 + 2) + a++
= 3 + a++
= 3 + 1
= 4 (a has a value 2 in the buffer, but does not get assigned to the var 'a')
In Set2,
a++ + 2 + a;
= (a++ + 2) + a
= (1 + 2) + a
= 3 + a (also a++ gets evaluated which results in a = 2 in the buffer, not in the variable 'a')
= 3 + 2
= 5
try this stmt
a=1;
a=a++ + 2 + a + a++ + 1 + a++;
results in a = 11
if a++; is a standalone stmt then it increments it by 1 and saves it in the var.
My understanding may be wrong. Please comment.
Regards
Usha
</PRE>
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Bindesh and Usah: the first time I understand these kind of statements
 
Bindesh Vijayan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all let me make a request
Please don't quote your postings in small font.Please..
And please tell me how did you get the concept of buffer.Since its new for me.Is there anything else?
THANKS
[This message has been edited by Bindesh Vijayan (edited September 05, 2001).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic