【java基础 15】java代码中“==”和equals的区别
导读:昨夜闲来无事,和贾姑娘聊了聊java基础,然后就说到了这个“==”和equals的问题,我俩都是以前了解过,也常用这个,但是,昨天说到的时候,又乱了,什么比较地址值,什么判断是否同一个对象,基本数据类型啥的,谁都没个准儿,后来写了点代码验证了一番,今儿个写此博客,纪念和好朋友一起探讨学习的经历!PS:我一直知道我这一路走来,受好朋友的恩惠太多了!
一、三组示例代码
1.1,String类(引用类型)
String str1="test";
String str2="test";
//true true
System.out.println(str1==str2);
System.out.println(str1.equals(str2));
String str3=new String("test");
String str4=new String("test");
//false true
System.out.println(str3==str4);
System.out.println(str3.equals(str4));
String str5=str1;
//true true
System.out.println(str5==str1);
System.out.println(str5.equals(str1));
String str6=str3;
//false true
System.out.println(str6==str1);
System.out.print(str6.equals(str1));
刚开始,统一的意见是“==”是比较值,equals是比较引用对象,请看下面的经典论断(主要是针对str3和str4):
A:==比值,这两个值都是test,应该是true,可结果是false,后面那个equals比较对象,两个都是new的,应该不一样,可结果是true
B:==比值,因为这两个不是简单类型,无法比较,所以返回false;equals 比较对象,此时str3 和str4新创建了两个字符串对象,这两个对象是一样的,返回true
A:如果是byte、int、boolean、long,float,这些的话,==就是比较值,equals比对象,是吧
B:对
1.2,Integer类(int的包装类,非8种基本类型)
Integer int1=1;
Integer int2=1;
//true true
System.out.println(int1==int2);
System.out.println(int1.equals(int2)); Integer int3=new Integer(1);
Integer int4=new Integer(1);
//false true
System.out.println(int3==int4);
System.out.println(int3.equals(int4)); Integer int5=int1;
//true true
System.out.println(int5==int1);
System.out.println(int5.equals(int1)); Integer int6=int3;
//true true
System.out.println(int6==int3);
System.out.print(int6.equals(int3));
结果和String类一致!
1.3,int(基本类型)
备注:用简单基本类型,根本无法使用equals方法,只能用==,对于String类或者简单类型的包装类(Integer是int的包装类)==比较的是否是同一个地址(对象),equals比较的是地址值
二、分析equals方法
所有的对象都是继承于Object类,那么首先看Object里面,关于equals的定义:
* @param obj the reference object with which to compare.
* @return {@code true} if this object is the same as the obj
* argument; {@code false} otherwise.
* @see #hashCode()
* @see java.util.HashMap
*/
public boolean equals(Object obj) {
return (this == obj);
}
从这里可以看出,equals事实上比较的是否是同一个对象目标,也就是是否是同一个内存地址。但从这个角度来说,那么就无法解释为什么在代码段1中str3.equals(str4)的结果为true了。因为这两个对象都采用了new 关键字,在堆中,必定会存在两个空间地址。为了解决这个问题,势必要去看看String类对于equals方法的重写:
/**
* Compares this string to the specified object. The result is {@code
* true} if and only if the argument is not {@code null} and is a {@code
* String} object that represents the same sequence of characters as this
* object.
*
* @param anObject
* The object to compare this {@code String} against
*
* @return {@code true} if the given object represents a {@code String}
* equivalent to this string, {@code false} otherwise
*
* @see #compareTo(String)
* @see #equalsIgnoreCase(String)
*/
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
从代码可以看出,String类对于equals方法进行了改写,当String类使用equals的时候,if and only if the argument is not null and isa object that represents the same sequence of characters
as this object. 对比Object基类返回true的条件:if this object is the same as the obj argument。 现在再去看代码段1中的str3.equals(str4)的结果为true,就能理解了!
接下来,再看基本类型的包装类中对于equals方法的改写:
/**
* Compares this object to the specified object. The result is
* {@code true} if and only if the argument is not
* {@code null} and is an {@code Integer} object that
* contains the same {@code int} value as this object.
*
* @param obj the object to compare with.
* @return {@code true} if the objects are the same;
* {@code false} otherwise.
*/
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
if and only if the argument is not null and is an object that contains the same value as this object. 从这里可以看出,它的改写,并未要求是同一对象,而要求是同一值!
附:更为明显的代码片段
Integer intA=new Integer(1);
Integer intB=new Integer(1);
Integer intC=1;
//false false true true
System.out.println(intA==intB);
System.out.println(intA==intC);
System.out.println(intA.equals(intB));
System.out.println(intA.equals(intC));
三、总结
写这篇博客,主要是想纪念一下和好朋友之间的探讨交流,感觉和大家一起交流一些简单又有意思的事儿,真心很好玩。而且感觉我现在还比较喜欢看源码,也就来源于对这些小东西的兴趣和好奇。当然目前的学习程度是远远不够的,正在努力中。对了,以上关于equals的各类源码,属于java 8.
多看代码,多交流,多总结,希望自己保持下去,然后和大家一起成长!
【java基础 15】java代码中“==”和equals的区别的更多相关文章
- Java基础:Object类中的equals与hashCode方法
前言 这个系列的文章主要用来记录我在学习和复习Java基础知识的过程中遇到的一些有趣好玩的知识点,希望大家也喜欢. 一切皆对象 对于软件工程来说面向对象编程有一套完整的解决方案:OOA.OOD.O ...
- Java基础-使用JAVA代码剖析MD5算法实现过程
Java基础-使用JAVA代码剖析MD5算法实现过程 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.
- Java基础语法(8)-数组中的常见排序算法
title: Java基础语法(8)-数组中的常见排序算法 blog: CSDN data: Java学习路线及视频 1.基本概念 排序: 是计算机程序设计中的一项重要操作,其功能是指一个数据元素集合 ...
- 黑马程序员:Java基础总结----java注解
黑马程序员:Java基础总结 java注解 ASP.Net+Android+IO开发 . .Net培训 .期待与您交流! java注解 lang包中的基本注解 @SuppressWarnings ...
- java基础(二)-----java的三大特性之继承
在<Think in java>中有这样一句话:复用代码是Java众多引人注目的功能之一.但要想成为极具革命性的语言,仅仅能够复制代码并对加以改变是不够的,它还必须能够做更多的事情.在这句 ...
- Java基础技术-Java其他主题【面试】
Java基础技术-Java其他主题[面试] Java基础技术IO与队列 Java BIO.NIO.AIO Java 中 BIO.NIO.AIO 的区别是什么? 含义不同: BIO(Blocking I ...
- Java基础:Java的四种引用
在Java基础:java虚拟机(JVM)中,我们提到了Java的四种引用.包括:强引用,软引用,弱引用,虚引用.这篇博客将详细的讲解一下这四种引用. 1. 强引用 2. 软引用 3. 弱引用 4. 虚 ...
- java基础-学java util类库总结
JAVA基础 Util包介绍 学Java基础的工具类库java.util包.在这个包中,Java提供了一些实用的方法和数据结构.本章介绍Java的实用工具类库java.util包.在这个包中,Java ...
- Java基础15:深入剖析Java枚举类
更多内容请关注微信公众号[Java技术江湖] 这是一位阿里 Java 工程师的技术小站,作者黄小斜,专注 Java 相关技术:SSM.SpringBoot.MySQL.分布式.中间件.集群.Linux ...
随机推荐
- 关于SQL Server索引密度的知识
文章主要描述的是SQL Server索引密度(Index Densities),当一个查询的SARG 的值直到查询运行时才得以知晓,或是SARG是一个关于索引的多列时,SQL Server才使用为索引 ...
- SQL SERVER之填充因子
建SQL SERVER索引的时候有一个选项,即Fillfactor(填充因子). 这个可能很少人会去注意它,但它也是比较重要的.大家可能也都知道有这个东西,但是如何去使用它,可能会比较迷糊.另外,即使 ...
- python+selenium之处理HTML5的视频播放
from selenium import webdriver from time import sleep driver = webdriver.Firefox() driver.get(" ...
- Bot Framework:Activity类简明指南
Bot Framework相关文档:https://docs.botframework.com/en-us/csharp/builder/sdkreference/attachments.html B ...
- Erlang程序设计(第2版)读书笔记(一)
正如<代码的未来>中所说,为了充分利用多核,并发变成将成为未来发展的趋势,对于并发编程的支持,Erlang确实是不二之选,Erlang在国内仍然较为小众,经典书籍相对也要少很多,最终选择了 ...
- UVA 810 A Dicey Promblem 筛子难题 (暴力BFS+状态处理)
读懂题意以后还很容易做的, 和AbbottsRevenge类似加一个维度,筛子的形态,可以用上方的点数u和前面的点数f来表示,相对的面点数之和为7,可以预先存储u和f的对应右边的点数,点数转化就很容易 ...
- Fruit Ninja(取随机数)
链接:https://www.nowcoder.com/acm/contest/163/A来源:牛客网 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C++ 262144K,其他语言524 ...
- Python 求两个文本文件以行为单位的交集 并集 差集
Python 求两个文本文件以行为单位的交集 并集 差集,来代码: s1 = set(open('a.txt','r').readlines()) s2 = set(open('b.txt','r') ...
- Apache超时配置
Apache超时配置 1. KeepAliveTimeout 语法 KeepAliveTimeout seconds 默认 5 上下文 server config, virtual host 说明 服 ...
- 利用ss5服务搭建代理服务器
利用ss5服务搭建代理服务器 1. 下载ss5-3.8.9-8.tar.gz ###官网下载http://ss5.sourceforge.net/ 2. 安装ss5 yum -y install gc ...