Is there a difference between x++ and ++x in java?

2018-12-31 07:43发布

Is there a difference between ++x and x++ in java?

16条回答
唯独是你
2楼-- · 2018-12-31 08:22

Yes, using ++X, X+1 will be used in the expression. Using X++, X will be used in the expression and X will only be increased after the expression has been evaluated.

So if X = 9, using ++X, the value 10 will be used, else, the value 9.

查看更多
何处买醉
3楼-- · 2018-12-31 08:23

If it's like many other languages you may want to have a simple try:

i = 0;
if (0 == i++) // if true, increment happened after equality check
if (2 == ++i) // if true, increment happened before equality check

If the above doesn't happen like that, they may be equivalent

查看更多
其实,你不懂
4楼-- · 2018-12-31 08:23

There is a huge difference.

As most of the answers have already pointed out the theory, I would like to point out an easy example:

int x = 1;
//would print 1 as first statement will x = x and then x will increase
int x = x++;
System.out.println(x);

Now let's see ++x:

int x = 1;
//would print 2 as first statement will increment x and then x will be stored
int x = ++x;
System.out.println(x);
查看更多
余欢
5楼-- · 2018-12-31 08:25

The Question is already answered, but allow me to add from my side too.

First of all ++ means increment by one and -- means decrement by one.

Now x++ means Increment x after this line and ++x means Increment x before this line.

Check this Example

class Example {
public static void main (String args[]) {
      int x=17,a,b;
      a=x++;
      b=++x;
      System.out.println(“x=” + x +“a=” +a);
      System.out.println(“x=” + x + “b=” +b);
      a = x--;
      b = --x;
      System.out.println(“x=” + x + “a=” +a);
      System.out.println(“x=” + x + “b=” +b);
      }
}

It will give the following output:

x=19 a=17
x=19 b=19
x=18 a=19
x=17 b=17
查看更多
永恒的永恒
6楼-- · 2018-12-31 08:26

++x is called preincrement while x++ is called postincrement.

int x = 5, y = 5;

System.out.println(++x); // outputs 6
System.out.println(x); // outputs 6

System.out.println(y++); // outputs 5
System.out.println(y); // outputs 6
查看更多
路过你的时光
7楼-- · 2018-12-31 08:31

In Java there is a difference between x++ and ++x

++x is a prefix form: It increments the variables expression then uses the new value in the expression.

For example if used in code:

int x = 3;

int y = ++x;
//Using ++x in the above is a two step operation.
//The first operation is to increment x, so x = 1 + 3 = 4
//The second operation is y = x so y = 4

System.out.println(y); //It will print out '4'
System.out.println(x); //It will print out '4'

x++ is a postfix form: The variables value is first used in the expression and then it is incremented after the operation.

For example if used in code:

int x = 3;

int y = x++;
//Using x++ in the above is a two step operation.
//The first operation is y = x so y = 3
//The second operation is to increment x, so x = 1 + 3 = 4

System.out.println(y); //It will print out '3'
System.out.println(x); //It will print out '4' 

Hope this is clear. Running and playing with the above code should help your understanding.

查看更多
登录 后发表回答