- What is the length Property?
- What is the length() Method?
- What is the Difference Between length and length() Methods?
What is the length Property?
In Java, the “length” property is the built-in property that returns the array’s element count. It is not a method, but a public instance variable that is declared in the Array class. The length property can be used with any type of array, including arrays of primitive types, arrays of objects, and multi-dimensional arrays.
Example: 1
Here is an example that demonstrates the use of the length property:
In the above example,
- The “numbers” array contains 5 elements.
- The “length” property is to determine how many elements are in the array.
Output
The value of “arrayLength” is 5, which is the number of elements in the numbers array.
Example: 2
Here is another example that uses a multidimensional array:
The explanation is below here,
- The “matrix” array is a two-dimensional array that contains 3 rows and 3 columns.
- The “length” property is utilized to retrieve the row numbers in the matrix array.
- Also, the length property is also used to retrieve the number of columns in the first row of the matrix array.
Output
The output of this program is “The matrix has 3 rows and 3 columns” which is displayed in the terminal.
What is the length() Method?
A length() method is utilized to determine a string’s length. It is a method, not a property, and is called using parentheses after the string variable or literal.
Example: 1
Here is an example that demonstrates the use of the length() method:
In this example,
- The “greeting” string contains 13 characters, including the space and punctuation.
- The “length()” method is used to retrieve the characters in the greeting string.
Output
The value of stringLength is 13, that is the character length in the greeting string.
Example: 2
Here is another example that uses the length() method in a loop:
The description of the above code is mentioned below:
- The “length()” method is used in a loop to iterate over each character in the message string.
- The “charAt()” method is used to retrieve the character at the current index, and that character is printed to the console.
Output
The output shows that each character in the message string has been printed to the console.
What is the Difference Between length and length() Methods?
In Java, length, and length() are used to get the size of an array and the length of a string, respectively. However, there are some key differences between these two concepts:
- length is a public instance variable of an array that is used to get the number of elements in the array. It is not a method and is directly accessed via the dot (.) operator. The length() determines a string’s length. It is accessed using parentheses after the variable name.
- length can only be used with arrays, whereas length() can only be used with strings.
- length gives an integer value that represents the array’s element count. An integer value representing the number of characters in the string is returned by the length() function.
- length is a final variable that cannot be modified, whereas length() is a method that can be called on any string object.
- length is a property of the array object, so it is accessed using dot notation, while length() is a method of the String class, so it is accessed using method invocation syntax.
Conclusion
In Java, the “length” property is the built-in property that returns the array’s element count. It can be used with any type of array and is accessed using the dot operator (.) after the array name. On the other hand, the “length()” method is utilized to return the string length. It is a method, not a property, and is called using parentheses after the string variable or literal.