Java实体类不要使用基本类型的知识点总结
作者:幽暗森林之猪大屁 时间:2023-02-21 10:04:49
今天来记录一下,在项目中因为基本类型,所产生的bug
包装类:8种基本类型的包装类
应用场景:数据库建立实体映射多用包装类
这两句话是重点:就是建立实体类禁止使用基本数据量类型!!!而用对应的包装类,
为什么呢,看以下场景。
JAVA代码
<font style="color:rgb(77, 77, 77)"><font face="""><font style="font-size:16px">/**
* 8中基本类型的对应包装类'
* byte short int long double float boolean char
* Byte Short Integer Long Double Float Boolean Character
* 区别:(举例int,其余相同)
* 1、int默认为0,integer默认为null
* 2、int是java的基本数据类型,integer是int的包装类
* 3、integer必须new,int直接使用
*/
/**
* 场景一:
* 创建对应数据库的实体类字段
* 1、创建一个类型(type),对应数据库的一个字段
* 2、注意:此存在严重问题,基本类型都默认有值。如int 默认为0
* 3、那在进行数据库新增的时候,如果不填,则会默认为0。
* 4、会产生严重的bug,应该改为包装类的引用类型
*/
//错误示范
private int type;//代表类型
//正确,设置为integer类型
private Integer typeT;
</font></font></font>
所以,多用包装类进行赋值。
补充:
<font style="color:rgb(77, 77, 77)"><font face="""><font style="font-size:16px">/**
* 场景二:
* 自动装箱And自动拆箱
*/
private void testBox() {
//原本转换方式
int t = 10;
Integer ct = new Integer(t);
int tt = ct.intValue();
int i = 10;
//自动装
Integer c = i;
//自动拆
int ic = c;
}
</font></font></font>
笔试题题如下?为什么一个为true,一个为false???
<font style="color:rgb(77, 77, 77)"><font face="""><font style="font-size:16px">/**
* 自动装拆箱
*/
public static void main(String[] args) {
Integer integer0 = 127;
Integer integer1 = 127;
System.out.println(integer0 == integer1);//等于true
Integer integer2 = 128;
Integer integer3 = 128;
System.out.println(integer2 == integer3);//等于false
/** 源码
* public static Integer valueOf(int i) {
* if (i >= Integer.IntegerCache.low && i <= Integer.IntegerCache.high)
* return Integer.IntegerCache.cache[i + (-Integer.IntegerCache.low)];
* return new Integer(i);
* }
* 通过上我们发现,如果他的int值在最高和最低之间,他直接返回cache内的数据
* 否则, new Integer(i);
* 那么最高值:?=high 127 ,最低值:?=low -128,
* 所以:在-128至127内,他们引用的是缓存内的数据,地址相同,所以为true。超过此则为false
*
* private static class IntegerCache {
* static final int low = -128;
* static final int high;
* static final Integer cache[];
*
* static {
* // high value may be configured by property
* int h = 127;
* String integerCacheHighPropValue =
* sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
* if (integerCacheHighPropValue != null) {
* try {
* int i = parseInt(integerCacheHighPropValue);
* i = Math.max(i, 127);
* // Maximum array size is Integer.MAX_VALUE
* h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
* } catch( NumberFormatException nfe) {
* // If the property cannot be parsed into an int, ignore it.
* }
* }
* high = h;
*
* cache = new Integer[(high - low) + 1];
* int j = low;
* for(int k = 0; k < cache.length; k++)
* cache[k] = new Integer(j++);
*
* // range [-128, 127] must be interned (JLS7 5.1.7)
* assert IntegerCache.high >= 127;
* }
*
* private IntegerCache() {}
* }
*
*/
}</font></font></font>
来源:https://www.cnblogs.com/zhuxiaopijingjing/p/12258423.html
标签:Java,包装类,实体类
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
IDEA 2022 中的Lombok 使用基础教程
2023-04-09 21:57:09
![](https://img.aspxhome.com/file/2023/7/64547_0s.png)
java实现单人版五子棋游戏
2021-09-03 03:24:20
![](https://img.aspxhome.com/file/2023/0/63060_0s.png)
Java用邻接矩阵存储图的示例代码
2021-10-05 21:39:18
![](https://img.aspxhome.com/file/2023/1/64171_0s.png)
关于idea中SpringBoot启动失败的坑
2022-07-18 13:02:24
![](https://img.aspxhome.com/file/2023/4/62234_0s.png)
浅谈Java slf4j日志简单理解
2021-07-07 15:49:15
C#实现截图工具小项目
2023-10-02 08:20:03
Java异常处理try catch的基本用法
2022-11-27 11:36:15
![](https://img.aspxhome.com/file/2023/2/62402_0s.jpg)
java反射之Method的invoke方法实现教程详解
2023-07-09 20:24:36
![](https://img.aspxhome.com/file/2023/2/66632_0s.png)
关于Java中修饰符的总结(fina除外)
2023-11-22 23:15:57
SpringCloud微服务架构实战之微服务治理功能的实现
2023-07-20 09:06:38
![](https://img.aspxhome.com/file/2023/9/57609_0s.jpg)
Java基础之容器Vector详解
2023-11-25 13:10:07
Java文件快速copy复制实例代码
2021-05-27 12:25:22
![](https://img.aspxhome.com/file/2023/7/66077_0s.png)
SpringBoot2.0解决Long型数据转换成json格式时丢失精度问题
2022-10-31 16:56:24
![](https://img.aspxhome.com/file/2023/1/61521_0s.png)
SpringBoot部署在tomcat容器中运行的部署方法
2023-08-04 13:02:28
java实现银行ATM管理系统
2023-10-30 14:50:51
SpringBoot整合activemq的案例代码
2023-11-06 18:41:37
![](https://img.aspxhome.com/file/2023/9/58589_0s.png)
深入浅析Java反射机制
2023-11-25 07:02:03
解决@Transactional注解事务不回滚不起作用的问题
2022-10-29 09:18:51
BaseJDBC和CRUDDAO的写法实例代码
2022-09-03 14:13:33
关于springboot配置文件密文解密方式
2023-11-09 04:21:24
![](https://img.aspxhome.com/file/2023/8/59418_0s.png)