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 are the generics in the common knowledge points of Java?

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article shows you what generics refer to in the common knowledge points of Java. The content is concise and easy to understand, and it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

When developers use generics, it is easy to make some mistakes according to their own intuition. For example, if a method accepts List as a formal parameter, then if you try to pass an object of List as an actual parameter, only to find that it cannot be compiled. Although intuitively, Object is the parent of String, this type conversion should be reasonable. But in fact, this creates implicit type conversion problems, so the compiler directly forbids such behavior. one。 Type erase

Generics in Java are basically implemented at the compiler level, and the type information in generics is not included in the generated Java bytecode. The type parameters added when using generics are removed by the compiler at compile time, a process called type erasure. Types such as List and List defined in the code become List after compilation. All JVM sees is List, while type information attached by generics is not visible to JVM. The Java compiler will find possible errors as much as possible at compile time, but still cannot avoid type conversion exceptions at run time.

Many of the strange features of generics are related to the existence of this type of erasure, including:

Generic classes do not have their own unique Class class objects. For example, there is no List.

.class or List.class, and only List.class.

Static variables are shared by all instances of a generic class. For declaring as MyClass

The method to access the static variables is still MyClass.myStaticVar Objects created through new MyClass or new MyClass share a static variable.

Generic type parameters cannot be used in catch statements for Java exception handling. Because exception handling is done by JVM at run time. Because the type information is erased, JVM is unable to distinguish between the two exception types MyException

And MyException. For JVM, they are all of type MyException. The catch statement corresponding to the exception cannot be executed.

The basic process of type erasure is also relatively simple, starting with finding the concrete class to replace the type parameters. This concrete class is generally Object. This upper bound is used if the upper bound of the type parameter is specified. Replace the type parameters in the code with specific classes. At the same time, remove the type declaration that appears, that is, the removed content. For example, the T get () method declaration becomes Object get (); List becomes List. Next you may need to generate some bridging methods (bridge method). This is because the class after erasing the type may lack some necessary methods. For example, consider the following code:

Class MyString implements Comparable {public int compareTo (String str) {return 0;}}

When the type information is erased, the declaration of the above class becomes class MyString implements Comparable. But in this case, the class MyString will have a compilation error because the int compareTo (Object) method declared by the interface Comparable is not implemented. At this point, the compiler generates the method dynamically.

two。 Wildcard character

When using generic classes, you can specify a specific type, for example, List declares that the specific type is String; or you can use wildcards? To represent unknown types, such as List, declares that the element types contained in List are unknown. Wildcards represent a set of types, but the specific type is unknown. What List declares is that all types are OK. But List is not the same as List. List actually determines that List contains Object and its subclasses, which can be referenced through Object when using it. The type of elements contained in List is uncertain. It may contain String, or it may be Integer. If it contains String, it would be wrong to add elements of type Integer to it. Because the type is unknown, you cannot create a new ArrayList object through the new ArrayList () method. Because the compiler cannot know what the specific type is. But elements in List can always be referenced with Object, because although the type is unknown, it must be Object and its subclasses. Consider the following code:

Public void wildcard (List list) {list.add (1); / / compilation error}

As shown above, a compilation error always occurs when you try to operate on a generic class with wildcards. The reason for this is that the type represented by wildcards is unknown.

Because elements in List can only be referenced with Object, it is not very convenient in some cases. In these cases, you can use upper and lower bounds to limit the scope of unknown types. Such as 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.

Share To

Internet Technology

Wechat

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

12
Report