Java

What is the Difference Between Length and Length() Method in Java?

In Java, length, and length() are used to get the size of an array and the length of a string, respectively. The “length” is a property of an array that indicates how many elements are included in the array. It is not a method and is accessed directly via the dot (.) operator. While the “length()” is a method of the String class that returns the string length. It is accessed using parentheses after the variable name.

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:

class marks{

public static void main(String[] args) {

int[] numbers = {1, 2, 3, 4, 5};

int arrayLength = numbers.length;

    System.out.println("The length of the numbers array is: " + arrayLength);

  }

}

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:

class marks {

public static void main(String[] args) {

int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

int numRows = matrix.length;

int numCols = matrix[0].length;

    System.out.println("The matrix has " + numRows + " rows and " + numCols + " columns.");

  }

}

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:

class marks{

public static void main(String[] args) {

   String greeting = "Hello, World!";

int stringLength = greeting.length();

   System.out.println("The length of the greeting string is: " + stringLength);

  }

}

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:

class marks{

public static void main(String[] args) {

  String message = "Java";

for (int i = 0; i < message.length(); i++) {

char currentChar = message.charAt(i);

     System.out.println("Character at index " + i + " is: " + currentChar);

}

  }

}

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.

About the author

Syed Minhal Abbas

I hold a master's degree in computer science and work as an academic researcher. I am eager to read about new technologies and share them with the rest of the world.