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's the difference between the JAVA generic wildcard TMagi Epene Kpeng V?

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

Share

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

The main content of this article is to explain "what is the difference between the JAVA generic wildcard Tpene Epene Kpene V". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what's the difference between the JAVA generic wildcard Tpene Epene Kpene V"!

1. Explain the concept of generics first.

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.

Take a chestnut:

The Box class is defined as a generic class

Public class Box {private T object;public void set (T object) {this.object = object;} public T get () {return object;}}

Create a Box object without generic parameters, and find that you need to cast when getting the object

Box box2 = new Box (); box2.set (new Apple ()); Apple apple = (Apple) box2.get ()

Create a Box object with generic parameters that do not need to be cast when getting the object

Box box = new Box (); box.set (new Apple ()); Apple apple = box.get ()

To sum up, the advantage of generics is

Without casting, you can check type safety at compile time, and can be used on classes, methods, and interfaces

But when we define generic classes, generic methods, generic interfaces, we often come across a lot of different wildcards, such as TMagi, E, K, V, and so on. What do these wildcards mean? Keep looking down.

two。 Next, let's talk about the difference between the generic wildcard character Tpene, Epene, K, and V.

These all belong to the wildcards of java generics. At first I saw so many wildcards and got dizzy. There is no difference between these. It's just an agreed code, that is to say,

If you use the capital letters A, B, C, D, X, Y, Z, they are all generics, and it's the same with changing T into A, where T is just the meaning of the name.

? Represents an uncertain java type T (type) represents a specific java type K V (key value) represents Key Value E (element) in the java key value represents Element

Take a chestnut:

Public class Test {public List list = new ArrayList (); public static void main (String [] args) {Test test = new Test (); test.list.add ("hello"); System.out.println (test.list);}}

And

Public class Test {public List list = new ArrayList (); public static void main (String [] args) {Test test = new Test (); test.list.add ("hello"); System.out.println (test.list);}}

Replace T with A, there is no difference in the implementation effect, but we agreed that T represents type, so it is better to follow the agreed specification to increase the readability of the code.

If you want to define multiple generic parameters, such as two generic parameters, a typical chestnut is Map's key,value generic, we can also define one of these

Public interface Mymap {public K getKey (); public V getValue ();} public class MymapImpl implements Mymap {private K key;private V value;public MymapImpl (K key, V value) {this.key = key;this.value = value;} public K getKey () {return key;} public V getValue () {return value;}}

Next, you can pass in any type and create an instance without converting the type.

Mymap mp1= new MymapImpl ("Even" 8); Mymap mp2= new MymapImpl ("hello", "world"); Mymap mp3= new MymapImpl (888,888)

If you want to define more than two, three or more generic parameters, you can use T1, T2,..., Tn, like this.

Public class Test {public void print (T1 T1 T2 T2 T3 T3) {System.out.println (t1.getClass ()); System.out.println (t2.getClass ()); System.out.println (t3.getClass ());}}

3. Next, let's talk about the difference between List,List,List.

ArrayList al=new ArrayList (); specifies that collection elements can only be of type T

ArrayList al=new ArrayList (); collection elements can be of any type, which is meaningless, usually in a method, just to illustrate usage

ArrayList distinction

T is a concrete class, such as String,List,Map. Wait, these all belong to specific classes, which is easier to understand.

What is Class? Class is also a class, but Class stores the above String,List,Map. Class information of a class, a bit abstract, let's look at it step by step.

There are three ways to get the Class class:

1. Call the getClass () method of the Object class to get the Class object, which is the most common way to generate a Class object.

For example:

List list = null;Class clazz = list.getClass ()

two。 Use the static forName () method in the Class class to get the Class object corresponding to the string.

For example:

Class clazz = Class.forName ("com.lyang.demo.fanxing.People")

3. The third method to get an object of type Class is very simple. If T is a Java type, then T.class represents the matching class object.

Class clazz = List.class

So here comes the question? The Class class has been created, but when are Class and Class applicable? **

Using Class and Class often happens in reflective scenes, so let's first see what it's like to create a class by reflection if we don't use generics.

People people = (People) Class.forName ("com.lyang.demo.fanxing.People") .newInstance ()

See, you need to make a strong turn, and if the type of reflection is not the People class, a java.lang.ClassCastException error will be reported.

After using Class generics, you don't have to force it.

Public class Test {public static T createInstance (Class clazz) throws IllegalAccessException, InstantiationException {return clazz.newInstance ();} public static void main (String [] args) throws IllegalAccessException, InstantiationException {Fruit fruit= createInstance (Fruit .class); People people= createInstance (People.class);}}

So what's the difference between Class and Class?

When Class is instantiated, T will be replaced with a concrete class

Class it is a wildcard generic,? Can represent any type and is mainly used to limit the situation at the time of declaration

For example, you can declare a

Public Class clazz

But you can't declare one.

Public Class clazz

Because T needs to specify a type

So when you don't know what type of Class to declare, you can define a Class,Class that can be used for parameter type definitions, method return value definitions, and so on.

At this point, I believe that everyone has a deeper understanding of the difference between "JAVA generic wildcard Treco Epene Kpene V", so you might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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