Vector capacity() method in Java

In this post, we are going to look at the capacity() method of the Vector class in detail.

We have already talked about the capacity and capacityIncrement in one of our previous posts. When we create an object of the Vector class, then a default capacity of 10 is assigned to it, even though the size of the vector is still zero. And if we want to know the capacity of a vector at any place in our programs, then we have to use the capacity() method provided by the vector class.

  • Method declaration – public synchronized int capacity()
  • What does it do and returns? – It returns the current capacity of the vector.

Now, why is capacity important to the vector? As we know that the size of a vector can shrink or grow as per our need but there are things that happen behind the scene to support this. When we create an empty vector, then a default capacity of 10 is assigned to it which means that the vector can accommodate 10 elements now.
But what will happen if we try to add the 11th element? the capacity of the vector has to be increased to add the additional element, but again, by how much amount do we have to increase the capacity of the vector? This is where capacityIncrement comes into the picture. If we don’t assign any value to it, then the capacity of the vector would be doubled, otherwise, the capacity of the vector would be incremented by an amount defined by capacityIncrement.

Code example
public class Codekru {

	public static void main(String[] args) {

		Vector<String> v = new Vector<String>();

		System.out.println("capacity: " + v.capacity()); // default capacity of the
															// vector would be printed here

	}
}

Output –

capacity: 10

Now, if we assigned an initial capacity to the vector, then that would be returned by the capacity() method.

public class Codekru {

	public static void main(String[] args) {

		Vector<String> v = new Vector<String>(4); // assigned an intial capacity of 4

		System.out.println("capacity: " + v.capacity());

	}
}

Output –

capacity: 4

Now, if we fill up 4 elements in the vector, then the capacity of the vector would be doubled as we haven’t defined any capacityIncrement values.

public class Codekru {

	public static void main(String[] args) {

		Vector<String> v = new Vector<String>(4); // assigned an intial capacity of 4

		v.add("first");
		v.add("second");
		v.add("third");
		v.add("fourth");
		
		System.out.println("capacity: " + v.capacity());
		
		v.add("fifth");
		
		System.out.println("capacity now: " + v.capacity());

	}
}

Output –

capacity: 4
capacity now: 8
What if we try to use the capacity() method on a null vector?
public class Codekru {

	public static void main(String[] args) {

		Vector<String> v = new Vector<String>(4);

		v = null;

		System.out.println("capacity: " + v.capacity());

	}
}

Output –

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.Vector.capacity()" because "v" is null

Please visit this link if you want to know more about the Vector class in java and its other functions or methods.

Hope you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *