1. Introduction to array classes
In Java, an array is also a reference type, that is, a class.
Let’s look at an example to 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());
}
Copy the code
Output results:
[I [Ljava.lang.Integer; [[Ljava.lang.Integer; [F [Ljava.lang.Float;
“[” represents a one-dimensional array,” [[” represents a two-dimensional array, and so on. For reference types, “[” is followed by” L “+ the fully qualified name of the Class. For primitive types,” L “+ the fully qualified name of the Class. Just use the corresponding capital letter.
2. Classification of array classes
As you can see from the output of the above example, array classes are classified in the JDK: “[” for one-dimensional primitives, and” [L “for one-dimensional reference arrays, with an extra” L “.
Array classes can be categorized into two classes:
- An array class of primitive type;
- An array class that references a type;
The biggest difference between these two array classes is that they have different ancestor classes.
2.1 Primitive array classes
For primitive types, there is a parent of the primitive array class, which is the Object class.
public static void main(String[] args) { int[] a = new int[3]; Object o = a; // Type conversion succeeds; // Print the array superclass system.out.println ("The superClass of int[] is:"+int[].class.getSuperclass());
}
Copy the code
Running results:
The superClass of int[] is: class java.lang.object
2.2 Array classes of reference type
If A is the ancestor of B, then A[] is also the ancestor of B[]. The same goes for other dimensions. There is no relationship between dimensions.
Look at the following example:
public class Test_3 { public static void main(String[] args) { Children[] childrens = new Children[3]; Ancestor[] ancestors = childrens; Childrens is the descendant of their Ancestor Ancestor[]. System.out.println()"childrens instanceof Ancestor[] : "+ (childrens instanceof Ancestor[])); }} class Ancestor extends extends Ancestor{class Ancestor extends Ancestor{class Ancestor extends Ancestor{class Ancestor extends Ancestor{class Ancestor extends Ancestor{class Ancestor extends Ancestor{ Inheriting from Parent}Copy the code
Running results:
childrens instanceof Ancestor[] : true
In the example above, because the Ancestor is Children, the Ancestor[] is also Children[].
If we call the following code:
System.out.println(Children[].class.getSuperclass());
Copy the code
The output is:
class java.lang.Object
The Children[] array is the parent of the Object class
Ancestor[] is the Ancestor of Children[]
Conflict broke out. Because if the parent class is Object and the Object class is the root class, then there is only one ancestor class and no other ancestor class is possible
Why is that?
Let’s seegetSuperclass()
API description:
public Class<? Super T> getSuperclass() returns the Class representing the superclass of the entity (Class, interface, primitive type, or void) represented by this Class. Null is returned if this Class represents an Object Class, an interface, a primitive type, or void. If this Object represents an array Class, the Class Object representing the Object Class is returned. – Returns the superclass of the class represented by this object.
GetSuperclass returns the class Object of the Object class if the caller is an array.
Reference: http://www.cnblogs.com/jinggod/p/8428062.html
If there are any improper articles, please correct them. You can also pay attention to my wechat public number: Learn Java well and get high quality resources.