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 is the principle analysis of Java generics?

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how the principle analysis of Java generics is, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.

I. Preface

Generics play an important role in java and are also very useful in actual development.

2. Generics

Generics: a feature introduced in jdk5, which provides a security detection mechanism for compiling types to types. This mechanism allows illegal types to be detected at compile time. It is essentially a parameterized type, that is, the data type being operated is invariably specified as a parameter.

Parameterize the original specific type, and then pass in the specific parameters when using / calling

Types of generics:

①: generic class

②: generic method

③: generic interface

3. The format of the generic definition:

Specifies the format of a type, where the type can be regarded as a formal parameter.

Specify the format of multiple types, which are separated by commas. Here, type 1 and type 2 can be regarded as formal parameters, and the parameters for specific calls in the future can be regarded as arguments, and the generics here can only be reference types.

What is a reference type?

Except for the eight basic types, the others are reference types, such as the wrapper class corresponding to the basic type.

Boolean-- > Boolean

Char-> Character

Byte-- > Byte

Short-- > Short

Int-- > Integer

Long-- > Long

Float-- > Float

Double-- > Double

The benefits of generics are:

Advance the running problem to the compilation time

Avoid forced conversion

No specified type defaults to Object type

For example, when generics are not used:

Import java.util.HashMap;import java.util.Map;import java.util.Set; public class generic Demo {public static void main (String [] args) {/ / create an object Map map=new HashMap (); / / add data map.put (1, "Zhang San"); map.put (2, "Li Si"); map.put (3, "Wang Wu") Map.put (5,6); / / set Set keySet=map.keySet () with different types of data / / keys added; / / key for (Object key: keySet) {String value= (String) map.get (key); System.out.println (key+ ":" + value);}

A type conversion exception was reported (int will be automatically converted to Integer). Integer cannot be converted to String.

Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

At this point, the benefits of generics are realized, pulling errors that can occur at run time to the compilation time.

Of course, you can also use Object to receive the output.

/ / set of keys Set keySet=map.keySet (); / / key finding values for (Object key: keySet) {Object value=map.get (key); / / String value= (String) map.get (key); System.out.println (key+ ":" + value);}

In this way, you can also get the desired results.

4. Generic classes

The definition format of generic classes:

Modifier class class name {}

For example:

Public class Generic {}

The T here can be identified arbitrarily, and common forms such as T, E, K, V and so on are often used to represent generics.

Under the generic class

Public class Generic {/ / defines a variable that provides the get set method private T t; public T getT () {return t;} public void SetT (T t) {this.t=t;}}

Under the test class

Public class GenericDemo {public static void main (String [] args) {Generic g1=new Generic (); g1.SetT (Chapter 3); System.out.println (g1.getT ()); System.out.println ("-"); Generic g2=new Generic (); g2.SetT (100); System.out.println (g2.getT ()) }}

Running result

The advantage of defining this generic class is that you can enter various types you want in the test.

5. Generic method

Define the format of generic methods:

Modifier returns value type method name (type type name)

For example:

Public void show (T t) {}

Generic method

Public class Generic {public void show (T t) {System.out.println (t);}}

Under the generic method test:

Public class GenericDemo {public static void main (String [] args) {Generic g=new Generic (); g.show ("Zhang San"); g.show; g.show (true); g.show (null);}}

Running result

Generic methods are much more convenient than generic classes. You can use them directly here, and you can use the parameters of these types.

VI. Generic interface

Format: modifier interface interface name {}

For example:

Public interface Generic {}

Since it is an interface, you have to define a class to implement the interface.

Generic interface:

Public interface Generic {}

Under GenricImp class:

Public class GenricImp Generic {public void show (T t) {System.out.println (t);}}

Under GenricDemo class:

Public class GenricDemo {public static void main (String [] args) {Generic g1=new GenericImp (); g1.show ("Zhang San"); Generic g2=new GenericImp (); g2.show (100);}} VII. Type 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