1. Introduction to Array classes
?? In Java, an array is also a reference type, which is a class.
Let's take a look at an example and understand the array class:
Public Static void Main(string[] args) {Class c =int[].class; Class cIn = integer[].class; Class ccin = integer[][].class; System. out.println(c.GetName()); System. out.println(CIn.)GetName()); System. out.println(Ccin.GetName()); Class f =float[].class; Class Flo = float[].class; System. out.println(F.GetName()); System. out.println(Flo.GetName());}
Results of the output:
[I
[Ljava.lang.Integer;
[[Ljava.lang.Integer;
[F
[Ljava.lang.Float;
?? The above output is the output through the class toString() method of the content, you can see the law, "[" represents a one-dimensional array, "[[" means a two-dimensional array, and so on. Also, if it is a reference type, the "[" followed by the fully qualified name of the "L" + class. In the case of a basic type, it is only associated with the uppercase letter.
2. Classification of array classes
As can be seen from the output of the above example, the array class is categorized in the JDK: for a one-dimensional array of primitives, output "[", and for a one-dimensional reference array output "[L", a "L".
Array classes can be categorized into two categories:
- An array class of basic types;
- An array class of reference types;
The biggest difference between the two array classes is that their ancestor classes are different.
2.1 Primitive types of array classes
?? For a base type, the parent class of the primitive type array class is the object class.
publicstaticvoidmain(String[] args) { intnewint[3]; Object o = a;//编译通过,类型转换成功; //打印一下数组的超类 System.out.println("int[]的superClass是: "+int[].class.getSuperclass()); }
Operation Result:
Int[] 's superclass is: Class Java.lang.Object
2.2 Array classes of reference types
?? For an array class of reference types, all of its ancestor classes include the following classes in addition Object to the classes:
If a is the ancestor class of B, a[] is also the ancestor class of b[], the other dimensions are so and so on, and there is no relationship between the different dimensions.
Look at the following example:
Public classTest_3 { Public Static void Main(string[] args) {children[] childrens =Newchildren[3]; ancestor[] ancestors = childrens;//Compile pass, type on success //Judge whether childrens is a Ancestor or an instance of its subclass, further proving Children[] is a descendant of ancestor[]System. out.println("Childrens instanceof ancestor[]:"+ (ChildrensinstanceofAncestor[]));} }classancestor{//Ancestor class}classParentextendsancestor{//Parent class, inherited from ancestor}classChildrenextendsparent{//Subclass, inherited from parent}
Operation Result:
Childrens instanceof ancestor[]: True
?? In the example above, because Ancestor it is Children the ancestor class, so is Ancestor[] also Children[] the ancestor class.
If this time we call execute the following code:
System.out.println(Children[].class.getSuperclass());
The result of the output is:
Class Java.lang.Object
Well, why is the parent class of the reference type array Children[] the object class, then with the above mentioned
Ancestor[]is Children[] the Ancestor class
The conflict. Because if the parent class is the Object,object class, then there is only one ancestor class, and there is no other ancestor class
What is this for?
Let's look at getSuperclass() the API description:
Public class<? Super T> Getsuperclass ()
Returns the class of the superclass representing the entity (class, interface, base type, or void) represented by this class. Returns NULL if this class represents an Object class, an interface, a base type, or void. If this object represents an array class, a class object that represents the object class is returned.
Return:
The superclass of the class represented by this object.
Originally, getSuperclass for the caller is an object of the array class, then the Object class object is returned directly.
Java Basics (10) Array type