Java泛型 - 不允许 instanceOf

Java泛型 - 不允许 instanceOf 首页 / 泛型入门教程 / Java泛型 - 不允许 instanceOf

由于编译器使用类型擦除,因此运行时不会跟踪类型参数,因此在运行时无法使用instanceOf运算符验证Box <Integer>和Box <String>之间差异。

Box<Integer> integerBox = new Box<Integer>();

//Compiler Error:
//Cannot perform instanceof check against 
//parameterized type Box<Integer>. 
//Use the form Box<?> instead since further 
//generic type information will be erased at runtime
if(integerBox instanceof Box<Integer>) { }

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

微服务架构实战160讲 -〔杨波〕

如何设计一个秒杀系统 -〔许令波〕

TensorFlow快速入门与实战 -〔彭靖田〕

编辑训练营 -〔总编室〕

视觉笔记入门课 -〔高伟〕

职场求生攻略 -〔臧萌〕

中间件核心技术与实战 -〔丁威〕

Go进阶 · 分布式爬虫实战 -〔郑建勋〕

快速上手C++数据结构与算法 -〔王健伟〕

好记忆不如烂笔头。留下您的足迹吧 :)