+ 1

What is the difference between length and length() ?

I wanted to know what is the difference between the methods, length and length()

28th Dec 2017, 5:09 PM
Karan Kumar
Karan Kumar - avatar
2 Answers
+ 9
length:Ā It is a final variable and only applicable for array. It represent size of array. Example: int[] a=new int[10]; System.out.println(a.length); // 10 System.out.println(a.length()); // compile time error length():Ā It is the final method applicable only for String objects. It represents the number of characters present in the String. Example: String s="Java"; System.out.println(s.length()); // 4 System.out.println(s.length); // compile time error
28th Dec 2017, 5:26 PM
LukArToDo
LukArToDo - avatar
+ 6
length is used in a array length() is used in string ex String[] ar = { "hello" , "world" }; System.out.println(ar.length);/// 2 System.out.println(ar[0].length()); /// prints the lengths of hello
28th Dec 2017, 5:21 PM
MR Programmer
MR Programmer - avatar