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) 派生类对基类 ...
随机推荐
- mac与windows通过ftp传输文件
1.两个系统相互传文件,比较通用的方式是用QQ,两台电脑一台各登陆一个qq,发文件就行了,在同一个网段时,qq会自动转换为按局域网的方式传输. 2.本人不愿安装qq,以ftp方式进行传输,先在wndo ...
- [WPF] 我的WPF自学日记1,无标题窗体拖动
学习WPF的第一天,尝试写比较常用的功能,无标题窗体拖动. 先在设计界面给它加上MouseDown事件 <Window x:Class="MyFirstWPFAPP.MainWindo ...
- nodejs 模块恩仇录
anywhere 一句话:随时随地将你的当前目录变成一个静态文件服务器的根目录. 安装 npm install anywhere -g anywhere -h localhost -p 8060 fa ...
- SQL——行值表达式(Row Value Expressions)
概述 最近接触了一个新概念——行值表达式,也叫做行值构造器.这是一个很强大的SQL功能,通常我们所操作的SQL表达式都只能针对一行中的单一字段进行操作比较,而行值表达式可以针对一行中的多个字段进行操作 ...
- Surface Shader简单向导
Surface Shader 表面着色器 描述 当你的Material要处理光照,则一般使用Surface Shader.Surface Shader隐藏了光照的计算,你只需要在surf函数里设置好反 ...
- 使用java反射机制编写Student类并保存
定义Student类 package org; public class Student { private String _name = null; ; ; public Student() { } ...
- 使用JDBC调用存储过程
DELIMITER $$ DROP PROCEDURE IF EXISTS `jdbc`.`addUser` $$ ),in birthday date,in money float,out pid ...
- TDDL分库分表规则
规则如下: 判断一个ID在哪个库里的公式是:id % 4 / 2判断一个ID在哪个表里的公式是:id % 4 % 2 其中4表示总共有多少个分表,2表示总共有多少个数据库:上面这个例子,表示总共有2个 ...
- UI控件(UITextField)
@implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UITextField* textField1 = ...
- [nRF51822] 11、基础实验代码解析大全 · 实验16 - 内部FLASH读写
一.实验内容: 通过串口发送单个字符到NRF51822,NRF51822 接收到字符后将其写入到FLASH 的最后一页,之后将其读出并通过串口打印出数据. 二.nRF51822芯片内部flash知识 ...