In depth analysis of length and length () in Java

Before starting this article, consider the following question

How to get the length of an array without using any ide with automatic completion function? And how to get the length of a string?

I've asked programmers at different levels, including junior and intermediate levels. They can't answer this question accurately and confidently (if you can answer this question accurately and confidently, it proves that you master this knowledge better than most intermediate programmers). Because many ides now have code completion function, developers understand a lot of questions superficially.

The correct answer to the above question should be as follows:

So the question is, why does the array have the length attribute but the string does not? Or, why does a string have a length () method and an array not?

Why does an array have a length attribute?

First, an array is a container object that contains a fixed number of values of the same type. Once the array is created, its length is fixed. The length of the array can be used as the length of the final instance variable. Therefore, length can be regarded as an array attribute.

There are two ways to create an array:

1. Create an array through an array expression.

2. Create an array by initializing values.

Either way, once the array is created, its size is fixed.

The method of creating an array using an expression is as follows, which indicates the element type, the dimension of the array, and the length of the array of at least one dimension.

This declaration method is qualified because it specifies the length of a dimension (the type of the array is int, the dimension is 2, and the length of the first dimension is 3)

When using array initialization to create an array, you need to provide all initial values. The form is to use {and} to enclose all initial values and separate them with.

Note:

There may be a question here. Since the array size is specified during initialization, int [] [] arr = New Int [3] []; The defined array does not give the size of the second dimension of the array, so how is the length of this arr "specified"?

In fact, the length of arr is 3. In fact, all arrays in Java, no matter how many dimensions, are actually one-dimensional arrays. For example, arr allocates three spaces, and each space stores the address of a one-dimensional array, which becomes a "two-dimensional" array. But for arr, its length is 3.

Why is there no array class like string defined in Java

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

The array contains all methods inherited from object (the inheritance relationship of arrays in Java), except clone (). Why not have an array class? There is no array in Java Java file. A simple explanation is that it is hidden (Note: the array in Java is a bit similar to the basic data type. It is a built-in type, and there is no actual class corresponding to it). You can think about such a question - if there is an array class, what will it look like? It will still need an array to store all array elements, right? Therefore, defining an array class is not a good idea. (translator's note: it may be a little around here, and the reason is a little similar to the problem of chicken laying eggs and chicken laying eggs. The metaphor may not be very appropriate. Please understand it by yourself.)

In fact, we can get the class definition of the array through the following code:

Output:

"Class [I" represents the runtime type signature of the class object whose member type is an array of int

Why does string have a length () method?

The data structure behind string is a char array, Therefore, it is not necessary to define an unnecessary attribute (because the attribute is already provided in the char value). Unlike C, char array in Java is not equal to string, although the internal mechanism of string is implemented by char array. (Note: in C language, there is no string class, and the definition of string usually uses the form of char string [6] = "Hollis")

Note: to convert char [] into a string, there are the following methods:

summary

The above is the whole content of this article. I hope the content of this article can help you learn or use Java. If you have any questions, you can leave a message.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>