I'd like to know why a postfix increment on a value being returned in a return statement is not evaluated if the increment occurs on the righthand side of the return statement.
I know that the value returned should be the pre-increment value, but assuming a class-level variable, the increment should still show up in the variable itself.
Here is my example.
public class ReturnDemo {
static int val=1;
public static void main(String[] args)
{
System.out.println(getVal());
System.out.println(val);
}
public static int getVal()
{
return(val=(val++));
}
}
I would have expected this to print out 1 2 but it actually printed out 1 1
If you do the same thing without the assign, it increments correctly.
This, for example:
public class ReturnDemo {
static int val=1;
public static void main(String[] args)
{
System.out.println(getVal());
System.out.println(val);
}
public static int getVal()
{
return(val++);
}
}
Returns 1 2 (It gets val at 1 then increments the variable val to 2.)
Why is it that there is a line drawn in the sand at evaluating the postfix increment within a return statement if it's on the righthand side of an assignment, but that same line is not applied to postfix increments that are not part of an assignment statement?