什么是泛型
1.背景:
JAVA推出泛型以前,程序员可以构建一个元素类型为Object的集合,该集合能够存储任意的数据类型对象,而在使用该集合的过程中,需要程序员明确知道存储每个元素的数据类型,否则很容易引发ClassCastException异常。
小案例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | package com.genericity; import java.util.ArrayList; /** * 泛型产生的背景 */ public class MainClass { public static void main(String[] args) { ArrayList list = new ArrayList<>(); list.add( "丫丫" ); list.add(100); list.add( true ); //对数据进行使用 for ( int i = 0; i <list.size() ; i++) { //如果我们将获取到的每个元素转换成String类型的时候,会出现类型转换异常ClassCastException String str = (String)list. get (i); System. out .println(str); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | package com.genericity; import java.util.ArrayList; /** * 泛型产生的背景 */ public class MainClass { public static void main(String[] args) { ArrayList list = new ArrayList<>(); list.add( "丫丫" ); list.add(100); list.add( true ); list.add( new Student(1,0, "小丫丫" )); //对数据进行使用 for ( int i = 0; i <list.size() ; i++) { System. out .println(list. get (i)); } } } class Student{ private int stuNo; private int stuSex; private String stuName; public Student( int stuNo, int stuSex,String stuName){ this .stuNo=stuNo; this .stuSex=stuSex; this .stuName=stuName; } public int getStuNo() { return stuNo; } public void setStuNo( int stuNo) { this .stuNo = stuNo; } public int getStuSex() { return stuSex; } public void setStuSex( int stuSex) { this .stuSex = stuSex; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this .stuName = stuName; } //这里我没有重写set和get方法 } |
运行结果:
丫丫
100
true
com.genericity.Student@4dc63996
我们能够看到,实体类我们在使用的时候,打印出来的是对象的内存地址,如果我们想要使用这个对象,就需要对这个对象进行单独的处理。这里我们需要思考一下,如果我们的list集合中很多个不同的实体类对象,那么我们就要进行很多个不同的单独的处理,这是非常麻烦的。这个时候我们就有必要引入泛型。
使用泛型的小案例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | package com.genericity; import java.util.ArrayList; public class Test { public static void main(String[] args) { /** * 泛型:提供了编译器功能的类型检查,减少了数据类型转换 * ArrayList<Integer> strList = new ArrayList<>(); * strList.add("a");//申明的泛型是Integer类型的,如果我们添加String类型的数据,就会报错 */ ArrayList<String> strList = new ArrayList<>(); strList.add( "a" ); strList.add( "b" ); strList.add( "c" ); for ( int i = 0; i < strList.size(); i++) { System. out .println(strList. get (i)); } System. out .println( "===========================" ); ArrayList<Integer> intList= new ArrayList<>(); intList.add(100); intList.add(200); intList.add(300); for ( int i = 0; i < intList.size(); i++) { int num = intList. get (i); System. out .println(num); } } } |
2.概念:
Java泛型(generics)是JDK5中引入的一个新特性,泛型提供了编译时类型安全监测机制,该机制允许我们在编译时检测到非法的类型数据结构。泛型的本质就是参数化类型,也就是所操作的数据类型被指定为一个参数。
3.好处:
-
类型安全
-
消除了强制类型的转换
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
2019-12-08 mybatis框架-resultMap的自动映射级别-partial 和full的探讨
2019-12-08 mybatis框架-使用resultMap实现高级结果映射,collection属性的使用
2019-12-08 mybatis框架-使用resultMap实现高级结果映射,association属性
2019-12-08 前端知识--mulline框架,返回到前一个页面,并且刷新前一个页面