Finally met the java generic

Source: Internet
Author: User

I knew that the generic type was added after Java5.0. At present, I have been reading 4.0 of entry books. For example, the second edition of "Java programming" published by Tsinghua University Press (edited by Tan Haoqiang and Ma Suxia) and Sun Xin's teaching video, which has been translated by Hou Jie, it seems that these books were made before 5.0, so I didn't know about generics in the learning process. Later, I only learned about generics intermittently, but I didn't take it seriously. today, programming has finally appeared.

When defining an Arraylist type, the following error occurs after compilation:

Note: checkUser. java uses unchecked or unsafe operations.
Note: Recompile with-Xlint: unchecked for details.
 

Baidu:

If it is displayed in Chinese, it is:

Note: a. java uses unchecked or insecure operations.

Note: for details, use-Xlint: unchecked to recompile.

In fact, it is very simple because the generic type of JAVA5.0 is used, while the generic type of 5.0 does not perform type check, for example, ArrayList a = new ArrayList ();

A. add ("hello"); in this case, a warning is displayed. There are several solutions:

1) add @ SuppressWarnings ("unchecked") before the method ")

2) declare generic types, such as ArrayList <Object> a = new ArrayList <Object> ();

3) use 1.4 compatible with JDK for compilation, javac-source 1.4 Test. java

4) You can also view the warning information, javac Xlint: unchecked Test. java. This will display detailed warning information



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.