views:

191

answers:

4

Please explain the difference between the Vector.add() method and the Vector.addElement() method, along with a sample code snippet

+8  A: 

add() comes from the List interface, which is part of the Java Collections Framework added in Java 1.2. Vector predates that and was retrofitted with it. The specific differences are:

  1. addElement() is synchronized. add() isn't. In the Java Collections Framework, if you want these methods to be synchronized wrap the collection in Collections.synchronizedList(); and

  2. add() returns a boolean for success. addElement() has a void return type.

The synchronized difference technically isn't part of the API. It's an implementation detail.

Favour the use of the List methods. Like I said, if you want a synchronized List do:

List<String> list = Collections.synchronizedList(new ArrayList<String>());
list.add("hello");
cletus
-1, You didn't spoon feed the entire answer. Sample code wasn't given for the usage of both methods.
camickr
@camickr If you're going to complain keep it to your/my answer and let at least one of the good answers here get upvoted to the top so people searching for this question can find the answer easily instead of wading through nonsense
Michael Mrozek
@camickr so basically you've retaliated against a (deserved) downvote and compounded a poor answer with even worse behaviour. I seriously suggest you figure out how to interact with people in SO in a mature way (for your own sake).
cletus
Don't worry, I didn't downvote your answer, I was being sarcastic since you answer doesn't answer the question. The answer is "there is no difference, the code is the same". The fact that one returns a boolean value is apparent if you take the time to read the API. If the question was "why are there two methods that do the same thing", or "why does one method return a boolean" then I would have understood that the OP took the time to read the API and was still confused. I don't believe quoting information found in the API helps the poster in the long run. Teach the poster reading skills!
camickr
A: 

The javadoc mentions that:

public void addElement(E obj)

This method is identical in functionality to the add(E) method (which is part of the List interface).

The reason they both exist is (from the same javadoc):

As of the Java 2 platform v1.2, this class was retrofitted to implement the List interface, making it a member of the Java Collections Framework.

List has an add method, so an implementation was added to Vector, but to maintain backwards-compatibility, addElement wasn't removed

Michael Mrozek
-1, for spoonfeeding and answer that is found in the API for the method description.
camickr
@camickr ...are you serious? You're going to downvote my complete answer because you're upset over the argument on your "RTFM" post?
Michael Mrozek
+2  A: 

The method signature is different, add returns true, while addElement is void.

from http://www.docjar.com/html/api/java/util/Vector.java.html

  153       public synchronized boolean add(E object) {
  154           if (elementCount == elementData.length) {
  155               growByOne();
  156           }
  157           elementData[elementCount++] = object;
  158           modCount++;
  159           return true;
  160       }

and

223       public synchronized void addElement(E object) {
  224           if (elementCount == elementData.length) {
  225               growByOne();
  226           }
  227           elementData[elementCount++] = object;
  228           modCount++;
  229       }
jskaggz
Technically true, but not the real reason, since the added return value is hardcoded to true and doesn't tell you anything useful
Michael Mrozek
Actually, `add` returns `true` (read the code, it *never* returns `false`)
Mark E
Thanks yes, only returns true. But it does return a value while the other is void.
jskaggz
Neither Vector nor the addElement method is final. Thus Vector can be extended and addElement can be overridden. In theory at least, an extension could have totally different behaviour (including a useful return value). If they wanted to enforce that addElement is equivalent to add, they should have written it thus: public final synchronized addElement ( E object ) { add ( object ) ; return true ; }
emory
A: 

addElement

This method is identical in functionality to the add(Object) method (which is part of the List interface).

So there is no difference between:

Vector v = new Vector();
v.addElement( new Object() );

and

Vector v = new Vector();
v.add( new Object() );

This class ( vector ) exists since Java1.0 and now is pretty much replaced by ArrayList which has the benefit of being slightly faster.

OscarRyz