In Java length length of size of explain the differences

  • 2020-05-17 05:33:52
  • OfStack

length,length(),size()

length property: used to get the array length.

eg:


int ar[] = new int{1,2,3}

/**
*  Array with length Property gets the length 
*/

int lenAr = ar.length;// here lenAr=3
System.out.println("Arr length:"+lenAr);

length() method: used to get the length of a string.


String str = "Hello World Java";

/**
*  String to use length() Method to get the length 
*/

int lenStr = str.length();// here lenStr=16
System.out.println("Str length():"+lenStr);

size() method: used to get how many elements there are in a generic collection.

eg:


/**
* Collection Is the largest single-value saved parent interface in the entire class set, so you need to instantiate it with a concrete subclass 
*/
Collection<String> col = new ArrayList<String>();
col.add("Hello");
col.add("World");
col.add("Java");
/**
*  Class set framework size() Method takes the number of elements 
*/
int sizeCol = col.size();// here sizeCol=3
System.out.println("Col size:"+sizeCol);

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: