小明学java基础系列——反射类型Type

参考文档:JAVA15语言官方文档

一、Type介绍

Type是Java 编程语言中所有类型的公共高级接口,也就是Java中所有"类型"的接口。官方原话定义如下

官方文档:Type is the common superinterface for all types in the Java programming language. These include raw types, parameterized types, array types, type variables and primitive types.

这样的官方描述有点难懂,此处我画个图解释一下。Type其实是和泛型一起出现的,可以说Type就是为了支持泛型。

  • 泛型出现之前,我们可以通过Class来确认一个对象的类型,比如ClassA A,那么A的类型就是ClassA;
  • 泛型出现之后,显然不能通过Class唯一确认一个对象的类型,比如List<ClassA> A,A的Class是List,但是A的类型显然不仅仅是List,它是由Class类型的List + TypeVariables的ClassA联合确认的一个Type。

A type variable is an unqualified identifier used as a type in class, interface, method, and constructor bodies.

在这里插入图片描述

The types of the Java programming language are divided into two kinds: primitive types and reference types. The primitive types are the boolean type and the numeric types. The numeric types are the integral types byte, short, int, long, and char, and the floating-point types float and double. The reference types are class types, interface types, and array types. There is also a special null type. An object is a dynamically created instance of a class type or a dynamically created array. The values of a reference type are references to objects. All objects, including arrays, support the methods of class Object . String literals are represented by String objects.

二、Type的类型

Type可以分为两大类:包含TypeVariables和不包含TypeVariables的类型:

  • 不包含TypeVariable:包含基本数据类型(int, long等),基本Class(如Object,不包含泛型的类);
  • 包含TypeVariable,按照包含的TypeVariable又分为以下几类:
    • ParameterizedType: 表示一种参数化的类型,如List<String>,泛型的参数已经指定;
    • GenericArrayType: 表示一种元素类型是参数化类型或者类型变量的数组类型,如List<String>[][];
    • WildcardType: 代表一种通配符类型表达式,比如List<?>, List<? extends ClassA>, List<? super Object>。
      在这里插入图片描述

继续介绍Type之前,需要先介绍一下java的泛型机制:

泛型是Java SE 1.5的新特性,泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数。这种参数类型可以用在类、接口和方法的创建中,分别称为泛型类、泛型接口、泛型方法。 Java语言引入泛型的好处是安全简单。泛型的好处是在编译的时候检查类型安全,并且所有的强制转换都是自动和隐式的,以提高代码的重用率。

泛型信息只存在于代码编译阶段,

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

-御狐神-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值