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