0
 public void push(E element) {
    if (size == elements.length) {
        resize(); // doubel of size
    }
    elements[size++] = element;
}


public E pop() {
    if (size == 0) {
        throw new java.util.EmptyStackException();
    }
    E element = elements[--size];
    elements[size] = null; // set null in last top
    return element;
}

what is the difference between a++ and ++a or a-- and --a in java

thanks

Wooble
  • 87,717
  • 12
  • 108
  • 131
Ghadeer
  • 63
  • 1
  • 1
  • 3

1 Answers1

23

Postfix Operation:

a++ or a-- is postfix operation, meaning that the value of a will get changed after the evaluation of expression.

x = a++;
// This can be rewritten as
x = a;
a = a+1;

// Similarly
y = a--;
// is equivalent to
y = a;
a = a-1;

Prefix Operation:

++a or --a is prefix operation, meaning that the value of a will get changed before the evaluation of expression.

x = ++a;
// This can be rewritten as
a = a+1;
x = a;

// Similarly
y = --a;
// is equivalent to
a = a-1;
y = a;

Example:

lets assume this;

a = 4;

b = a++; // first b will be 4, and after this a will be 5

// now a value is 5
c = ++a; // first a will be 6, then 6 will be assigned to c

Refer this answer also.

Denim Datta
  • 3,740
  • 3
  • 27
  • 53