Differences between length, length() and size() in Java
length attribute: used to get the array length.
eg:
int ar[] = new int{1,2,3}/*** The array uses the length attribute to get the length*/int lenAr = ar.length;//here lenAr=3System.out.println("Arr length:"+lenAr);length() method: used to get the length of the string.
String str = "Hello World Java";/*** Use length() method to get the length */int lenStr = str.length();//here lenStr=16System.out.println("Str length():"+lenStr);size() method: used to get how many elements there are in a generic collection.
eg:
/*** Collection is the largest parent interface saved by a single value in the entire class set, so when using it, you need to instantiate it with a specific subclass */Collection<String> col = new ArrayList<String>();col.add("Hello");col.add("World");col.add("Java");/*** The class set framework uses the size() method to get the number of elements */int sizeCol = col.size();//Here sizeCol=3System.out.println("Col size:"+sizeCol);Thank you for reading, I hope it can help you. Thank you for your support for this site!