Home  >  Article  >  Java  >  What does <> mean in java?

What does <> mean in java?

青灯夜游
青灯夜游Original
2019-11-19 14:49:5717403browse

What does <> mean in java?

What does <> mean in java?

In java, <> refers to generics. Generics can detect type errors during compilation and prevent undefined types from appearing. The essence of generics is a parameterized type, which means that the data type being operated on is specified as a parameter.

Definition of generics:

Generics are a feature of programming languages. Allows programmers writing generic code in a strongly typed programming language to define mutable parts that must be specified before use.

Various programming languages, their compilers, and runtime environments have different support for generics. A data type that parameterizes types to achieve code reuse and improve software development work efficiency.

Generic classes are reference types and heap objects. They mainly introduce the concept of type parameters.

Benefits of using generics

The introduction of generics into the Java language is a major functional enhancement. Not only have languages, type systems, and compilers changed significantly to support generics, but class libraries have also undergone major changes. Many important classes, such as the collection framework, have become generic. This brings many benefits:

1. Type safety

The main goal of generics is to improve the type safety of Java programs. By knowing the type restrictions of variables defined using generics, the compiler can verify type assumptions at a very high level. Without generics, these assumptions exist only in the minds of system developers.

Generics allow the compiler to enforce these additional type constraints by capturing this additional type information in the variable declaration. Type errors are now caught at compile time instead of being displayed as a ClassCastException at runtime. Moving type checking from runtime to compile time helps Java developers find errors earlier and more easily, and can improve program reliability.

2. Eliminate casts

A side benefit of generics is the elimination of many casts in the source code. This makes the code more readable and reduces the chance of errors. Although reducing casts can improve code that uses generic classes, there is a corresponding penalty when declaring generic variables. Using a generic variable once in a simple program does not reduce the code's popularity. But for large programs that use generic variables multiple times, the level of likes can be reduced cumulatively. Therefore, after generics eliminate forced type conversion, the code will be clearer and cleaner.

2. Eliminate casts

A side benefit of generics is the elimination of many casts in the source code. This makes the code more readable and reduces the chance of errors. Although reducing casts can improve code that uses generic classes, there is a corresponding penalty when declaring generic variables. Using a generic variable once in a simple program does not reduce the code's popularity. But for large programs that use generic variables multiple times, the level of likes can be reduced cumulatively. Therefore, after generics eliminate forced type conversion, the code will be clearer and cleaner.

3. Higher operating efficiency

In non-generic programming, passing a single type as an Object will cause Boxing and Unboxing operations. This Both processes have significant overhead. After the introduction of generics, there is no need to perform Boxing and Unboxing operations, so the operating efficiency is relatively high. Especially in systems where collection operations are very frequent, the performance improvement brought by this feature is more obvious.

4. Potential performance gains

Generics bring the possibility of greater optimization. In the initial implementation of generics, the compiler inserts casts (without generics, Java system developers would specify these casts) into the generated bytecode. But the fact that more type information is available to the compiler opens up the possibility of optimizations in future versions of the JVM.

Recommended tutorial: Java tutorial

The above is the detailed content of What does <> mean in java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn