JAVA-depth understanding of arrays are objects

The concept of an array

Array refers to the same types of finite elements of a sequence. Array occupy contiguous space in memory.

In C points to the first element of the array inside the array corresponds to a pointer. But in JAVA, the internal array has many methods and properties, then this reference is only a simple data type "pointer" it? (JAVA and no pointer concept, say just for the convenience of the reader understand)

//创建数组
int [] arr=new int[10];
//创建对象
class obj=new class();
  • Regulations require a reference type variable. Arr inside pass only integer type, obj passed only class type
  • We need to apply for new space in memory
  • You will need to pass parameters. Array [] value between the incoming object instantiation parameters passed in () The constructor.

Such contrast surface, people feel as if the array may itself be a class, and the new int [] process is actually an instance of a process object, a different class constructor may be used is (), and the array using Yes[].

Further

new Object[0].getClass().getSuperClass() 

Get is Object.class, OH My god ! ! ! His parent is actually Object, Array is a class so you can not see? I was able to inherit! ! !

But the array did not .class file, we know there will be a corresponding class after class file is compiled, such as the String class to have .class files. But according to our parent array is Object, we can already consider quite sure java is definitely the array as a class to handle.

In order to avoid doubt my readers out of nothing, the cover of darkness ...

Then a judge instanceof array JAVA process is not treated the same class have that taste (if students do not come into contact with instanof, you just need to know that it can determine whether an object is an instance of the class or object to its subclasses).

		int [] a=new int[10];
		if(a instanceof int[]) {
			System.out.println("a is int[]的子类");
		}

Inside the if statement executed successfully, indicating a pointing object actually belongs int [], and indeed it would appear java array as a class to handle.

what? Do not believe?

		int[] a = new int[10];
        System.out.println(a.getClass().getName());

And it returned to the class name, printed [I.

And a different class name of the returned array is not the same, as follows:

	short[]                   [S
	byte[]                    [B
	boolean[]                 [Z
	int[]                     [I
	int[][]                   [[I
	double[]                  [D
	double[][]                [[D

The results of observation returns the class name [, and the first data type of capital, [the number determined by the dimension of the array.

An array is an example of how
we need to know the class constructor in the object instantiation process, since the array is in accordance with the class to handle, then certainly need the constructor friends.

	Class arrclass = arr[].class;
	System.out.println(arrclass.getConstructors().length);

Print is 0, illustrate the internal array really is no constructor property or method. Array objects do not like a class is instantiated come, but by the JVM created directly, the direct parent of this object is created Object, each array will have a Class object , so I can show you an array of the above properties, as an object.

Published 10 original articles · won praise 1 · views 815

Guess you like

Origin blog.csdn.net/ShangDiYeGuJi/article/details/104927775