In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "how to quickly understand generics". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
First, the first knowledge of generic
Before generics, we usually used element objects of type Object. For example, we can construct a collection of type Object, which can store objects of any data type, but when we extract elements from the collection, we need to know exactly the data type of each element stored, so that we can perform element conversion, otherwise ClassCastException will occur.
1. What is generic
Generics is a new feature introduced after Java 1.5 that provides compile-time type-safety monitoring that allows us to detect illegal type data structures at compile time.
Its essence is parameterized typing, i.e. the data type being manipulated is specified as a parameter. This parameter type can be used in classes, interfaces, and methods, called generic classes, generic interfaces, and generic methods, respectively
2. Benefits of Use
type-safe
With generics, there is no ClassCastException exception at runtime as long as no warnings appear at compile time.
Eliminates cast
Taking elements out of generic collections allows us to avoid type conversions
More readable
You can see directly what data type elements are stored in the collection
2. Use of Generics
1. usage scenarios
1)generic class
basic syntax
class Name { private generic identifier variable name; ..... }
uses examples
class Result{ private T data; }
Note:
Java 1.7 can be used for type inference, and the specific data types in the following can be omitted:
class name object name = new class name ();
If we don't use it to specify the data type, then the operation type is Object.
Type arguments within generics can only be class types, not primitive data types, such as int,double,float...
When we pass in different data types to construct an object, it can logically be seen as multiple different data types, but in fact they are all the same type.
This is a simple way to use generic classes. We specify the type we want to use when we create them, and when we use them, the class automatically converts to the type the user wants to use.
So if we define a generic class, but when constructing the object without declaring the data type, then the default is Object type, when taking out the data, we need to perform type conversion:
Result objectRes = new Result("testObejct"); String str = (String) objectRes.getData(); System.out.println(str);
Rules:
Subclasses are also generic classes, so the generic types of subclasses and parent classes should be consistent.
public class ResultChild extends Result {}
Subclass is not generic class, then parent class should specify data type
public class ResultChild extends Result {}
2)generic interface
basic syntax
public interface name { generic identity method name (); ... }
uses examples
public interface ResultInterface { T getData(); }
Generic interfaces, like generic classes, have the following rules:
Implementation class is not generic class, interface should be clear data type
The implementation class is also a generic class, and the generic type of the implementation class and interface should be consistent.
3)generic method
In Java, generic classes and generic interfaces are relatively simple to define, but generic methods are more complex.
Generic class, which indicates the specific type of generic when instantiating the class
Generic methods are concrete types that indicate generics when methods are called
basic syntax
modifier return value type method name (parameter list){}
Modifier and return value type used to declare this method generic
Only declared methods are generic methods, even if generic classes in return value types use generic member methods that are not generic methods.
Indicates that the method will use generic type T before generic type T can be used in the method
uses examples
private Result getResult(T data) { return new Result(data); }
Generic methods and variable parameters:
private void printData(T... data) { for (T t : data) { System.out.println(t); } }
Note:
Generic methods enable methods to vary independently of classes
If a static method is to use generic capabilities, it must be made generic
2. Type wildcard
1)What is a type wildcard?
Type wildcards are generally used " ? "Replace specific argument types
Type wildcards are argument types, not formal argument types
Type wildcards are further divided into the upper limit of type wildcards and the lower limit of type wildcards
2)basic syntax
Upper limit of type wildcard:
class/interface
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.