Some notes on Java generics

Source: Internet
Author: User

1, in the Java Virtual machine is not generic, all generic class parameters will be erased, as follows:

 Public void int num) {}    publicvoidint num) {}

These two methods are the same for the Java compiler and therefore cannot be compiled.

2, the following two kinds of writing are OK, but recommended the latter:

 Public extends void Test (list<i> list) {}        publicvoidextends fruit> list) {}

3, about <? Extends > and <? Extends >

        //< extends t> represents the upper bounds of the type, which indicates that the parameterized type may be a subclass of T or T//< Super t> represents the lower bound of a type, indicating that the parameterized type is a supertype of this type (parent type)List<?extendsfruit> list =NewArraylist<apple>(); //Flist.add (New Fruit ()); Compile errorList.add (NULL); Apple Apple= (Apple) list.get (0); Fruit Item= (Fruit) list.get (0); List<?SuperFruit> List1 =NewArraylist<fruit>(); List1.add (NewFruit ()); List1.add (NewApple ()); List1.add (NULL); Fruit item1= (Fruit) list1.get (0); //Apple Apple = (apple) list1.get (0); compilation error

Some notes on Java generics

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.