Questions of Java generic concept-related polygons

Source: Internet
Author: User
Tags java se

Q: What is the generic type of Java? What are the benefits and advantages? What is the difference between generics in different versions of JDK?

A: Generics are a new feature of Java SE 1.5, and the nature of generics is parameterized types, which can be used in the creation of classes, interfaces, and methods, respectively, as generic classes, generic interfaces, and generic methods. In the case of Java SE 1.5 without generics, only through the reference to the type Object to implement the arbitrary parameter, the disadvantage is to do explicit coercion type conversion, and this cast compiler period is not checked, it is easy to leave the problem to run time, so The benefit of generics is that type safety is checked at compile time, and all casts are automatic and implicit, increasing the reuse rate of code and avoiding classcastexception at run time.

JDK 1.5 introduced generics to allow strongly typed type checking at compile time, and the JDK 1.7 generic instantiation type has automatic inference capabilities, such as list<string> List = new arraylist<string> (); Can be written list<string> llist = new arraylist<> (); , the JDK has automatic inference capability. Here are a few ways to say compatibility with different versions:

JDK 1.5 Recommended use of the wording

List<string> list =new arraylist<string> ();

JDK 1.7 Recommended use of the wording

List<string> llist =new arraylist<> ();

Can be used, but not recommended, to be compatible with older versions, the IDE prompts for warnings and can be masked by annotations

List<string> list =new ArrayList ();

Can be used, but not recommended, to be compatible with older versions, the IDE prompts for warnings and can be masked by annotations

List List =new arraylist<string> ();

Q: How does the Java generics work? What is type erase?

A: Generics are implemented by type Erasure, where the compiler erases all generic type-related information at compile time, so there is no generic type-related information at run time, for example, list<integer> is represented only by a List at run time, and is intended to be used with Java Compatible with versions prior to 1.5. A generic erase is specifically a type check that is compiled into bytecode, followed by a type wipe (that is, all type parameters are replaced with their qualified type, including classes, variables, and methods), and then a bridge method is resolved in the subclass if the type erasure and polymorphism conflict occur. Then, if the return type of the calling generic method is erased, the coercion type conversion is inserted when the method is called.

Q: What is the difference between a Java generic class, a generic interface, and a generic method?

A: A generic class is a type that can be determined when an object of a class is instantiated, such as class Test<t> {}, which must indicate the specific type of generic T when instantiating the class.

Generic interfaces, like generic classes, are defined such as interface generator<e> {e Dunc (e e);}.

The class of a generic method can be a generic class or a non-generic class, and whether the owning generic method is independent of the class in which it resides, we should use the generic method as much as possible in our application, and do not magnify the space, especially when the static method cannot access the type parameters of the generic class. Therefore, it is more important to use the static method of generics (declaring generics must be written before returning a value type after static). A generic method is defined such as <T> void Func (T val) {}.

Q: How does Java gracefully implement tuples?

A: The tuple is actually a relational database of an academic term, a record is a tuple, a table is a relationship, record composition table, tuple generation relationship, this is the core concept of relational database. Many languages naturally support tuples, such as Python, in the syntax itself supports tuples in the language of the tuple is expressed in parentheses, such as (int, bool, string) is a ternary group type, but in Java, C and other languages are compared to the pit father, language grammar itself does not have this feature, so in In Java, if we want to implement the tuple gracefully, we can use the generic class implementation, which is the implementation of a ternary group type:

triplet<a,b,c>{

Private a A;

Private B A;

Private C A;

Public Triplet (A, b b,c C) {

THIS.A =a;

this.b =b;

THIS.C =c;

}

}


Welcome to join the Learning Exchange Group 569772982, we learn to communicate together.

Questions of Java generic concept-related polygons

Related Article

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.