用List举例子,add添加的是对象,那么基本数据类型就不能添加进去,比如int,byte,short,long,double float,boolean,char.这些都是基本数据类型,不是对象。所以不能添加到集合中。
![abb362bd8dfc3ca37a338b262f40a00a.png](https://img-blog.csdnimg.cn/img_convert/abb362bd8dfc3ca37a338b262f40a00a.png)
但是实际上是可以添加进去的,为什么,因为添加进去的时候,自动把这些基本数据类型转成了object类型的包装类,integer之类的。就不说了。
![548a3c1bced453865ff1d83517ccafe6.png](https://img-blog.csdnimg.cn/img_convert/548a3c1bced453865ff1d83517ccafe6.png)
![67539e9195bcc1a4a7d3b90d18557827.png](https://img-blog.csdnimg.cn/img_convert/67539e9195bcc1a4a7d3b90d18557827.png)
翻一下源码,也是一个构造方法
![e9ecae2b2043f1ba132ececf560e8487.png](https://img-blog.csdnimg.cn/img_convert/e9ecae2b2043f1ba132ececf560e8487.png)
自动装箱,下图就是自动装箱,1是int类型的,装成了integer
![086befbd9e37c96a16c83c4a4f739e04.png](https://img-blog.csdnimg.cn/img_convert/086befbd9e37c96a16c83c4a4f739e04.png)
自动拆箱,把包装类拆了给int
![164ea7104d882d9aedc793835a7ab0c4.png](https://img-blog.csdnimg.cn/img_convert/164ea7104d882d9aedc793835a7ab0c4.png)
包装类是一个类,是引用数据类型,是有方法的,而基本数据类型不能打点访问方法