Java length,length (), size () Detailed and Difference _java

Source: Internet
Author: User

Length,length () in Java, size () difference

Length property: Used to get the length of the array.

eg

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

/**
* Array with the Length property to get the lengths/

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

Length () Method: Used to get the length of the string.

String str = "Hello world Java";
the/** * string uses the length () method to obtain
the lengths */

int lenstr = Str.length ();//Here lenstr=16
System.out.println ("str Length (): "+lenstr";

Size () Method: lets you get the number of elements in a generic collection.

eg

/**
* Collection is the largest parent interface for single value saving in the entire class set, so it is necessary to instantiate it with a specific subclass * *
collection<string> col = new ArrayList <String> ();
Col.add ("Hello");
Col.add ("World");
Col.add ("Java");
/**
* Class set framework using the size () method to take 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 for this site!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.