# What is the difference between size and capacity of a Vector in Java?

The size of a vector represents the number of components in the vector. The capacity of a vector represents the maximum number of elements the vector can hold.

Example:

import java.util.*;

public class VectorDemo {
public static void main(String args[]) {
Vector v = new Vector(3, 2);
System.out.println("Initial size: " + v.size());
System.out.println("Initial capacity: " + v.capacity());
System.out.println("Capacity after four additions: " + v.capacity());
System.out.println("Current capacity: " + v.capacity());
System.out.println("Current capacity: " + v.capacity());
System.out.println("Current capacity: " + v.capacity());
System.out.println("First element: " + (Integer)v.firstElement());
System.out.println("Last element: " + (Integer)v.lastElement());
if(v.contains(new Integer(3)))
System.out.println("Vector contains 3.");
Enumeration vEnum = v.elements();
System.out.println("Elements in vector:");
while(vEnum.hasMoreElements())
System.out.print(vEnum.nextElement() + " ");
System.out.println();
}
}

Output:

Initial size: 0
Initial capacity: 3
1 2 3 4 5.45 6.08 7 9.4 10 11 12