知识库 知识库
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)

Jim FuckPPT

Java小学生
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)
  • Hyperskill - Java

    • Java basic

    • Java OOP

    • 应知

      • Theory:Functional decomposition
      • Theory:Paradigms
      • Theory:Overloading
      • Theory:Write, compile, and run
      • Theory:Annotations basics
      • Theory:JVM, JRE, and JDK
      • Theory:Jave Archive
      • Theory:Running programs on your computer
      • Theory:Enums in Java
      • Theory:Fields and methods in enum
      • Theory:StringBuilder
      • Theory:Immutability
      • Theory:Boxing and unboxing
      • Theory:Introduction to generic programming
      • Theory:Generics and Object
        • Reusing code with generics
        • Reusing code with Object
        • The advantage of generics: from run-time to compile-time
        • Generics without specifying a type argument
        • Conclusion
      • Theory:What are collections
      • Theory:The collections Framework overview
      • Theory:ArrayList
      • Theory:The List interface
      • Theory:Comparable
      • Theory:Processing strings
      • Theory:Initialization blocks
      • Theory:Introduction to API
      • Theory:Generic methods
    • 扩展

    • IO & Stream

    • Error & Exception

    • Algorithm & Data structure

    • Design pattern

    • Web

    • Spring boot

  • 练习题

  • Frank - Java与生活

  • Frank - Java API进阶

  • 学习笔记

  • Java
  • Hyperskill - Java
  • 应知
Jim
2022-09-28
目录

Theory:Generics and Object

As you know, generics enable us to parameterize types when defining classes (or interfaces) and methods. Parameterized types make it possible to re-use the same code while processing different concrete types.

# Reusing code with generics

Let's consider a generic class named GenericType that stores a value of "some type".

class GenericType<T> { 

    private T t;

    public GenericType(T t) {
        this.t = t;
    }

    public T get() {
        return t;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12

It is possible to create an object with a concrete type (e.g., String):

GenericType<String> instance1 = new GenericType<>("abc");
String str = instance1.get();
1
2

We can also create instances with other types (Integer, Character) and then invoke the get method to access the internal field. In this manner, generics allow us to use the same class and methods for processing different types.

# Reusing code with Object

But there is another way to reuse code. If we declare a field of type Object, we can assign a value of any reference type to it. This approach was widely used before the introduction of generics in Java 5.

The following class demonstrates this concept:

class NonGenericClass {

    private Object val;

    public NonGenericClass(Object val) {
        this.val = val;
    }

    public Object get() {
        return val;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12

Now we can create an instance of this class with the same string as in the previous example (see GenericType).

NonGenericClass instance2 = new NonGenericClass("abc");
1

It is also possible to create an instance by passing in a value of type Integer, Character, or any other reference type.

Using the Object class this way allows us to reuse the same class to store different data types.

# The advantage of generics: from run-time to compile-time

After an invocation of the get() method we obtain an Object, not a String or an Integer. We cannot get a string directly from the method.

NonGenericClass instance2 = new NonGenericClass("abc");
String str = instance2.get(); // Compile-time error: Incompatible types
1
2

To get the string back, we must perform an explicit type-cast to the String class.

String str = (String) instance2.get(); // "abc"
1

This works because a string was passed into instance2. But what if the instance does not store a string? If this is the case, the code throws an exception. Here is an example:

NonGenericClass instance3 = new NonGenericClass(123);
String str = (String) instance3.get(); // throws java.lang.ClassCastException
1
2

Now we can see the main advantage of generics over the Object class. Because there is no need to perform an explicit type-cast, we never get a runtime exception. If we do something wrong, we can see it at compile-time.

GenericType<String> instance4 = new GenericType<>("abc");
        
String str = instance4.get(); // There is no type-casting here
Integer num = instance4.get(); // It does not compile
1
2
3
4

A compile-time error will be detected by the programmer, not a user of the program. Because generics let the compiler take care of type casting, generics are both safer and more flexible compared to the Object class.

# Generics without specifying a type argument

When you create an instance of a generic class, you have the option to not specify an argument type at all.

GenericType instance5 = new GenericType("my-string");
1

In this case, the field of the class is Object, and the get method returns an Object as well.

The above code is equivalent to the following line:

GenericType<Object> instance5 = new GenericType<>("abc"); // it is parameterized with Object
1

Usually, you will not use generics parameterized by Object due to the same problems as presented above. Just remember that this possibility exists.

# Conclusion

Both generics and Object allow you to write generalized code. Using Object, however, may require explicit type-casting, which can lead to error-prone code. Generics provide type-safety by shifting type checking responsibilities to the Java compiler.

编辑 (opens new window)
#Generic#Java
上次更新: 2022/10/04, 21:45:45
Theory:Introduction to generic programming
Theory:What are collections

← Theory:Introduction to generic programming Theory:What are collections→

最近更新
01
《挪威的森林》
04-14
02
青钢影
04-14
03
Processing strings
02-18
更多文章>
Theme by Vdoing | Copyright © 2022-2023 Jim Frank | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式