Understanding of Java Arrays

Source: Internet
Author: User

Yesterday, I was asked if the Java array is a class. The first response to this question is certainly yes, whether from the way Java arrays are defined or from object-oriented understanding. But think carefully, but it is not so.

According to the usual understanding, Java is a pure object-oriented language, so everything is object, for all types are objects or object variants. However, arrays and other data types are not generic objects. For example, the General object definition is this:

Myclass C1 = new Myclass ();
And the array definition is this:

int a[] = new INT[50];
This will see a problem when the array is declared without calling the constructor. The definition of an object in Java needs to call the constructor, and the array does not, which indicates that the array is not a class, or is not a simple class. A simple program has been written to test the correlation functions of arrays in Java.

You can also print examples of common classes and arrays of classes to see the results:

public class Test {public
	static void Main (string[] args)
	{
		Test t1 = new test ();
		Test[] Arr=new test[9];
		Class clazz = Test[].class;
		System.out.println (clazz);
		Class Clasz = Test.class;
		System.out.println (Clasz);
		
	}


The result:



You can see that the array class is not the same as the normal class.

But the array class has its own parent class, which prints the parent class of the array class.

System.out.println (Clazz.getsuperclass ());


So the array class can be counted as a special class.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.