问题
Consider the following code
int val1 = 3;
val1++;
int val2 = val1++;
System.out.println(val1);
System.out.println(val2);
Val1 value = 5; Val2 value = 4;
Why is the value of Val1 "5"?
As I understand it it should be 4, because: at line1 it is assigned value of 3, on line2 1 gets added by way of val1++ which result in val1 being 4.
Val2 is the value of val1 thus 4, plus 1 which is 5
HOwever the compiler gives val1 a value of 5 and val2 a value of 4, what am I not understanding or missing here?
I realize val1++ is used a second time but it is assigned to val2, and should not effect val1s value, or am I wrong?
回答1:
a++
is known as postfix.
add 1
to a
, returns the old value.
++a
is known as prefix.
add 1
to a
, returns the new value.
so int val2 = val1++;
will return the old Value (4)
and still add one to val1
回答2:
You can increment a variable by
val1++
or by using
++val1
The first option performs the assigment first and increments val1 afterwards. Long version:
val2 = val1;
val1 = val1 + 1;
The 2nd option performs the increment first and assigns then the new value to val2. Long version of this behaviour:
val1 = val1 + 1;
val2 = val1;
回答3:
Because the postfix increment operator add one to the val1
after the assignment to val2
, if you want to have val2
equal to 5 too you should use :
int val2 = ++val1;
A postfix expression followed by a ++ operator is a postfix increment expression. The value of the postfix increment expression is the value of the variable BEFORE the new value is stored(incremented).
To recap :
Post Increment(val++
) : First execute the statement then increase the value by one.
Pre Increment (++val
) : First increase the value by one then execute the statement.
回答4:
Basically,
int val2 = val1++;
has the same effect as:
int val2 = val1;
val1++;
回答5:
val++
is post-increment operation.
In post-increment/decrement operation a value is assigned first then it is incremented/decremented.
So, y = x++
is equivalent to y = x
x=x+1
In your case, val2
is assigned old value of val1
and the val1
is incremented, which gives output as
Val2 = 4
Val1=5
回答6:
Its simple:
int val2 = val1++; <--- this statement works like this
Step 1: int val2 is initialized
Step 2: val1's value is assigned to val2
Step 3: value of val1 is incremented by 1.
回答7:
++
operator aworks in 2 phases:
- returns current value of
val1
- add 1 to
val1
So in your scenario, you first add 1 to val1 (but you don't print it)
Secondly you assign to val2
value of val1, but after assigning it's value operator ++ adds 1 to val1
.
As for your update question: When you are assigning val1++
to a variable, you are first copying value from val1, then assigning it to val2
, and thirdly incrementing val1. That is why you have such result. Integers as well as primitives are copied by value, not reference, so change to val1
will not afect val2
.
For better understanding it, try to replace val1++
with ++val1
. In this way you will first increment value, and then return it, then you should have val1==val2==5
回答8:
int val1 = 3; here val1=3
val1++; here val1=4
int val2 = val1++;
and thus val1 becomes 5 and val2 also becomes 5 because before you assign value to val2 you increment as val1++
回答9:
The val++
is a Postfix Increment operation, which produces the original pure value stored in the variable before it is incremented.
You can take a look at Increment/Decrement Operators for further information.
In your case :
int val2 = val1++;
Will set the value of val1
to val2
before incrementing it, that's why you got 4
as value in val2
and 5
in val1
.
Note:
And if you don't want your operation to affect val1
value , you should have done this:
int val2 = val1+1;
来源:https://stackoverflow.com/questions/30069410/java-value-plus-variable