以前有碰到Integer“==”和Integer“equals”的问题,但是都没有总结,有时开发时也忘记注意了
最近有碰到这个问题了,发现是一个容易忽略的注意点,所以稍微整理了一下
看下面这段代码:
- /**
- * @author wuliwei
- *
- */
- public class IntegerEqualsTest {
- /**
- * @param args
- */
- public static void main(String[] args) {
- Integer i1 = 2;
- Integer i2 = Integer.valueOf(2);
- Integer i3 = Integer.valueOf(2);
- Integer i4 = Integer.valueOf("2");
- Integer i5 = Integer.valueOf("2");
- System.out.println("test ==");
- System.out.println("i1 == i2 ? " + (i1 == i2));
- System.out.println("i1 == i4 ? " + (i1 == i4));
- System.out.println("i2 == i3 ? " + (i2 == i3));
- System.out.println("i2 == i4 ? " + (i2 == i4));
- System.out.println("i4 == i5 ? " + (i4 == i5));
- System.out.println("test equals");
- System.out.println("i1.equals(i2) ? " + i1.equals(i2));
- System.out.println("i1.equals(i4) ? " + i1.equals(i4));
- System.out.println("i2.equals(i3) ? " + i2.equals(i3));
- System.out.println("i2.equals(i4) ? " + i2.equals(i4));
- System.out.println("i4.equals(i5) ? " + i4.equals(i5));
- }
- }
先来分析下这段代码的对象创建过程:
Integer i1 = 2; 这条语句创建了一个整型对象
Integer i2 = Integer.valueOf(2); 这条语句不创建新的对象,i2和i1引用同一个对象
Integer i3 = Integer.valueOf(2); 这条语句不创建新的对象,i3和i1引用同一个对象
Integer i4 = Integer.valueOf("2"); 这条语句创建了一个整型对象
Integer i5 = Integer.valueOf("2"); 这条语句创建了一个整型对象
根据对象的创建过程,很明了,程序运行结果为:
- test ==
- i1 == i2 ? true
- i1 == i4 ? false
- i2 == i3 ? true
- i2 == i4 ? false
- i4 == i5 ? false
- test equals
- i1.equals(i2) ? true
- i1.equals(i4) ? true
- i2.equals(i3) ? true
- i2.equals(i4) ? true
- i4.equals(i5) ? true
感觉很简单,但实际的开发过程中,如果不注意,判断两个Integer相等就直接用“==”,但是在如今这个开源框架的天下,对象的创建很多时候并不是我们想的那样,所以类似Integer这种基础数据类型的相等,也应该跟String判断相等一样,用equals。
但是要注意下,如果是STRING类型的与整形用EQUALS判断结果是FALSE,主意类型。
相关推荐
equals()和hashcode()这两个方法都是从object类中继承过来的。当String 、Math、还有Integer、Double。。。。等这些封装类在使用equals()方法时,已经覆盖了object类的equals()方法.
举例分析 equals 和 hashcode 方法,hashcode应该怎么样生成 8个基本类型与基本对象的比较:byte与Byte shot与Short int与Integer long与Long float与Float double与Double char与Character
Integer id = Integer.parseInt(parser.getAttributeValue(0)); currentPerson.setId(id); } if (currentPerson != null) { if ("name".equals(parser.getName())) { String name = ...
//金额里面的角和分 private static final String[] jf={ "角","分" }; /** * 处理金额的整数部分,返回"...圆整" * @param integer * @return String * @throws Exception */ public static ...
regexNew.equals("")) { regex = regexNew; } Pattern expression = Pattern.compile(regex); Matcher matcher = expression.matcher(source); TreeMap, Integer> myTreeMap = new TreeMap, Integer>(); ...
基本数据类型是可以用“==”进行比较,而引用数据类型则不可以,一般是通过equals方法来实现比较。 二 、再说说基本数据类型与引用数据类型的区别? 特点: 1、从概念方面来说 基本数据类型:变量名指向具体的...
TreeMap<Integer,List<OperationLog>> genMap = new TreeMap<Integer,List<OperationLog>>(); String id = "id1"; OperationLog log1 = new OperationLog(); log1.setId(id); log1.setTargetNum(...
private Integer showCount = 10; //在页面上显示多少个链接 private Integer spanWidth = 50; public static final String TEXT = "background-color:blue;color:white;"; public int doEndTag(){ Body...
s.equals("") && tf2.getString().equals("")) { i = Integer.valueOf(s).intValue(); tf2.setString(i*2.20462 + ""); } else { tf1.setString(""); tf2.setString(""); } break; ...
if (Integer.parseInt(kk[0]) < Integer.parseInt(jj[0])) return "0"; else { double y = Double.parseDouble(kk[0]) + Double.parseDouble(kk[1]) / 60; double u = Double.parseDouble(jj[0]) + Double....
count=Integer.parseInt(rs.getString(1)); } } catch(Exception e){ e.printStackTrace(); } return count; } public static void day_number(String s1,String s2,int s3){ float days=0;...
return (o1.equals(o2)? 0 : (o1.booleanValue()==true?1:-1)); } public int compare(Person o1, Person o2) { String firstname1 = o1.getFirstName(); String firstname2 = o2.getFirstName(); String ...
if(canshu.equals("tianjia")){ String gysname=request.getParameter("proName"); String gysmiaoshu=request.getParameter("proDesc"); String gyslianxiren=request.getParameter("contact"); ...
因此,对两个对象的实际内容进行比较时,必须使用所有对象都适用的特殊方法equals()。 看下面一个例子: public class EqualsMethod{ public static void main(String[] strs){ Integer n1 = new ...
实现getFirst() , getSecond() , equals()和hashCode()方法以及static工厂方法of() 。 构造函数必须是私有的。 使用正确实现的Pair类,以下代码应可以编译并成功运行: Pair< Integer> pair = Pair . of( 1 , " ...
实验1 三角形、梯形和圆形的类封装 9 实验2 实例成员与类成员 12 实验3 使用package语句与import语句 13 上机实践4 继承与接口 15 实验1 继承 15 实验2 上转型对象 17 实验3 接口回调 18 上机实践5 字符串、时间与...
Foo".equals(result); Pretty simple stuff. But what if we want to inject a bunch of variables? MVEL supports that too, and there is both and easy way and a more advanced way (dealing with resolvers ...
else if ("save".equals(action) || "update".equals(action)) { String sid = req.getParameter("id"); String[] answers = req.getParameterValues("choice"); List<Integer> list = new ArrayList...
Map<Integer, String> cityMap= getLocationCityInfo(); //记录匹配的城市的索引 int provinceIndex = -1; int cityIndex = -1; //传给处理类的数据封装对象 Bundle bundle = ...
if (a == null || "".equals(a) || "null".equals(a) || "NULL".equals(a)) { return true; } else { return false; } } public static boolean isNull(Integer a) { if (a == null) { return true; } ...