Java value plus variable++

青春壹個敷衍的年華 提交于 2019-12-06 15:35:26
B. Kemmer

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

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;

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.

Basically,

int val2 = val1++;

has the same effect as:

int val2 = val1;
val1++;

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

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.

++ 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

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++

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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!