Java String length() Method with Examples

Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java

The length() method is an important method in the Java String class that returns the number of characters present in a string. Since strings in Java are composed of Unicode characters, the length returned by the length() method is equal to the number of Unicode code units in the string.

The String class in Java implements the CharSequence interface, which defines the length() method. So, all Java strings inherit the length() method from the CharSequence interface.

Returns and Internal Implementation of Java length() method

The length() method returns an int value, which is the number of Unicode code units in the string. This value ranges from 0 for an empty string to 2,147,483,647 for a string containing the maximum number of Unicode code units.

Internally, the Java String class maintains a character array (char[]) to store the contents of the string. The length variable of this character array indicates the current length of the string. When length() is called on a string, it simply returns this length variable from the internal character array.

The character array representation with the length variable allows Java strings to be mutable in nature, unlike strings in other languages like Python, which are immutable.

Java String length() method examples

Example 1:

public class StringLengthExample {
    public static void main(String[] args) {
        String s1 = "Hello";
        String s2 = "World";

        int len1 = s1.length();
        int len2 = s2.length();

        System.out.println("Length of s1: " + len1);
        System.out.println("Length of s2: " + len2);
    }
}

Output:
Length of s1: 5
Length of s2: 5

Example 2:

public class CheckEmptyString {
    public static void main(String[] args) {
        String s = "";

        if (s.length() == 0) {
            System.out.println("String is empty");
        } else {
            System.out.println("String is not empty");
        }
    }
}

Output:
String is empty

Example 3:

public class ReverseStringUsingLength {
    public static void main(String[] args) {
        String s = "Hello";

        // Reverse string using length
        for (int i = s.length() - 1; i >= 0; i--) {
            System.out.print(s.charAt(i));
        }
    }
}

Output:
olleH

Example 4:

public class CountSpacesInString {
    public static void main(String[] args) {
        String s = "This string has spaces";
        int spaces = 0;

        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ' ') {
                spaces++;
            }
        }

        System.out.println("Number of spaces: " + spaces);
    }
}

Output:
Number of spaces: 3

Conclusion

In summary, the Java String length() method is a crucial feature for handling strings, providing a simple way to determine the number of characters (Unicode code units) in a string. Its implementation in the CharSequence interface ensures its availability for all Java strings. Internally, it efficiently retrieves the string’s length from the character array, allowing Java strings to be mutable.

The practical examples presented in this article illustrate its versatility in tasks such as checking for empty strings, reversing strings, or counting character occurrences. Understanding and using the length() method is essential for Java developers, simplifying common string operations.

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

courses

Leave a Reply

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