Length and length in Java ()

Source: Internet
Author: User

First of all, can you answer the following questions quickly?

How to get the length of an array without the help of the IDE's code auto-completion? How do I get the length of a string?


I have asked different levels of developers about this issue. They cannot answer the question correctly or confidently. When the IDE provides convenient code completion, it also brings problems that are understood to remain on the surface. Here, I'll explain some of the key concepts about arrays.

Answer:

int[] arr = new INT[3]; System.out.println (arr.length);//length for array String str = "ABC"; System.out.println (Str.length ());//length () for string


Why does an array have the length property and the string does not? Why does the string have the length () method and the array does not?

First question: Why does the array have the length property?
First, an array is a container object that stores a fixed number of values of a single type. When an array is created, its length can no longer be changed. The length of the array can be regarded as a variable of immutable instance length. Therefore, the length can be seen as an array of explicit properties.

You can create an array in two ways: 1) An array initializer that creates an array expression and 2). When it is created, the size has been specified.

In the example above we used the Create array expression. It specifies the element type, and the dimensions and arrays of the nested arrays are at least one-dimensional arrays in length.

This declaration is also legal, as long as the first dimension of the array is specified.

Uses the array initializer to create an array and specifies all the initialization values that the array contains, using {} to contain all the values, separated by commas between each value.

For example:

Int[] arr = {A-i};

a secondQuestion: Why not have a class "Array" similar to "String"

Because the array is an object, the following code is also valid:

Object obj = new INT[10];


An array contains all the members that inherit from the class object (excluding clones). Why don't you define an array class? We can't find Array.java this file. One rough answer is that it hides and doesn't let us find out. You can think of it this way--what would it look like if there was an array such a class? It still needs an array to store the array data, right? Therefore, defining such a class is of little significance.

In fact, we can get the class of an array using the following code:

int[] arr = new INT[3]; System.out.println (Arr.getclass ());


Output :

class [I


"Class [I") represents the run-time type signature of the class object for an array of element type int.


The third question: Why the string has the length () method
The backup data structure of a string is a character array. You do not need to define a field length that is not required for each program. Unlike C, a character array in Java is not equivalent to a string.

Length and length 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.