Java is a programming language that develops applications using certain packages, methods, and functions. These methods, functions, etc are utilized according to the need. Among these methods, one of the frequently used methods is the length() method. Apart from the length() method there also exists a length variable/property in Java. Both of them keep the same name; however, each of them serves different functionality.
This article will first discuss the details of the length variable and the length() method and then proceed to highlight the differences between them in Java.
What is a Length Variable in Java?
The length variable/property in Java depicts the size of the array in Java. This variable in Java returns the total number of elements available in an array. For instance, if an array is declared with a total of five elements, implementing the length variable on that particular array will return the size of the array as “5”.
What is the length() Method in Java?
The total number of characters available in the String is calculated using the length() method in Java. It counts all the whitespaces when determining the length. For instance, the string “Code” consists of “4” characters, so implementing Java’s length() method on the stated string will retrieve “4”.
Now we will proceed with the main topic which is the differences between the length variable and the length() method in Java.
Difference Between length Variable and length() Method in Java
The differences between the length variable and the length() method in Java are listed below:
- The key difference is that the length is a “variable” and the “length()” in Java is a “method”.
- The length variable determines the size or length of the array whereas the length() method in Java depicts the total number of characters declared in the string.
- Another difference is that the length variable can be implemented on the arrays only, not the strings whereas the length() method can be implemented on the strings.
- The length property is applied to int[ ], double[ ], and String[ ] to calculate the size of an array whereas the length() method utilizes the StringBuilder, String to find the length of the string.
Below is the implementation of code examples to depict how the length variable and length() method works in Java.
Example 1: Using the length Variable on String Array
The code example below utilizes the length variable to find the length of the declared array in Java.
class LengthExample1{// main() method
public static void main(String[] args)
{
String s[]={"Coding", "in", "Java"};
System.out.println("The Length of the Array is "+ s.length);
}
}
In the above code:
- The Class is simply declared as “LengthExample1”.
- The main() method of Java consists of an array of strings declared as “s[ ]”.
- The println() method uses the length variable to compute the total length of the given array.
Output
The output below depicts that the length of the array is printed as “3” using the length variable.
Example 2: Using the length Variable on Integer Array
The code example below depicts the implementation of the length variable on an integer array.
// Declare the classclass LengthExample2{ // main() method
public static void main(String[] args)
{
int a[]= new int[20];
System.out.println("The Length of the Array is "+ a.length);
}
}
In the above code, the int array is declared as “a[]” with an integer value of “20”, and the size is returned as the output using the println() method.
Output
The length of the array is returned as “20” using the length variable of Java.
Example 3: Using the length Variable on a String
In the code below, the length variable is used to print the length of the string declared as “Coding”.
// Declare class to understand the errorclass LengthError1{
public static void main(String[] args)
{
String s = "Coding";
System.out.println("The Length of the String is "+ s.length);
}
}
It is to keep in mind that the length variable is applicable on arrays only not on strings in Java.
Output
The output below shows that the error is printed as “variable s of type String” which means that the length variable can not be implemented on the strings in Java.
Example 4: Using the length() Method of Java on Strings
The code below implements the length() method to calculate the length of the string in Java.
// Declare a class in Javaclass LengthExample4{
public static void main(String[] args)
{
String s = "Coding";
System.out.println("The Length of the String is "+ s.length());
}
}
In the above Java code:
- The class is declared as “LengthExample3”.
- In the next step, the string is declared as “coding” to implement the length() method.
- The s.length() method fetches the length of the declared strings and the println() method prints the result.
Output
In the output below, the length of the string “Coding” is printed as “6” using the length() method.
Example 5: Using the length() Method in Java With Specific Index
In the code below, the index or the position of the strings in the array is declared.
// Declare the class for Example 5class LengthExample5{
public static void main(String[] args)
{
String []s = {"Coding", "in", "Java"};
System.out.println("The Length of the string 'Java' is "+ s[2].length());
}
}
In the above code block,
- The array comprises the string “Coding” at index “0”, the string “in” at index “1” and the string “Java” at index “2”.
- The index is declared as “2” which will calculate the length of the string “Java” at the second index using the length() method.
Output
In the output below, the length of the string “Java” at index “2” is printed as “4” using the length() method of Java.
Example 6: Using the length() Method on an Array
In the code below, the length() method of Java is utilized to print the length of the array whereas it only deals with the strings in Java.
// Declare class to understand the errorclass LengthError2{
public static void main(String[] args)
{
String []s = {"Coding", "in", "Java"};
System.out.println("The Length of the string 'Java' is "+ s.length());
}
}
Output
The output below shows that an error occurs when the length() method is used to calculate the length of the array.
The output concludes that even if we implement the length() function on a string array, still it’s not gonna work.
This ends the implementation of the length variable and length() method in Java along with the differences.
Conclusion
The main difference between the length variable and the length() method is that the length variable/property can be implemented on the arrays only not the strings whereas the length() method can be implemented on the strings only not the arrays. This blog post effectively explains the length variable and the length() method in Java.