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

后端 未结 16 2413
-上瘾入骨i
-上瘾入骨i 2020-11-22 02:34

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

相关标签:
16条回答
  • 2020-11-22 03:12

    Yes.

    public class IncrementTest extends TestCase {
    
        public void testPreIncrement() throws Exception {
            int i = 0;
            int j = i++;
            assertEquals(0, j);
            assertEquals(1, i);
        }
    
        public void testPostIncrement() throws Exception {
            int i = 0;
            int j = ++i;
            assertEquals(1, j);
            assertEquals(1, i);
        }
    }
    
    0 讨论(0)
  • 2020-11-22 03:14

    ++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
    
    0 讨论(0)
  • 2020-11-22 03:17

    These are known as postfix and prefix operators. Both will add 1 to the variable but there is a difference in the result of the statement.

    int x = 0;
    int y = 0;
    y = ++x;            // result: y=1, x=1
    
    int x = 0;
    int y = 0;
    y = x++;            // result: y=0, x=1
    
    0 讨论(0)
  • 2020-11-22 03:17

    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
    
    0 讨论(0)
  • 2020-11-22 03:18

    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

    0 讨论(0)
  • 2020-11-22 03:20

    I landed here from one of its recent dup's, and though this question is more than answered, I couldn't help decompiling the code and adding "yet another answer" :-)

    To be accurate (and probably, a bit pedantic),

    int y = 2;
    y = y++;
    

    is compiled into:

    int y = 2;
    int tmp = y;
    y = y+1;
    y = tmp;
    

    If you javac this Y.java class:

    public class Y {
        public static void main(String []args) {
            int y = 2;
            y = y++;
        }
    }
    

    and javap -c Y, you get the following jvm code (I have allowed me to comment the main method with the help of the Java Virtual Machine Specification):

    public class Y extends java.lang.Object{
    public Y();
      Code:
       0:   aload_0
       1:   invokespecial  #1; //Method java/lang/Object."<init>":()V
       4:   return
    
    public static void main(java.lang.String[]);
      Code:
       0:   iconst_2 // Push int constant `2` onto the operand stack. 
    
       1:   istore_1 // Pop the value on top of the operand stack (`2`) and set the
                     // value of the local variable at index `1` (`y`) to this value.
    
       2:   iload_1  // Push the value (`2`) of the local variable at index `1` (`y`)
                     // onto the operand stack
    
       3:   iinc  1, 1 // Sign-extend the constant value `1` to an int, and increment
                       // by this amount the local variable at index `1` (`y`)
    
       6:   istore_1 // Pop the value on top of the operand stack (`2`) and set the
                     // value of the local variable at index `1` (`y`) to this value.
       7:   return
    
    }
    

    Thus, we finally have:

    0,1: y=2
    2: tmp=y
    3: y=y+1
    6: y=tmp
    
    0 讨论(0)
提交回复
热议问题