`
lxy2330
  • 浏览: 460872 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Integer“==”和Integer“equals”

    博客分类:
  • java
阅读更多

以前有碰到Integer“==”和Integer“equals”的问题,但是都没有总结,有时开发时也忘记注意了

 

最近有碰到这个问题了,发现是一个容易忽略的注意点,所以稍微整理了一下

 

看下面这段代码:

 

Java代码  收藏代码
  1. /** 
  2.  * @author wuliwei 
  3.  * 
  4.  */  
  5. public class IntegerEqualsTest {  
  6.     /** 
  7.      * @param args 
  8.      */  
  9.     public static void main(String[] args) {  
  10.         Integer i1 = 2;  
  11.         Integer i2 = Integer.valueOf(2);  
  12.         Integer i3 = Integer.valueOf(2);  
  13.         Integer i4 = Integer.valueOf("2");  
  14.         Integer i5 = Integer.valueOf("2");  
  15.           
  16.         System.out.println("test ==");  
  17.           
  18.         System.out.println("i1 == i2 ? " + (i1 == i2));  
  19.         System.out.println("i1 == i4 ? " + (i1 == i4));  
  20.         System.out.println("i2 == i3 ? " + (i2 == i3));  
  21.         System.out.println("i2 == i4 ? " + (i2 == i4));  
  22.         System.out.println("i4 == i5 ? " + (i4 == i5));  
  23.           
  24.         System.out.println("test equals");  
  25.   
  26.         System.out.println("i1.equals(i2) ? " + i1.equals(i2));  
  27.         System.out.println("i1.equals(i4) ? " + i1.equals(i4));  
  28.         System.out.println("i2.equals(i3) ? " + i2.equals(i3));  
  29.         System.out.println("i2.equals(i4) ? " + i2.equals(i4));  
  30.         System.out.println("i4.equals(i5) ? " + i4.equals(i5));  
  31.     }  
  32.   
  33. }  

 

先来分析下这段代码的对象创建过程:

 

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");        这条语句创建了一个整型对象

 

根据对象的创建过程,很明了,程序运行结果为:

 

Java代码  收藏代码
  1. test ==  
  2. i1 == i2 ? true  
  3. i1 == i4 ? false  
  4. i2 == i3 ? true  
  5. i2 == i4 ? false  
  6. i4 == i5 ? false  
  7. test equals  
  8. i1.equals(i2) ? true  
  9. i1.equals(i4) ? true  
  10. i2.equals(i3) ? true  
  11. i2.equals(i4) ? true  
  12. i4.equals(i5) ? true  

 

感觉很简单,但实际的开发过程中,如果不注意,判断两个Integer相等就直接用“==”,但是在如今这个开源框架的天下,对象的创建很多时候并不是我们想的那样,所以类似Integer这种基础数据类型的相等,也应该跟String判断相等一样,用equals。

但是要注意下,如果是STRING类型的与整形用EQUALS判断结果是FALSE,主意类型。

分享到:
评论

相关推荐

    hashcode和equals方法

    equals()和hashcode()这两个方法都是从object类中继承过来的。当String 、Math、还有Integer、Double。。。。等这些封装类在使用equals()方法时,已经覆盖了object类的equals()方法.

    java 举例分析 equals hashcode 基本类型与基本对象的比较 shot与Short int与Integer long与Long

    举例分析 equals 和 hashcode 方法,hashcode应该怎么样生成 8个基本类型与基本对象的比较:byte与Byte shot与Short int与Integer long与Long float与Float double与Double char与Character

    android pull 解析 源码 android2.2

    Integer id = Integer.parseInt(parser.getAttributeValue(0)); currentPerson.setId(id); } if (currentPerson != null) { if ("name".equals(parser.getName())) { String name = ...

    人民币小写转大写(Java 实现)

    //金额里面的角和分 private static final String[] jf={ "角","分" }; /** * 处理金额的整数部分,返回"...圆整" * @param integer * @return String * @throws Exception */ public static ...

    java统计字符串出现次数算法--StringCounter(算法源码)

    regexNew.equals("")) { regex = regexNew; } Pattern expression = Pattern.compile(regex); Matcher matcher = expression.matcher(source); TreeMap, Integer> myTreeMap = new TreeMap, Integer>(); ...

    Java中int与integer的区别(基本数据类型与引用数据类型)

     基本数据类型是可以用“==”进行比较,而引用数据类型则不可以,一般是通过equals方法来实现比较。 二 、再说说基本数据类型与引用数据类型的区别? 特点: 1、从概念方面来说  基本数据类型:变量名指向具体的...

    fusionCharts dragnode demo

    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...

    实验二 使用MIDP用户界面API编程.doc

    s.equals("") && tf2.getString().equals("")) { i = Integer.valueOf(s).intValue(); tf2.setString(i*2.20462 + ""); } else { tf1.setString(""); tf2.setString(""); } break; ...

    java时间格式大全(算法源码)

    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....

    java 数据库 课程设计 图书馆管理信息系统

    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;...

    java对象比较器

    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 ...

    北大青鸟U2项目完全版

    if(canshu.equals("tianjia")){ String gysname=request.getParameter("proName"); String gysmiaoshu=request.getParameter("proDesc"); String gyslianxiren=request.getParameter("contact"); ...

    Java中对象的等价性比较

    因此,对两个对象的实际内容进行比较时,必须使用所有对象都适用的特殊方法equals()。  看下面一个例子: public class EqualsMethod{  public static void main(String[] strs){  Integer n1 = new ...

    jv-pair

    实现getFirst() , getSecond() , equals()和hashCode()方法以及static工厂方法of() 。 构造函数必须是私有的。 使用正确实现的Pair类,以下代码应可以编译并成功运行: Pair< Integer> pair = Pair . of( 1 , " ...

    Java 2实用教程(第三版)实验指导与习题解答

    实验1 三角形、梯形和圆形的类封装 9 实验2 实例成员与类成员 12 实验3 使用package语句与import语句 13 上机实践4 继承与接口 15 实验1 继承 15 实验2 上转型对象 17 实验3 接口回调 18 上机实践5 字符串、时间与...

    mvel 2.0.15

    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 ...

    投票系统,基于mvc开发,jsp+servlet+mysql

    else if ("save".equals(action) || "update".equals(action)) { String sid = req.getParameter("id"); String[] answers = req.getParameterValues("choice"); List<Integer> list = new ArrayList...

    Android天气预报实验报告

    Map<Integer, String> cityMap= getLocationCityInfo(); //记录匹配的城市的索引 int provinceIndex = -1; int cityIndex = -1; //传给处理类的数据封装对象 Bundle bundle = ...

    Java工具类

    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; } ...

Global site tag (gtag.js) - Google Analytics