Int,Long比较重使用equal替换==
首先,==有很多限制,如Integer 类型的值在[-128,127] 期间,Integer 用 “==”是可以的(参考),超过范围则不行,那么使用equal则代替则完全ok
public static void main(String[] args) {
Long prop=4004L;
Long prop1=4004l;
Integer prop2=4004;
Integer prop3=4004;
if(prop2.equals(prop3)){
System.out.println("hi");
}else if(prop2==prop3){
System.out.println("wrong");
}
if(prop1.equals(prop2.longValue())){
System.out.println("helloworld");
}else if(prop==prop1){
System.out.println("you are wrong!");
}
返回结果
hi
helloworld
上面的示例说明使用"=="和equal还是有不小的区别的,equal可以替代==
Long源码如下:
/**
* Compares this object to the specified object. The result is
* <code>true</code> if and only if the argument is not
* <code>null</code> and is a <code>Long</code> object that
* contains the same <code>long</code> value as this object.
*
* @param obj the object to compare with.
* @return <code>true</code> if the objects are the same;
* <code>false</code> otherwise.
*/
public boolean equals(Object obj) {
if (obj instanceof Long) {
return value == ((Long)obj).longValue();
}
return false;
}
Integer源码如下:
/**
* Compares this object to the specified object. The result is
* <code>true</code> if and only if the argument is not
* <code>null</code> and is an <code>Integer</code> object that
* contains the same <code>int</code> value as this object.
*
* @param obj the object to compare with.
* @return <code>true</code> if the objects are the same;
* <code>false</code> otherwise.
*/
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
Int,Long比较重使用equal替换==的更多相关文章
- WebSocket安卓客户端实现详解(一)–连接建立与重连
http://blog.csdn.net/zly921112/article/details/72973054 前言 这里特别说明下因为WebSocket服务端是公司线上项目所以这里url和具体协议我 ...
- BFS以及hash表判重的应用~
主要还是讲下hash判重的问题吧 这道题目用的是除法求余散列方式 前几天看了下算法导论 由于我们用的是线性再寻址的方式来解决冲突问题 所以hash表的大小(余数的范围)要包含我们要求的范围 对mod的 ...
- const关键字也许该被替换为readonly
只读的变量,其值在编译时不能被使用,因为编译器在编译时不知道其存储的内容. const修饰的只读变量 const int Max = 100: int Array[Max] ; c文件中,编译 ...
- Scalaz(4)- typeclass:标准类型-Equal,Order,Show,Enum
Scalaz是由一堆的typeclass组成.每一个typeclass具备自己特殊的功能.用户可以通过随意多态(ad-hoc polymorphism)把这些功能施用在自己定义的类型上.scala这个 ...
- C# 二进制替换第一弹 byte 数组替换
在做通讯相关的数据操作时常常须要用到 byte[] byte 数组替换操作.比方协义转换相关的 如今提供了几种替换的方法 /// <summary> /// 二进制数据 操作 /// ...
- C 语言实现字符串替换
void replaceFirst(char *str1,char *str2,char *str3) { ]; char *p; strcpy(str4,str1); if((p=strstr(st ...
- 介绍一种很棒的wince 如何替换系统声音的方法
Topic:介绍一种很棒的wince 如何替换系统声音的方法(作者:Baiduluckyboy) //------------------------------------------------- ...
- MySQL 库名重命名
MySQL ( RENAME database olddbname TO newdbname ) 对库名的重命名上会出现一些奇怪的错误.有丢失数据的风险. 所以如何去重命名呢: 1 用mysqldu ...
- C++ 重定义、重载、覆盖
想要用好C++继承和类自身函数实现就必须了解C++得三个概念重定义(redefine).重载(overload).重写(override). 一 基本感念 1 重定义(redefine) 派生类对基类 ...
随机推荐
- NFSv4的引用,迁移和备份(用户手册 v0.2)
RFC3530 定义了NFS文件系统迁移和引用的管理机制.文件系统定位功能通过fs_location属性向客户端提供文件系统的位置信息.fs_location属 性是一个包含有位置信息的列表,位置信息 ...
- *HDU3398 数学
String Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- bfs判断连通图(无向)
在图论中,连通图基于连通的概念.在一个无向图 G 中,若从顶点vi到顶点vj有路径相连(当然从vj到vi也一定有路径),则称vi和vj是连通的.如果 G 是有向图,那么连接vi和vj的路径中所有的边都 ...
- mysql如何在一个字段后面加个字符?
update city set 字段=concat(字段,'内容');
- Android再学习
1.点击事件的几种实现方式 Button Btn1 = (Button)findViewById(R.id.button1);//获取按钮资源 Btn1.setOnClickListener(new ...
- ubuntu1404安装搜狗输入法后出现黑框的问题
1.安装xcompmgr sudo apt-get install xcompmgr 2.设置xcompmgr自动启动 mkdir-/.config/autostart vim xcompmgr. ...
- Hadoop-1.2.1 升级到Hadoop-2.6.0 HA
Hadoop-1.2.1到Hadoop-2.6.0升级指南 作者 陈雪冰 修改日期 2015-04-24 版本 1.0 本文以hadoop-1.2.1升级到hadoop-2.6.0 Z ...
- #IrrlichtEngine# Example1 HelloWorld
配置IDE环境来使用irrlicht引擎: (VS2012下)菜单栏项目 -> 项目属性窗口下 C/C++ -> 常规 -> 附加包含目录中添加irrlicnt引擎文件目录下incl ...
- Dropbox创造共享新思维——Datastore API
7月9日,第一届Dropbox开发者大会上,Dropbox发布了Datastore API的beta版本,通过这个API,原始的结构化数据可以在多个设备间的Dropbox内同步.CEO Drew Ho ...
- eventbus 备注
Event在整个系统中是单例的. EventBus.getDefault().register(this); 注册 EventBus.getDefault().unregister(this); 注销 ...