• 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

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

Consider the below example :
public class test
{
public static void main( String[] args )
{
int i = 3;
System.out.println(i *=2 + i++);
}
}
Output : 15
I am following the below steps :
1. i++ is executed , take the value of i and then incr
2. i *= 2 + 3
3. i *= (2+3) ( as + has higher precendence over *= )
4. i = i * 5 ( why is the value of i = 3 and not 4 over here
as should be obtained from step 1. )
Pls. can someone explain ?

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My hit is that first it puts the initial value
either in a reg or on the stack and then build the
right operand (2 + i +)
------------------
http://anticariatonline.com/gigel
 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't tried this but perhaps it's because in your final step
4) i = 3*5 --> returns 15 to the System.out.println statement and the ++post increment occurs after this.
So if you add this at the end of the main method:
System.out.println(i);
You will get 16.
When I get home I'll see if this is correct.
cheers,
Yoo-Jin.
[This message has been edited by Yoo-Jin Lee (edited August 31, 2001).]
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Angela

Originally posted by Angela Narain:

Consider the below example :
public class test
{
public static void main( String[] args )
{
int i = 3;
System.out.println(i *=2 + i++);
}
}


Here is what is happening:
The equation would be evaluated like this,
i = i * (2 + i++)
The first thing the compiler does is evaluate the value of all of the operands, in this case it is only i. Because it a postfix expression it has no effect on i until after the expression is evaluated so i is 3. Now it will start to solve the expressions.
i = 3 * (2 + 3)
i = 3 * 5
i = 15
The value of 15 is assigned to i. Even if you tried to print the value of i in a line afterwards it will still print 15, because the value of the expression is assigned back into i after the increment is done.
Look at this code here. If we use a nother variable to hold the value of the expression then i will be incremented as we expect.

If that didn't help let me know...


------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dave,
Thanks for the reply. I could trace out the result now.
I wanted to confirm, that suppose if i have just one single variable
'i' in the expression, the initial values will be assigned from
left to right.
 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So in terms of operator affinity, ++ binds closer than *=? Thus,
i*=2+ i++ becomes i = i * (2 + i++)
?
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assignment operators including =, +=, *=, /=, %=, -=, <<= >>=, >>>=, &=, ^=, |= are at the bottom of the precedence list. (They have the lowest precedence.)
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic