Java实体类不要使用基本类型_为何封装javabean时,成员变量一律都不用基本类型

x33g5p2x  于2022-05-24 转载在 Java  
字(2.3k)|赞(0)|评价(0)|浏览(470)

Java实体类不要使用基本类型

记录一下,在项目中因为基本类型,所产生的bug

包装类:8种基本类型的包装类

应用场景:数据库建立实体映射多用包装类

这两句话是重点:就是建立实体类禁止使用基本数据量类型!!!而用对应的包装类,

为什么呢,看以下场景。

  1. /**
  2. * 8中基本类型的对应包装类'
  3. * byte short int long double float boolean char
  4. * Byte Short Integer Long Double Float Boolean Character
  5. * 区别:(举例int,其余相同)
  6. * 1、int默认为0,integer默认为null
  7. * 2、int是java的基本数据类型,integer是int的包装类
  8. * 3、integer必须new,int直接使用
  9. */
  10. /**
  11. * 场景一:
  12. * 创建对应数据库的实体类字段
  13. * 1、创建一个类型(type),对应数据库的一个字段
  14. * 2、注意:此存在严重问题,基本类型都默认有值。如int 默认为0
  15. * 3、那在进行数据库新增的时候,如果不填,则会默认为0。
  16. * 4、会产生严重的bug,应该改为包装类的引用类型
  17. */
  18. //错误示范
  19. private int type;//代表类型
  20. //正确,设置为integer类型
  21. private Integer typeT;

所以,多用包装类进行赋值。

补充:

  1. /**
  2. * 场景二:
  3. * 自动装箱And自动拆箱
  4. */
  5. private void testBox() {
  6. //原本转换方式
  7. int t = 10;
  8. Integer ct = new Integer(t);
  9. int tt = ct.intValue();
  10. int i = 10;
  11. //自动装
  12. Integer c = i;
  13. //自动拆
  14. int ic = c;
  15. }

看一个题,如下,为什么一个为true,一个为false??

  1. /**
  2. * 自动装拆箱
  3. */
  4. public static void main(String[] args) {
  5. Integer integer0 = 127;
  6. Integer integer1 = 127;
  7. System.out.println(integer0 == integer1);//等于true
  8. Integer integer2 = 128;
  9. Integer integer3 = 128;
  10. System.out.println(integer2 == integer3);//等于false
  11. }
  12. /** 源码
  13. * public static Integer valueOf(int i) {
  14. * if (i >= Integer.IntegerCache.low && i <= Integer.IntegerCache.high)
  15. * return Integer.IntegerCache.cache[i + (-Integer.IntegerCache.low)];
  16. * return new Integer(i);
  17. * }
  18. * 通过上我们发现,如果他的int值在最高和最低之间,他直接返回cache内的数据
  19. * 否则, new Integer(i);
  20. * 那么最高值:?=high 127 ,最低值:?=low -128,
  21. * 所以:在-128至127内,他们引用的是缓存内的数据,地址相同,所以为true。超过此则为false
  22. *
  23. * private static class IntegerCache {
  24. * static final int low = -128;
  25. * static final int high;
  26. * static final Integer cache[];
  27. *
  28. * static {
  29. * // high value may be configured by property
  30. * int h = 127;
  31. * String integerCacheHighPropValue =
  32. * sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
  33. * if (integerCacheHighPropValue != null) {
  34. * try {
  35. * int i = parseInt(integerCacheHighPropValue);
  36. * i = Math.max(i, 127);
  37. * // Maximum array size is Integer.MAX_VALUE
  38. * h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
  39. * } catch( NumberFormatException nfe) {
  40. * // If the property cannot be parsed into an int, ignore it.
  41. * }
  42. * }
  43. * high = h;
  44. *
  45. * cache = new Integer[(high - low) + 1];
  46. * int j = low;
  47. * for(int k = 0; k < cache.length; k++)
  48. * cache[k] = new Integer(j++);
  49. *
  50. * // range [-128, 127] must be interned (JLS7 5.1.7)
  51. * assert IntegerCache.high >= 127;
  52. * }
  53. *
  54. * private IntegerCache() {}
  55. * }
  56. *
  57. */

为何封装javabean时,成员变量一律都不用基本类型

Long和Integer两个包装类所声明的是对象得引用,产生的变量是以对象的身份出现,而long和int是两个原型的数据修饰类型,所产生的只是一个值,而不是一个对象。

Long和long的区别是,Long可能为null,long最少也是0。
在数据库操作的时候,null和0是有区别的。

JAVA是面向对象得,JAVABEAN作为一种规范使用。所以在习惯上会把它属性都包装成对象。记住。这本身就是一种规范。

相关文章

最新文章

更多