Java String length Method

The Java length Method is to find and return the length of the User specified string. The length is equal to the number of Unicode units in a string. In this article, we will show how to find the length of a String with an example. The basic syntax of the String length is as shown below.

public int length() // It will return the integer Value as Output

//In order to use in program
  • String_Object: Please specify the valid Object to find its length.

Java String length method example

The length method returns the length of the user-specified string. In this program, we are going to declare a few string objects and then find the count of those characters.

package StringFunctions;

public class StringLength {
	public static void main(String[] args) {
		String str = "Hello";
		int str1 = str.length();
		String str2 = "Tutorial GateWay";
		String str3 = "Free Java Tutorial at Tutorial GateWay";
		String str4 = "Java Programming Language";
		System.out.println("The Length of a String Str = " + str1);
		System.out.println("The Length of a String Str2 = " + str2.length());
		System.out.println("The Length of a String Str3 = " + str3.length());
		System.out.println("The Length of a String Str4 = " + str4.length());
String length Example 1

In this example, First, we declared the String variable and assigned a value using the first statement.

In the next statement, we used the len function that will find the total characters of the above-specified string. Here we are assigning the output to integer values because it returns an integer value.

int str1 = str.length();

Next, we declared three more strings using the following Java statements. The following System.out.println statements will find the total characters and then print the output.