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>) { }

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

技术教程推荐

朱赟的技术管理课 -〔朱赟〕

硅谷产品实战36讲 -〔曲晓音〕

Linux性能优化实战 -〔倪朋飞〕

玩转Git三剑客 -〔苏玲〕

即时消息技术剖析与实战 -〔袁武林〕

高并发系统设计40问 -〔唐扬〕

Go 并发编程实战课 -〔晁岳攀(鸟窝)〕

超级访谈:对话玉伯 -〔玉伯〕

手把手带你写一个 MiniTomcat -〔郭屹〕

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