# How to find String length in Java using length method? | Java String length() with Example | length vs length()

In this tutorial, we will discuss how to find the string length in java. We can find string length in java using the length() method of the String class.

## Java String length()

This method returns the length of the string. Remember that the length variable is used in the array whereas the length() method is used in the string. If you try to use the length variable to find the string length then the complier will get a compile-time error. The signature of java string length() is written as shown below.

### Syntax:

The syntax of the string class length() method is given below:

public int length()

### Return Value

This method returns the length of a string in Java.

### Internal Implementation

public int length() {
return value.length;
}

## length vs length()

 length length() Function The length variable in an array returns the length of an array i.e. a number of elements stored in an array. The length() returns the length of a string object i.e. the number of characters stored in an object. Once arrays are initialized, their length cannot be changed, so the length variable can directly be used to get the length of an array. String class uses this method because the length of a string can be modified using the various operations on an object. Examples: length can be used for int[], double[] to know the length of the arrays. Examples: length() can be used for String, StringBuilder, etc. Basically, it is utilized for String class-related Objects to know the length of the String The length variable is used only for an array. The String class internally uses a char[] array that it does not expose to the outside world.

## Example of Java String length() Method

class StringLength{
public static void main(String args[]){
String str1 = "Javastudypoint";
String str2 = "Prashant";
//14 is the length of the str1.
System.out.println("The length of the str1 is: "+str1.length());
//8 is the length of the str2.
System.out.println("The length of the str2 is: "+str2.length());
}
}

Output:

The length of the str1 is: 14
The length of the str2 is: 8

Let’s see an example of what happened if we are using the length variable instead of the length() method in the case of a string. If you do so, then the compiler will get a compile-time error saying cannot find the symbol. Let’s see the sample code on it.

class StringLength{
public static void main(String args[]){
String str1 = "Javastudypoint";
String str2 = "Prashant";
//using length variable instead of length().
System.out.println("The length of the str1 is: "+str1.length);
//using length variable instead of length().
System.out.println("The length of the str2 is: "+str2.length);
}
}

Output: