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

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

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

16条回答
  •  遥遥无期
    2020-11-22 03:25

    OK, I landed here because I recently came across the same issue when checking the classic stack implementation. Just a reminder that this is used in the array based implementation of Stack, which is a bit faster than the linked-list one.

    Code below, check the push and pop func.

    public class FixedCapacityStackOfStrings
    {
      private String[] s;
      private int N=0;
    
      public FixedCapacityStackOfStrings(int capacity)
      { s = new String[capacity];}
    
      public boolean isEmpty()
      { return N == 0;}
    
      public void push(String item)
      { s[N++] = item; }
    
      public String pop()
      { 
        String item = s[--N];
        s[N] = null;
        return item;
      }
    }
    

提交回复
热议问题