Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What does java generics mean and how to use it

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "what is the meaning of java generics and how to use them", so the editor summarizes the following contents, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article "what is the meaning of java generics and how to use it?"

Generics examples explain why generics / / do not use generics List list = new ArrayList (); list.add ("coding"); / / different types of elements can be added to collections (collections are designed to achieve generality, but also bring drawbacks, generics are created to solve this) list.add (1024) / / all the elements thrown into the collection become ObjectString result1 = list.get (0); / / this line of code compiler is just String result2 = (String) list.get (0); / / if you want to restore elements in the collection using cast, casting may throw an exception because there are many types of elements in the collection. String result3 = (String) list.get (1); / / this line of code will report a type conversion exception ClassCastException at run time, so it is easy to produce bugSystem.out.println (result2) if the collection does not use generics to restrict data types; the definition of generics

1. Through the introduction of the above example, we can conclude that generics are essentially parameterized types, and we can specify a type parameter for classes, interfaces, and methods to limit the data types of operations through this parameters. in order to ensure the absolute safety of type conversion.

2. Basic usage: generic collection

/ / use the generic type List str1 = new ArrayList (); / / specify the element type str1.add ("java"); str1.add (1024) in the collection; / / this line of code compiler reports an error, that is, it restricts that the type in the collection can only be String type, avoiding other uses of exception generics during forced type conversion.

The above example tells us that generics can solve the shortcomings in collections, but generics do more than that.

Generic method

When a generic method is defined, it needs to be added before the return type of the method, this T can be changed into other letters, T represents what the parameter of the method is, T can represent any wrapper type, and the basic type is not supported.

A generic method does not explicitly specify the data type of its parameters, but determines the data type only when the method is used. The advantage of this is that a generic method can accept different types of input parameters, reducing duplication of code. The following example is for illustration only.

Public class Generics_Test {/ / generic method public static void print (T [] arr) {System.out.println (arr [0]);} public static void main (String [] args) {String [] str2 = {"test"}; print (str2); Integer [] num = {1024} Print (num);}} generic class

Introduction background: when there are multiple generic methods in a class, in order to avoid the need for each generic method to be declared to indicate that it is a generic method, a generic class is introduced.

/ / generic class public class Generics_Test {public static void print (T [] arr) {/ / static still needs to display the declaration, otherwise it will report an error, because static methods can be called without class instantiation. System.out.println (arr [0]);} public void printf (T [] arr) {/ / ordinary methods no longer need to declare System.out.println (arr [0]);} public static void main (String [] args) {Generics_Test gt = new Generics_Test () String [] str2 = {"test"}; gt.printf (str2); Generics_Test gt1 = new Generics_Test (); Integer [] num = {1024}; gt1.printf (num);} Advanced usage wildcards for generics:

You don't have to specify the parameter type, that is, you don't have to declare it before the return value of the method

/ / the wildcard public class Generics_Test {public static void print (List arr) {/ / can be used without specifying the parameter type, that is, without declaring Object result = arr.get (0); System.out.println (result) before the return value of the method } public static void main (String [] args) {List str1 = new ArrayList (); str1.add ("coding"); print (str1);}} wildcards:

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report