All the above answers are proper. The important thing to observe is arrays have length attribute but not length method. Whenever you use strings and arrays in java the three basic models you might face are:
- String s=new String("vidyasagar");
System.out.println(s.length()); // In this case we are using only
String. No length attribute for Strings. we have to use length()
method.
- int[] s=new int[10]; System.out.println(s.length); //here we
use length attribute of arrays.
- String[] s=new String[10];
System.out.println(s.length); // Here even though data type is
String, it's not a single String. s is a reference for array of
Strings. So we use length attribute of arrays to express how
many strings can fit in that array.