public class Stack<E> extends Vector<E>
可以看到Stack類繼承了Vector類
這個是stack類里面的方法:
/** * Tests if this stack is empty. * * @return <code>true</code> if and only if this stack contains * no items; <code>false</code> otherwise. */ public boolean empty() { return size() == 0; }
調用了vector的size方法
/** * Returns the number of components in this vector. * * @return the number of components in this vector */ public synchronized int size() { return elementCount; }
vector的size方法返回elementCount
這個是Vector的方法(線程安全的):
/** * Tests if this vector has no components. * * @return {@code true} if and only if this vector has * no components, that is, its size is zero; * {@code false} otherwise. */ public synchronized boolean isEmpty() { return elementCount == 0; }
可以看到二者沒有區別,都是看elementCount 是否為0
