java中的Integer和Long值类型问题
来源:互联网 发布:php时间戳转换成时间 编辑:程序博客网 时间:2024/05/18 07:03
long validDay = 1;List<Integer> dayOfNeedHint = new ArrayList<Integer>(Arrays.asList(1,2,3,5,7)); if(null != use_type && use_type == 2 && dayOfNeedHint.contains(valid_dd)){ //if判断为true}
上面的代码的条件永远也不会为true,原因为dayOfNeedHint
的contains方法会调用Integer
的equals
方法 ,下为Integer
的 equals
方法源码:
public boolean equals(Object obj) { if (obj instanceof Integer) { return value == ((Integer)obj).intValue(); } return false;}
可见只要equals方法的参数类型不是Integer
(Integer
的子类也可以,但可惜Integer
是final
类),那么就会返回false。
所以用一个Integer.equals(Long)
,永远也不会为true…
改进方法为
- 把dayOfNeedHint的泛型和validDay的类型进行统一
- 在调用List.contains(Object)的时候把validDay强转为long
- 对validDay调用Long.intvalue()方法
/** * Returns the value of this <code>Long</code> as an * <code>int</code>. */public int intValue() { return (int)value;}
注:
不要对值包装类型的变量使用 ==
,因为会很危险(以Long举例):
public static void testValueType(){ Long long1 = 20L; Long long2 = 20L; Long long3 = new Long(20L); Long long4 = new Long(20L); long long5 = 20L; long long6 = 20L; System.out.println(long1 == long2);//true //1 System.out.println(long1 == long3);//false //2 System.out.println(long3 == long4);//false System.out.println(long1 == long5);//true System.out.println(long3 == long5);//true //3 System.out.println(long5 == long6);//true //4 System.out.println("************"); Long long12 = 2000L; Long long22 = 2000L; Long long32 = new Long(2000L); Long long42 = new Long(2000L); long long52 = 2000L; long long62 = 2000L; System.out.println(long12 == long22);//false System.out.println(long12 == long32);//false System.out.println(long32 == long42);//false System.out.println(long12 == long52);//true System.out.println(long32 == long52);//true System.out.println(long52 == long62);//true System.out.println("************"); int i = 20; float f = 20.000000f; System.out.println(long5 == i);//true System.out.println(f == i);//true System.out.println(long1 == i);//true }
对于注释1,编译器自动装箱,调用jdk的Long.valueOf(20L) 因为运行时jdk会将范围为-128到127的Long类型值缓存在内存中,而valueOf方法的源码如下:
public static Long valueOf(long l) { final int offset = 128; if (l >= -128 && l <= 127) { // will cache return LongCache.cache[(int)l + offset]; } return new Long(l);}
会从内存的缓存中获取数据,因此long1和long2返回的时相同的引用。
对于注释2,long3由于是使用的new运算符,会在内存中新开辟一块内存,因此返回的引用肯定和jdk运行时缓存的Long对象引用不同。
而对于注释3和注释4,只要 ==
运算符两侧有基本数据类型出没,那么另一侧的包装类数据类型就会自动拆箱为对应的基本类型。因此比较的总是数值。
而对于值不在-128到127的Long,编译器会调用Long.valueOf自动装箱为Long对象。
0 0
- java中的Integer和Long值类型问题
- Integer类型和Long类型
- java 类型转换 Long double String Integer
- C++中的long long和__int64类型
- C++中的long long和__int64类型
- C++中的long long和__int64类型
- C++中的long long和__int64类型
- 唯爱小粽子:Java中的long类型和Long类型比较大小
- JAVA中的类型和对象(Integer和Int)
- Map key为Integer类型时用Long值取value问题
- java中的值类型int与引用类型integer
- java类型转换 Integer String Long Float Double Date
- java类型转换 Integer String Long Float Double Date
- java类型转换 Integer String Long Float Double Date
- java long类型报错:error: integer number too large
- java中Long和Integer在hashmap判断key是否存在的问题
- java中Long类型和long类型的大小比较
- C++中的long long和__int64类型(转载)
- 一些有趣的js正则表达式
- eclipse中Junit报错总结(1)
- Quartz 起步
- USACO 3.4解题报告
- 一个简单的进度条
- java中的Integer和Long值类型问题
- 快牙网传——轻松传文件
- ZCMU—1871
- 医学成像技术简介(CT,MRI等)及相关论坛、技术应用链接
- 架构之路之spring+redis的集成
- sse转neon
- 一行代码解决各种IE兼容问题,IE6,IE7,IE8,IE9,IE10
- C++ STL的迭代器(iterator)
- 16. 3Sum Closest