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

后端 未结 16 2412
-上瘾入骨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);
        }
    }
    

提交回复
热议问题