Difference Between Length and Capacity in Java


In Java, the two terms length and capacity are related to the storage of elements in a collection like arrays, String and ArrayList. Length refers to the number of elements that are currently stored in a given collection, whereas capacity refers to the maximum number of elements that the collection can hold. In this article, we will explore the difference between Length and Capacity in Java.

Length vs Capacity in Java

Length

To get the length of an array we use its ‘length’ property and we can get length of a String using its built-in method ‘length()’. In the case of arrays, the length property will count the number of elements stored in it and in the case of Strings, the ‘length()’ method counts the number of characters contained by it.

Example 1

The following example illustrates the use of length property with an array.

public class Example1 {
   public static void main(String[] args) {
      int[] aray = new int[10]; // declaring array of size 10
      // initializing the array
      aray[0] = 1;
      aray[1] = 2;
      aray[2] = 3;
      // printing the length of array
      System.out.println("Length of the given array: " + aray.length);
   }
}

Output

Length of the given array: 10

In the above code, we have declared an array of size 10 means it can store a maximum of 10 elements. Since arrays are of fixed size, the length and capacity of an array are always equal in Java.

Example 2

In the following example, we will check the length of Strings using the length() method.

public class Example2 {
   public static void main(String[] args) {
      // declaration and initialization of strings
      String st1 = "Tutorialspoint";
      String st2 = "Tutorix";
      // printing the length of strings
      System.out.println("Length of the String 1: " + st1.length());
      System.out.println("Length of the given String 2: " + st2.length());
   }
}

Output

Length of the String 1: 14
Length of the given String 2: 7

Capacity

Since Strings are immutable, Java provides the StringBuffer and StringBuilder classes that are used to modify the Strings. To get the capacity of these classes, we use the in-built method named ‘capacity()’. By default, they have the capacity to hold 16 characters. Therefore, when we store any string in these classes the result will be 16 more than the actual length of string.

Example 2

The following example demonstrates the use of ‘capacity()’.

public class Example3 {
   public static void main(String[] args) {
      // initializing the string
      String st1 = "Tutorialspoint";
      // passing the string to StringBuilder object
      StringBuilder st2 = new StringBuilder(st1);
      // printing the length and capacity
      System.out.println("Length of the st1: " + st1.length());
      System.out.println("Length of the st2: " + st2.length());
      System.out.println("Capacity of st2: " + st2.capacity());
   }
}

Output

Length of the st1: 14
Length of the st2: 14
Capacity of st2: 30

Difference between Length and Capacity

From the above discussion, we can conclude the following differences between length and capacity: −

Length()

Capacity()

It is the method of String class.

It is the method of StringBuilder and StringBuffer classes

0 is the initial length of an empty String.

16 is the initial capacity of both StringBuilder and StringBuffer classes

It checks the total number of characters in a String

It checks the maximum number of elements that a collection can hold

When we copy a String, its length is considered.

It is not considered while copying

Conclusion

There exists a minor distinction between Length and capacity in Java, we can say that both complement each other. Although both are related to the storage of elements, length is the total count of characters or elements, whereas capacity tells the ability to hold maximum number of elements

Updated on: 21-Jul-2023

267 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements