• array.length: length is a final variable applicable for arrays. With the help of length variable, we will obtain the dimensions of the array.
  • string.length(): length() method is a final variable which is applicable for string objects. length() method returns the amount of characters presents within the string.

length vs length()

The length variable is applicable to array but not for string objects whereas the length () method is applicable for string objects but not for arrays.

Examples:

[pastacode lang=”java” manual=”%2F%2F%20length%20can%20be%20used%20for%20int%5B%5D%2C%20double%5B%5D%2C%20String%5B%5D%20%0A%2F%2F%20to%20know%20the%20length%20of%20the%20arrays.%0A%0A%2F%2F%20length()%20can%20be%20used%20for%20String%2C%20StringBuilder%2C%20etc%20%0A%2F%2F%20String%20class%20related%20Objects%20to%20know%20the%20length%20of%20the%20String%0A” message=”” highlight=”” provider=”manual”/]

To directly accesses a field member of array we will use .length; whereas .length() invokes a way to access a field member.

Example:

[pastacode lang=”java” manual=”%2F%2F%20Java%20program%20to%20illustrate%20the%20%0A%2F%2F%20concept%20of%20length%20%0A%2F%2F%20and%20length()%20%0Apublic%20class%20Test%20%7B%20%0Apublic%20static%20void%20main(String%5B%5D%20args)%20%0A%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Here%20array%20is%20the%20array%20name%20of%20int%20type%20%0A%20%20%20%20%20%20%20%20int%5B%5D%20array%20%3D%20new%20int%5B4%5D%3B%20%0A%20%20%20%20%20%20%20%20System.out.println(%22The%20size%20of%20the%20array%20is%20%22%20%2B%20array.length)%3B%20%0A%20%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Here%20str%20is%20a%20string%20object%20%0A%20%20%20%20%20%20%20%20String%20str%20%3D%20%22wikitechy%22%3B%20%0A%20%20%20%20%20%20%20%20System.out.println(%22The%20size%20of%20the%20String%20is%20%22%20%2B%20str.length())%3B%20%0A%20%20%20%20%7D%20%0A%7D” message=”” highlight=”” provider=”manual”/]

Output

[pastacode lang=”bash” manual=”The%20size%20of%20the%20array%20is%204%0AThe%20size%20of%20the%20String%20is%209%0A” message=”” highlight=”” provider=”manual”/]

Let’s have a look on the output of the following programs?

1.What will be the output of following program?

[pastacode lang=”java” manual=”public%20class%20Test%20%7B%20%0Apublic%20static%20void%20main(String%5B%5D%20args)%20%0A%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Here%20str%20is%20the%20array%20name%20of%20String%20type.%20%0A%20%20%20%20%20%20%20%20String%5B%5D%20str%20%3D%20%7B%20%22wikitechy%22%2C%20%22kaashiv%22%20%7D%3B%20%0A%20%20%20%20%20%20%20%20System.out.println(str.length)%3B%20%0A%20%20%20%20%7D%20%0A%7D%0A” message=”” highlight=”” provider=”manual”/]

Output

[pastacode lang=”bash” manual=”2″ message=”” highlight=”” provider=”manual”/]

Explanation: Here the str is an array of type string and that’s why str.length is used to find its length.

2.What will be the output of following program?

[pastacode lang=”java” manual=”public%20class%20Test%20%7B%20%0Apublic%20static%20void%20main(String%5B%5D%20args)%20%0A%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20String%5B%5D%20str%20%3D%20%7B%20%22wikitechy%22%2C%20%E2%80%9Ckaashiv%E2%80%9D%20%7D%3B%20%0A%20%20%20%20%20%20%20%20System.out.println(str%5B0%5D.length)%3B%20%0A%20%20%20%20%7D%20%0A%7D%0A” message=”” highlight=”” provider=”manual”/]

Output

[pastacode lang=”bash” manual=”error%3A%20cannot%20find%20symbol%0Asymbol%3A%20method%20length()%0Alocation%3A%20variable%20str%20of%20type%20String%5B%5D%0A” message=”” highlight=”” provider=”manual”/]

Explanation: Here the str is an array of type string and that’s why str.length() can’t be wont to find its length.

3.What will be the output of following program?

[pastacode lang=”java” manual=”public%20class%20Test%20%7B%20%0Apublic%20static%20void%20main(String%5B%5D%20args)%20%0A%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20String%5B%5D%20str%20%3D%20%7B%20%22wikitechy%22%2C%20%E2%80%9Ckaashiv%E2%80%9D%20%7D%3B%20%0A%20%20%20%20%20%20%20%20System.out.println(str%5B0%5D.length())%3B%20%0A%20%20%20%20%7D%20%0A%7D%0A” message=”” highlight=”” provider=”manual”/]

Output

[pastacode lang=”bash” manual=”9″ message=”” highlight=”” provider=”manual”/]

Explanation: Here str[0] pointing to String i.e. GEEKS and thus is accessed using .length()

Categorized in: