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

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/sk880609/article/details/7524006

1, the length property in Java is said for the array, for example, you declare an array, you want to know the length of this array is used to this property.

2. The length () method in Java is for string strings, and if you want to see the length of the string, use Length ().

3. The size () method in Java is said for a generic collection, and if you want to see how many elements of the generic, call this method to view it!

Examples are as follows:

Importjava.util.ArrayList;Importjava.util.Collection; Public classTest { Public Static voidMain (string[] args) {int[] array = {0, 1, 2, 3, 4, 5, 6, 7}; String Str= "Hello world!"; Collection<String> C =NewArraylist<string>(); C.add ("Hello"); C.add ("World"); C.add ("Java"); System.out.println ("Length:" +array.length); System.out.println ("Length ():" +str.length ()); System.out.println ("Size ():" +c.size ()); }}

Output Result:

Length:8Length ():size ():3

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

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.