+ 1
What is the difference between length and length() ?
I wanted to know what is the difference between the methods, length and length()
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
+ 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