
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - String length() Method
The Java String length() method is, used to retrieve the length of the string. A length is the number of characters presented in the string. The length() method does not accept any parameters, It does not throw any exception while retrieving the length of the string.
A string is an object that holds the sequence of the characters. The java.lang.string class represents the string in Java. The strings are immutable in Java their values cannot be changed after they are created.
Note − The length of the string is nothing but just a count of the characters that are present in the given string, it counts the white spaces as well along with the string characters.
Syntax
Following is the syntax of the Java String length() method −
public int length()
Parameters
It does not accept any parameter.
Return Value
This method returns the length of the sequence of characters represented by this object.
Getting Length of the String Example
If the given string is not null, the length() method returns the length of the string.
In the following program, we are creating a String literal with the value "HelloWorld". Then, using the length() method, we are trying to retrieve the length of the current string.
package com.tutorialspoint; public class Length { public static void main(String[] args) { //create a string literal String str = "HelloWorld"; System.out.println("The given string is: " + str); //using the length method; int len = str.length(); System.out.println("The lenght of the string is: " + len); } }
Output
On executing the above program, it will produce the following result −
The given string is: HelloWorld The length of the string is: 10
Getting Length of Empty String Example
If the given string value is empty, this method returns 0.
In the following example, we create an object of the string class with an empty value. Using the length() method, we are trying to get the string length of the current string.
package com.tutorialspoint; public class Length { public static void main(String[] args) { //create an object of the string class String str = "";// empty string System.out.println("The given string is: " + str); //using the length method; int len = str.length(); System.out.println("The lenght of the string is: " + len); } }
Output
Following is the output of the above program −
The given string is an empty. The length of the string is: 0
Checking Larger String using Length of the Strings Example
Using the length() method, we can find the larger string based on its length.
In this program, we are instantiating the string class with the value "java". Then, we create a string literal with the value "point". Using the length() method, we are trying to find the larger string based on string length.
package com.tutorialspoint; public class Length { public static void main(String[] args) { //instantiate the string class String str = new String("java"); System.out.println("The first string is: " + str); String str2 = "point"; System.out.println("The second string is: " + str2); //using the length method; int len1 = str.length(); int len2 = str2.length(); System.out.println("The length of the strings is: " + len1 + " and " + len2) ; if(len1 > len2) { System.out.println("The first string is larger than the second string"); } else { System.out.println("The first string is smaller than the second string"); } } }
Output
The above program, produces the following output −
The first string is: java The second string is: point The length of the strings is: 4 and 5 The first string is smaller than the second string
Getting Exception while checking Length of the Null String Example
If the given string value is null, the length() method throws the NullPointerException.
In the following program, we declare a string and initialize it with the null value. Using the length() method, we are trying to retrieve the string length. Since the given string value is null, this method throws an exception.
public class Length { public static void main(String[] args) { try { //declare a string String str; //initialize it with null value str = null; System.out.println("The given string is: " + str); //using the length() method System.out.println("The length of the string is: " + str.length()); } catch(NullPointerException e) { e.printStackTrace(); System.out.println("Exception: " + e); } } }
Output
After executing the above program, it will produce the following output −
The given string is: null java.lang.NullPointerException: Cannot invoke "String.length()" because "str" is null at com.tutorialspoint.String.Length.main(Length.java:11) Exception: java.lang.NullPointerException: Cannot invoke "String.length()" because "str" is null