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

Under what circumstances can Java generics not be used

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Today, I will talk to you about the circumstances under which Java generics cannot be used, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

1. Preface

Java 1.5 introduces generics to ensure type safety, prevent type conversion exceptions at run time, make types parameterized, and improve the readability and reuse of the code. However, generics are not allowed in some cases, so today we will summarize some scenarios where generics cannot be used in coding.

two。 Basic types cannot use generics directly.

The following words are incorrect:

/ / error

Map wrong= new HashMap ()

Basic types cannot be generic types, and their corresponding wrapper classes need to be used.

/ / OK

Map wrong= new HashMap ()

3. Generic types cannot be instantiated directly

A generic type can be understood as an abstract type, but only represents the abstraction of the type, so we cannot instantiate it directly, and the following is wrong:

Public E first (List list) {

/ / error

E e = new E ()

Return list.get (0)

}

4. Generics cannot be used as static variable types

Static types in Java are instantiated with class loading, and the specific type of the generic type is not declared. At the same time, because static variables are shared variables for all objects, their types can only be determined when the class is instantiated or the method is called. If it is a generic type, its type cannot be determined. Similarly, generics declared on a class cannot appear in a class's static method as a return value type, and the following is incorrect:

Public class Generic {

/ / generic types declared by a class cannot be used as static variables

Public static T t

/ / A generic type declared by a class cannot be used as the return value of a static method

Public static T rtval (List list) {

Return list.get (0)

}

}

5. Unable to make instanceof judgment

Generics in Java are pseudo generics, which are erased at compile time, and there are no generics in the running bytecode, so the following judgment conditions cannot be made:

Public static void wrong (List list) {

/ / error

If (list instanceof ArrayList) {

}

}

But the unbounded wildcards of generics can be used for instanceof judgment, so think carefully about why.

6. Unable to create array of parameterized type

First of all, the following is true:

/ / OK

List [] arrayOfLists = new List [2]

But with generics added, the compilation failed:

/ / error

List [] arrayOfLists = new List [2]

Failure to do so will result in the following logic error:

/ / if the above is true, then the following should also be true.

Object [] stringLists = new List []

/ / then we can put in the string List

StringLists [0] = new ArrayList ()

/ / put it into Integer list

StringLists [1] = new ArrayList ()

/ / this is obviously unreasonable

7. Throwable cannot be extended directly or indirectly

The following two ways of writing will cause a compilation error:

/ / Throwable cannot be extended indirectly

Class IndirectException extends Exception {}

/ / Throwable cannot be extended directly

Class DirectException extends Throwable {}

If established, it will appear:

Try {

/ /...

} catch (T e) {

/ / Type uncertainty cannot handle specific exception logic

}

How can you specifically handle exceptions, which is obviously not convenient for precise exception handling logic. But you can throw an indefinite exception, but you can't use class-declared generics in static methods either:

Class Parser {

/ / this is right

Public void okThrow (File file) throws T {

/ /...

}

/ / static methods cannot have generic types declared by the class as return values and exceptions

Public static void wrongThrow (File file) throws T {

}

}

8. Methods with the same parameter signature after generic erasure cannot be overloaded

Due to generic erasure, the following are not considered overloaded and cannot be compiled:

Public class NoReload {

Public void sets (Set strSet) {}

Public void sets (Set intSet) {}

} after reading the above, do you have any further understanding of the circumstances under which Java generics cannot be used? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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