Lion
Lion

Reputation: 19027

A really very simple expression in Java that is evaluated in a way that it is difficult to follow

While developing a web application in JSF, I came across an expression which was evaluated in a way that I couldn't follow. Let's represent the same expression with a simple Java code.

final public class Main
{
    public static void main(String[] args)
    {            
        int x=5;
        int temp=10;

        temp=temp + 1 + x;
        System.out.println("temp = "+temp);

        int var=10;
        var=var++ + x;
        System.out.println("var = "+var);
    }
}

In the above simple code snippet, the first case is obvious and it displays the value 16 through the statement System.out.println("temp = "+temp);. There is no question at all about it but in the second case, I have represented the same thing using the shorthand operator ++ (var++) and that causes the value 15 to be displayed on the console instead of displaying 16. How is this expression evaluated here?


Here, there is an obvious thing that should be mentioned. In the expression var++, 10 is first passed and that value (not 11) is used to evaluate the entire expression on the right of the assignment which is in this case var++ + x and then var is incremented by 1. Accordingly, the assignment to itself (var) can not be observed here. Where was the incrementation lost?

Upvotes: 0

Views: 248

Answers (5)

Kaypro II
Kaypro II

Reputation: 3340

This is the expected behavior of the post-increment operator. You can see what's going on more clearly by running the following:

int var = 10;
var = var++;
System.out.println("var = " + var);

var = 10;
var = var++ + var;
System.out.println("var = " + var);

This should produce the output:

10
21

The post-increment operator returns the current value of the variable in the current expression, then increments it, so the incremented value is only visible the next time that variable is used.

In your code, you overwrite the value of var before the incremented value is ever used (so the incrementation is "lost"). I imagine the byte code is something analogous to:

// r holds the intermediate value of the expression while it's calculated
int x = 5;
int var = 10;

int r = r + var;
var = var + 1;  // note var is not read after this point
r = r + x;
var = r;

Upvotes: 1

Gilbert Le Blanc
Gilbert Le Blanc

Reputation: 51445

You assigned a value to var,so the auto increment is executed but overwritten.

Your code would be better written as:

int var = 10;
var += x + 1;
System.out.println("var = " + var);

Upvotes: 2

r0ast3d
r0ast3d

Reputation: 2637

According to the Java Language Specification

The value of the postfix increment expression is the value of the variable before the new value is stored.

http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.14.2

Upvotes: 2

JB Nizet
JB Nizet

Reputation: 691685

var++ evaluates to var, and then increments var. So your expression is in fact evaluated to var + x.

The sequence of actions is the following:

  • evaluate var++ : 10
  • increment var : var = 11
  • add x to 10 : 15
  • assign the result of the addition to var : var = 15

Anyone programming like this should be banned from development, IMHO.

Upvotes: 4

Mechkov
Mechkov

Reputation: 4324

Post increment operator like x++ means first you use the value x then you increment and in our case

 var=var++ + x;

assignment takes precedence. Try this

 var=++var + x;

and you should get the value of 16.

Upvotes: 3

Related Questions