In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "the difference between T and question mark in Java generics". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the difference between T and question mark in Java generics.
There are types: simple types and complex types. After the introduction of generics, complex types are divided into more details.
Overview
Generics are a new feature of Java SE 1.5. generics are parameterized types in nature, that is, the data type being operated on is specified as a parameter. This parameter type can be used in the creation of classes, interfaces, and methods, called generic classes, generic interfaces, and generic methods, respectively. The advantage of introducing generics into the Java language is its security and simplicity.
Before Java SE 1.5, in the absence of generics, the parameter was "arbitrary" through a reference to the type Object. The disadvantage of "arbitrariness" is to explicitly cast a type that requires the developer to predict the actual parameter type. In the case of cast errors, the compiler may not prompt the error and an exception occurs at run time, which is a security hazard.
The advantage of generics is that type safety is checked at compile time, and all casts are automatic and implicit to improve code reuse.
Rule restrictions on generics
The type parameters of generics can only be class types (including custom classes), not simple types.
The same generic type can correspond to multiple versions (because the parameter type is uncertain), and different versions of generic class instances are incompatible.
Generics can have more than one type parameter.
The parameter types of generics can use extenders statements, for example. It is traditionally called a "bounded type".
The parameter type of a generic can also be a wildcard type. For example, Class classType = Class.forName ("java.lang.String")
1. Specific examples
Here are two simple examples to achieve the same function, one using generics and one without generics.
Example 1: using generics
Public class Gen {private T t; public Gen (T) {this.t = t;} public T getT () {return t;} public void setT (T) {this.t = t;} public void showType () {System.out.println ("the actual type of T is:" + t.getClass (). GetName ());} public static void main (String [] args) {Gen gen = new Gen (1) Gen.showType (); int I = gen.getT (); System.out.println ("value =" + I); System.out.println ("= ="); / / define a version of String of the generic class Gen GenstrObj = new Gen ("Hello Gen!"); strObj.showType (); String s = strObj.getT (); System.out.println ("value =" + s);}}
Example 2: no generics are used
Public class Gen2 {/ / defines a generic type member private Object obj; public Gen2 (Object obj) {this.obj = obj;} public Object getObj () {return obj;} public void setObj (Object obj) {this.obj = obj;} public void showType () {System.out.println ("T's actual type is:" + obj.getClass (). GetName ()) } public static void main (String [] args) {/ / define an Integer version of class Gen2 Gen2 intObj = new Gen2 (2); intObj.showType (); int I = (Integer) intObj.getObj (); System.out.println ("value =" + I); System.out.println ("= ="); / / define a String version of class Gen2 Gen2 strOb = new Gen2 ("Hello Gen!") StrOb.showType (); String s = (String) strOb.getObj (); System.out.println ("value=" + s);}}
2. Go deep into generics
Before Java 5, in order to make classes universal, parameter types and return types were often set to Object types. When these return types were obtained for use, they had to be "forced" to be converted to the original type or interface before methods on the object could be called.
Generics are exactly the same as using "Object generics" to achieve the result, but it is much simpler because there is no need to cast.
Generic class syntax:
Use to declare a type holder name, and then you can declare members, parameters, and return value types as a type representative. Of course, T is just a name, which can be defined by itself.
Class GenericsFoo declares a generic class, and this T has no restrictions and is actually equivalent to Object type and actually equivalent to class GenericsFoo.
In contrast to Object generic classes, classes defined using generics can use "" to specify the real type of the generic type holder when declaring and constructing instances. For example:
GenericsFoo douFoo=new GenericsFoo (new Double ("33"))
Of course, you can also construct an object without using angle brackets to specify the true type of the generic type, but you need to cast when you use the object. For example:
GenericsFoo douFoo=new GenericsFoo (new Double ("33"))
In fact, when you construct an object without specifying type information, the Object type is used by default, which is why you want to cast.
3. Advanced applications
Restricted generics
In the above example, since there is no restriction on the scope of the class GenericsFoo type holder T, the qualified type here is actually equivalent to Object, which is essentially the same as the "Object generic". Restrictions such as we want to restrict T to the collection interface type. Just do this:
Class GenericsFoo, so that the generic T in the class can only be the implementation class of the Collection interface, and passing in a non-Collection interface will cause an error.
Multi-interface restriction
Although Java generics simply use extends to uniformly express the original concepts of extends and implements, it still has to follow the application system. Java can only inherit one class, but it can implement multiple interfaces, so your certain type needs to be defined by extends, and when there are multiple types, only one is class, and the class is written first, and the interface is listed next, that is:
(type qualification of generic methods)
(restrictions on type parameters in generic classes)
The public class Demo {/ / T type can use the methods declared by Comparable and the features owned by Seriablizable}
Wildcard generics
In order to solve the problem that types are so limited that they cannot be determined dynamically based on instances, wildcard generics are introduced. For the above example, using the wildcard generic format without extends, then Object and any Java classes under it are allowed by default. That is, any class.
Wildcard generics can not only be restricted downward, such as unknownList;List
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.