There are following difference between variable length vs length() method in java.
length :-
- It is a final variable, applicable only for Arrays.
- It is represent the size of Arrays.
public class LengthArray {
public static void main(String[] args) {
int a[] = { 12, 13, 14, 15 };
System.out.println("total number of array data :- " + a.length);
}
}
Output :- total number of array data :- 4
length() :-
- It is a final method, applicable only for string object.
- This method is represent number of character in the string.
public class LengthString {
public static void main(String[] args) {
String e = "welcome";
System.out.println("total number of character in the string :- " + e.length());
}
}
Output :-
total number of character in the string :- 7