java generics advanced features

Senior Java language

1. Use the generic sense

Why do we need generics?

So the generic benefit is:

l applicable to a variety of data types same code is executed

Int type adder achieved, for example, sometimes need to achieve long summation type, double type if required sum, a need to re-enter the heavy-duty type double add method.

If you do not have to reload the generic Each type of digital add a method, and there is no more than the amount of code necessary, follow-up maintenance is also troublesome, if the add method to add a line of code inside? It would need to modify the 10 methods.

l in the generic type specified in use, do not need to cast

For example, do not set then the default generic type Object, you can deposit String, int, double, save the data very happy, but when used is a disaster. You have to cast to a specific target type, and it is prone to "java.lang.ClassCastException" exception.

Generic classes and generic interface

Generic, or "parameterized types."

Parameters are familiar, tangible custom method parameters, call the method when you want to pass arguments.

As the name suggests, is the type of the parameter of the specific type of the original , similar to the process variable parameter at this time is also defined a parameter type form (type parameter may be referred to), and particularly when using the incoming / calling type (type arguments).

Generic nature is to parameterized types , different output parameters of the different results in the past, can also be parameterized types, that is, different parameters of different types of output.

That is the generic use, the data type of the operation is specified as a parameter, which can be used in the type of classes, interfaces and methods, are called generic class, generic interfaces, generic method .

Defined type variable

extends the upper limit of the wildcard may receive extend Types subclass

The lower limit of the super wildcard may receive super parent Types

T extends Comparable中

T represents the type of subtype should be bound, represents the Comparable binding type, binding types and subtypes may also be an interface class.

If this time, we try to pass a no instance of a class that implements the interface Comparable, it will compile error.


The generic class type variable static context failure

Generics and static are not compatible,

You can not reference type variable domain or in a static method. ** due to generics is to know when objects created what type of the code object is created in order to perform static part, and then the constructor, and so on. So before the static object initialization section has been executed, and if you are in the static part of the generic reference, then there is no doubt that virtual machines do not know what it is, because this time ** class has not initialized.
Type erasure
generics is Java 1.5 version before the introduction of the concept, before this is not a generic concept, but obviously, generic code is well and good compatibility with previous versions of the code.

This is because the information exists only in the generic code compilation stage, before entering the JVM, associated with the generic information will be erased, jargon is called type erasure.

More simply, generic class and common class in the java virtual machine is nothing special about it.
Java Generics erase is an important feature of Java generics, and its purpose is to avoid too much class and create excessive consumption caused by the operation of. So, I think ArrayList and ArrayList both instances, it is the same class instance.

But in many cases we need to get generic information at runtime, then we can define a class by way of (usually anonymous inner classes, because we create this class just to get generic information) to get generic parameters at runtime, to meet the needs e.g. serialization, deserialization other work.

Published 71 original articles · won praise 4 · views 10000 +

Guess you like

Origin blog.csdn.net/wang_8649/article/details/103938534