Important Java Concept

Difference between Length and Size in Java

We will learn about the differences between the length and size in java. Before going directly into the differences here is the definition and uses of length and size.

Length

1. .length

Length is nothing but the number of elements an array can hold, or the size of the array. You can use this length attribute or variable to find the size of the array in java by using the dot operator before the array name.

import java.util.*;
class Main {
          public static void main(String[] args) {
	     int[] arr = new int[10]; 
	     System.out.println("The length of the array is: " +arr.length);
             }
         }

Output

The length of the array is:  10

NOTE: In .length, even if the elements are added to the array, the array’s length would not change.

2. length()

The length () method is a final method in java, applicable for string objects. By using this method you can find the number of characters present inside a given string.

import java.util.*;
class Main {
       public static void main(String[] args) {
	  String str = "Hello everyone, this is aniket malik";
	  System.out.println("length of the string is: " +str.length());
         }
    }

Output

length of the string is: 36

In the above-given input to find the length of the string, the compiler includes characters, special characters, and white spaces between the characters. If we consider only characters there are 29 characters, but the output was printed as 36 because the compiler included 5 white spaces, and 1 special character(“,”). So the total became 29+5+1 = 36 characters.

NOTE: If we use .length to find the length of a string it would raise an exception.

NOTE: If we use length() to find the length of an array, it would raise an exception.

Size

1. size()

size() method is mostly used in lists. size() method in java is used to get the number of elements present inside a list. Unlike length, the value returned by the size method is not constant and changes according to the number of elements inside the list. Now let us see how the size method works by using the below java code.

class Main {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        System.out.println("The size of the arrayList, in the beginning, is : " + list.size());
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        System.out.println("The size of the arrayList after adding four elements is: " + list.size());
    }
}

Output

 The size of the arrayList, in the beginning, is: 0
 The size of the arrayList after adding four elements is: 4

Conclusion

The .length is used to find the number of elements an array can hold or we can simply say that the maximum capacity of elements that can be stored inside an array. Length is static because if we declare the size of an array, we cannot change it inside the program.
The length() method is used to tell the length of the string.

The size() method is mostly used in lists. This method is used to display the number of elements present inside the list. It is dynamic, because as we are adding and removing elements from the list the size changes.

In this article, we have learned about what is length, how to use .length, how to use length(), size(), and finally the differences between length and size.

Do you want

Java Tutor?

Aniket Malik

Aniket Malik

ORACLE CERTIFIED JAVA TUTOR

🟢 Oracle Certified Java Expert

🟢 300 + ⭐ ⭐ ⭐ ⭐ ⭐

🟢 B.tech & M.tech in CSE

🟢 6+ Years of Teaching Experience

🟢 Worked as SE in Virtusa and Digidez

Do you want

Java Tutor?