• 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

Operator precedence and postfix operators

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In a table precedence in C++, I've seen the prefix and postfix operators shown in separate places. In the seemingly rare times when someone actually lists a precedence table in Java, they don't make that distinction. So I did a test:

public class ATest
{
static int i=1;
static int j=2;
public static void main(String args[])
{
int k=i+(j=i++); // Line in question
System.out.println("i="+i);
System.out.println("j="+j);
System.out.println("k="+k);
}
}

The line in question wouldn't compile when I left out the parenthesis, but with them it showed i=2 j=1 and k=2. Can I then assume it

1. Set j=1
2. Set k=i+j which is k=1+1 which is 2
3. Set i=i+1 which is i=2

Does this mean that it does the postfix operations after all the other assignments are done?

Thanks,
Ben
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is distinction in Java also for prefix and postfix.
Order of Precedence : postfix then prefix.

k = i + (j=i++) //

i++ //value of i is used and then incremented
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The rule is that operators are evaluated from left to right.

In the expression above


The expression i is evaluated to 1 first, then the whole parenthesis is evaluated, that would be (j=i++). So j is assigned the value 1 and after that it increments i to value 2. But the left most i had already been evaluated before. So k will evaluate to 2, and i would have already been incremented.

You can see that's true if you evaluate the expression this way:


This way you will see i is incremented before evaluating the right most operand.
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ben Pracht:
The line in question wouldn't compile when I left out the parenthesis,


int k = i + j = i++; // the same as: // int k = ( i + j ) = i++;
is a multiple assignment statement that is assigning values for each variable from right to left in the form of:
a = b = c;
Then c has to evaluate to a value and is assigned to b, which has to be a variable (we cannot assign a value to another value), then the new value of the variable b is assigned to the variable a. In this example, however, ( i + j ) returns a value, not variable. A compiler notices that and won't compile.
Regards,
Andris
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic