In depth analysis of length and length of in Java

  • 2020-05-17 05:24:57
  • OfStack

Before you start this article, consider the following question

How do I get the length of an array without using IDE with auto completion? And, how do I get the length of a string?

This is a question I've asked programmers at different levels, both beginning and intermediate. None of them can answer the question accurately and confidently (if you can answer the question accurately and confidently, then prove that you have a better grasp of this knowledge than most intermediate programmers). Since many IDE now have code completion capabilities, this makes the developer's understanding of many issues superficial.

The correct answer to the above question should look like this:


int[] arr = new int[3];
System.out.println(arr.length);// use length The degree to which you get an array 

String str = "abc";
System.out.println(str.length());// use length() Gets the length of the string 

So the question is, why do arrays have an length property and strings don't? Or, why do strings have them length() Method, and the array doesn't have?

Why does an array have an length property?

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

There are two ways to create an array:

1. Create an array with an array expression.

Create an array by initializing the value.

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

An array is created using an expression that specifies the element type, the dimensions of the array, and the length of an array of at least one dimension.

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


int[][] arr = new int[3][];

You need to provide all the initial values when creating an array using array initialization. The form is to enclose all the initial values in 1 and separate them with {and}.


int[] arr = {1,2,3};

Note:

There might be a question here, since the array size is specified at initialization, so int[][] arr = new int[3][];定 The array does not give the size of the second dimension of the array, so how is the length of arr "specified"?

Actually, the length of arr is 3. In fact, all arrays in Java, no matter how many dimensions, are actually 1-dimensional arrays. For example, arr allocates three Spaces, each containing the address of a 1-dimensional array, so it becomes a "2-dimensional" array. But for arr, its length is 3.


int[][] a=new int[3][];
System.out.println(a.length);//3
int[][] b=new int[3][5];
System.out.println(b.length);//3

Why does Java not define a class Array like String1

Since arrays are also objects, the following code is also legal:


Object obj = new int[10];

The array contains all methods inherited from Object (the inheritance relationship of arrays in Java), except clone(). Why isn't there a class array? There is no Array.java file in Java. One simple explanation is that it is hidden (note: the array in Java is a bit like a primitive data type, it is a built-in type, and there is no actual class to correspond to it). You can think about the question -- what would an Array class look like if it had one? It would still need 1 array to hold all the array elements, right? Therefore, it is not a good idea to define an Array class. The problem is that the chicken comes from the egg, and the egg comes from the egg.

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


int[] arr = new int[3];
System.out.println(arr.getClass());

Output:


class [I

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

Why does String have the length() method?

The data structure behind String is an char array, so there is no need to define an unnecessary property (as this property is already provided in the char value). Unlike C, the array of char in Java is not equal to a string, although the internal mechanism of String is implemented by char array. (note: in the C language, there is no String class and the definition string is usually used char string[6] = "hollis"; The form of

Note: to convert char[] to a string, you can:


char []s = {'a','b','c'};
String string1 = s.toString();
String string2 = new String(s);
String string3 = String.valueOf(s);

conclusion

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


Related articles: