Length,length (), size () difference in Java
- The Length property in Java is said for an array, for example, if you declare an array, and you want to know how long this array is, this property is used.
- The length () method in Java is for string strings, and the length () method is used if you want to see how long the string is.
- The size () method in Java is said for a generic collection, and if you want to see how many elements the generic has, call this method to view it!
For example:
Demonstrates the use of both methods and a property
Public Static void Main (string[] args) { String []list={"Ma", "Cao", "Yuan"}; String a= "Macaoyuan"; System.out.println ("Length>>> in Java;:" +list.length); System.out.println ("Length in Java () >>>:" +a.length ()); List<Object> array=new ArrayList (); Array.add (a); System.out.println ("size in Java () >>>:" +array.size ()); }
The output value is:
Length>>>:3 Java in Java haslength ()>>>:9size () in Java>>>:1
Length,length (), size () difference in Java