Java Objects Introductionjava.util.Objects 类是 Java 7 引入的一个实用工具类,提供了一些静态方法来操作对象。它主要用于处理对象的空值情况,避免了空指针异常。 Commonly Used Methods equals(Object a, Object b):比较两个对象是否相等,避免了空指针异- 常。 hash(Object... values):计算对象的哈希码,避免了空指 2024-04-15 Java #Java Utils
Java 8 Optional IntroductionOptional 类是 Java 8 引入的一个容器类,用于解决可能存在空值的情况。它提供了一种优雅的方式来处理可能为空的对象,避免了空指针异常。 Commonly Used Methods of(T value):创建一个包含非空对象的 Optional 实例。 ofNullable(T value):创建一个可能为空的 Optional 实例。 isPresent(): 2024-04-15 Java #Java 8
Generics, Inheritance, and Subtypes As you already know, it is possible to assign an object of one type to an object of another type provided that the types are compatible. For example, you can assign an Integer to an Object, since Obje 2024-03-18 Java #Java Generics
Bounded Type Parameters There may be times when you want to restrict the types that can be used as type arguments in a parameterized type. For example, a method that operates on numbers might only want to accept instances of 2024-03-18 Java #Java Generics
Generic Methods Generic methods are methods that introduce their own type parameters. This is similar to declaring a generic type, but the type parameter’s scope is limited to the method where it is declared. Static 2024-03-18 Java #Java Generics
Generic Types A generic type is a generic class or interface that is parameterized over types. The following Box class will be modified to demonstrate the concept. A Simple Box ClassBegin by examining a non-generic 2024-03-18 Java #Java Generics
Why Use Generics? 简而言之,泛型使类型(类和接口)能够在定义类、接口和方法时成为参数。与方法声明中使用的更熟悉的形式参数非常相似,类型参数提供了一种通过不同输入重复使用相同代码的方法。区别在于形式参数的输入是值,而类型参数的输入是类型。 使用泛型的代码比非泛型代码有很多好处: 编译时更强的类型检查。 Java 编译器对泛型代码应用强类型检查,如果代码违反类型安全,则会发出错误。修复编译时错误比修复运行时错误更容易 2024-03-18 Java #Java Generics
Java Annotations Annotations, a form of metadata, provide data about a program that is not part of the program itself. Annotations have no direct effect on the operation of the code they annotate. Annotations have a n 2024-03-18 Java #Java Annotations