Consider the following questions before starting this article
How to get the length of an array without using any IDE with autocomplete function? And, how to get the length of a string?
I have asked programmers of different levels, including both junior and intermediate levels. None of them can answer this question accurately and confidently (if you can answer this question accurately and confidently, it proves that you are better at this knowledge than most intermediate programmers). Since many IDEs now have code completion functions, developers can understand many issues very superficially.
The correct answer to the above question should be as follows:
int[] arr = new int[3];System.out.println(arr.length);//Use length to get the degree of array String str = "abc";System.out.println(str.length());//Use length() to get the length of the string
So the question is, why does the array have a length attribute but the string does not? Or, why does strings have length() method, but arrays don't?
Why does 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, the length can be considered as an attribute of an array.
There are two ways to create an array:
1. Create an array through array expressions.
2. Create an array by initializing the 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 specifies the element type, the dimension of the array, and the length of the array of at least one dimension.
This declaration method meets the requirements because it specifies the length of a dimension (the array has type int, dimension is 2, and the first dimension is 3)
int[][] arr = new int[3][];
When creating an array using array initialization, you need to provide all the initial values. The form is to use { and } to enclose all initial values together and separate them.
int[] arr = {1,2,3};Note:
There may be a question here. Since the array size is specified when initialized, then the array int[][] arr = new int[3][];定does not give the size of the second dimension of the array. So how is the length of this arr "defined"?
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, 3 spaces are allocated, each space stores the address of a one-dimensional array, which becomes a "two-dimensional" array. But for arr, his length is 3.
int[][] a=new int[3][];System.out.println(a.length);//3int[][] b=new int[3][5];System.out.println(b.length);//3
Why is there no Array class like String defined in Java
Because arrays are also objects, the following code is also legal:
Object obj = new int[10];
The array contains all methods inherited from Object (inheritance relationship of arrays in Java), except clone(). Why is there no array class? There is no Array.java file in Java. A simple explanation is that it is hidden (Note: Arrays in Java are a bit similar to basic data types, they are a built-in type, and there is no actual class corresponding to it). You can think about this question - if there is an Array class, what would it look like? It will still need an array to store all the array elements, right? Therefore, it is not a good idea to define an Array class. (Translator's note: Here may be a bit confusing, the reason is a bit similar to: the problem of chickens being born with eggs and chickens, and the metaphor may not be very appropriate, please understand it by yourself)
In fact, we can obtain the class definition of the array, through the following code:
int[] arr = new int[3];System.out.println(arr.getClass());
Output:
class [I
"class [I" represents the signature of the runtime type 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, so there is no need to define an unnecessary property (because the 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 arrays. (Note: In C language, there is no String class, and defining strings usually uses the form of char string[6] = "hollis"; )
Note: There are the following ways to convert char[] into a string:
char []s = {'a','b','c'};String string1 = s.toString();String string2 = new String(s);String string3 = String.valueOf(s);Summarize
The above is the entire content of this article. I hope the content of this article will be helpful to everyone in learning or using Java. If you have any questions, you can leave a message to communicate.