Why can't Java create a generic array? __java

Source: Internet
Author: User

Looking at Java Core Technology Volume I recently, I saw one of the pages saying that you can't create a generic array. The situation is as follows:

public class Pair<t> {public
	void info ()
	{
		System.out.println ("I am Pair");
	}
Pair<string>[] P=new pair<string>[10];//This sentence programming environment Eclipse prompts for an error and cannot even compile through

Why Java does not allow the creation of generic arrays. See the core technology in the explanation can not understand, and opened the "Crazy Java Handout", two books together, finally read. Also read the online data, but most of the copy to copy, but also said not understand.

First, from a question about why--java uses generics. One of the purposes of introducing generics is to improve the security of the program, to reduce the occurrence of errors, or to use a more vivid code. Please look at the following code

public class Test {public
	
	static void Main (string[] args) {
		
		ArrayList list=new ArrayList ();
		List.add (New Pair ());
		List.add (5);
		Pair p= (Pair) list.get (0);
		P.info (); 
	}


Running the code above will not have any problems, but we have a slight change, that is, to release the annotated line of code, and to change the index to 1, that becomes the following code

public class Test {public
	
	static void Main (string[] args) {
		
		ArrayList list=new ArrayList ();
		List.add (New Pair ());
		List.add (5);
		Pair p= (Pair) list.get (1);
		P.info (); 
	}


The program will occur java.lang.ClassCastException, obviously, because we are forcing the int type to pair type. The above code was written before the introduction of generic code, and after the introduction of generics, the Java collection has been overridden to cater to generics. One of the purposes of introducing generics is to eliminate this vulnerability, and a principle of generics is cited-if a piece of code does not present an "unchecked conversion" warning at compile time, the program does not throw a ClassCastException exception at run time.

We'll rewrite the above code again in a generic way, as follows

The compilation environment prompts the error, the compilation all does not pass, therefore this in compile phase avoids this kind of hidden trouble occurrence. Generics can guarantee the unification of types.

Next, the focus of this blog-why not create a generic array.

This is about a feature of the array, look at the code

public class Father {

}

public class Son extends father{

}

public class Test {public
	
	static void Main (string[] args) {
		
		father[] son=new son[10];
	}

Arrays are allowed to assign a subclass array to a parent class array variable. What's going to happen.

public class Test {public
	
	static void Main (string[] args) {
		
		pair<string>[] p=new pair<string>[10];/ /The actual sentence is not compiled, eclipse will prompt for error
		object[] oj=p
	}
If you allow the creation of a generic array, will be able to store objects of any class in the array p, and be able to compile because P is considered a object[in the compile phase, where p can put an int, or a pair, when we take out the int and force it into pair, What happens when you call its info (). Java.lang.ClassCastException. This violates the principle of generics introduction. Therefore, Java does not allow the creation of generic arrays.

One might say that even if I use the generic arraylist<pair> list=new arraylist<pair> (), I'm not going to be able to take out the objects inside, using explicit forced-type conversions? For example, Father f= ( Father) list.get (0), the actual operation you will find that you will still be prompted to error, compile however, this does not violate the principle of generics introduction.


Finally, thanks to the Java Core Technology volume I and the Crazy Java handout.

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.