java ==和equals()  --Java in a Nutshell, 6th

==对于基本类型,比较的是他们的值是否相等,对于引用类型,比较的是内存中,他们是否都指向内存中同一个对象.
所有类都继承Object类的equals()方法,此方法默认的实现使用==比较,一些类重载了equals()方法,从而比较的是对象的内容.

We’ve seen that primitive types and reference types differ significantly in the way
they are assigned to variables, passed to methods, and copied. The types also differ
in the way they are compared for equality. When used with primitive values, the
equality operator (==) simply tests whether two values are identical (i.e., whether
they have exactly the same bits). With reference types, however, == compares refer‐
ences, not actual objects. In other words, == tests whether two references refer to the
same object; it does not test whether two objects have the same content.

When working with reference types, there are two kinds of equality: equality of ref‐
erence and equality of object. It is important to distinguish between these two kinds
of equality. One way to do this is to use the word “identical” when talking about
equality of references and the word “equal” when talking about two distinct objects
that have the same content. To test two nonidentical objects for equality, pass one of
them to the equals() method of the other:

All objects inherit an equals() method (from Object), but the default implementa‐
tion simply uses == to test for identity of references, not equality of content. A class
that wants to allow objects to be compared for equality can define its own version of
the equals() method. Our Point class does not do this, but the String class does,
as indicated in the code example. You can call the equals() method on an array, but
it is the same as using the == operator, because arrays always inherit the default
equals() method that compares references rather than array content. You can com‐
pare arrays for equality with the convenience method java.util.Arrays.equals().

时间: 2024-08-08 14:33:38

java ==和equals()  --Java in a Nutshell, 6th的相关文章

【转】Java中equals和==的区别

[转]Java中equals和==的区别 java中的数据类型,可分为两类: 1.基本数据类型,也称原始数据类型.byte,short,char,int,long,float,double,boolean 他们之间的比较,应用双等号(==),比较的是他们的值. 2.复合数据类型(类) 当他们用(==)进行比较的时候,比较的是他们在内存中的存放地址,所以,除非是同一个new出来的对象,他们的比较后的结果为true,否则比较后结果为false. JAVA当中所有的类都是继承于Object这个基类的,

Java中equals与==和comparaTo的区别

一.先说说Java中equals和==的区别: Java中的数据类型,可分为两类: 1.基本数据类型(也叫原始数据类型) 八大基本数据类型 char byte short int long double float boolean 2.引用数据类型(也就复合数据类型)除了上面的8个,其他都是引用数据类型 区别: 基本数据类型比较只能用==,不能用equals,因为用equals(参数),参数必须为对象. 他们之间的比较,比较的是他们的值. public class Test { public s

关于java中equals与==的区别的小实验

java中equals与==经常容易混淆,简单一点说就是equals比较的是值是否相等,是一种方法,==比较的是二者是都为同一对象,是一种操作符. 做了几个小实验比较结果. 实验一: String str1="ab": String str2="ab": System.out.println(s1==s2)://trueSystem.out.println(str1.equals(str2));//true 这里的str1与str2都指向了常量池中的同一对象,所以S

Java hashCode(), equals()

转自:http://blog.csdn.net/fenglibing/article/details/8905007冯立彬的博客 以下是关于HashCode的官方文档定义: hashcode方法返回该对象的哈希码值.支持该方法是为哈希表提供一些优点,例如,java.util.Hashtable 提供的哈希表. hashCode 的常规协定是: 在 Java 应用程序执行期间,在同一对象上多次调用 hashCode 方法时,必须一致地返回相同的整数,前提是对象上 equals 比较中所用的信息没有

java中.equals和==的区别?

Java中的equals是十分重要的,和= =要区别开来,孙卫琴的JAVA面向对象编程一书对这个做了阐述,现在小结其主要内容,而且要将 = =和 equals列为重要的对比概念来学习 1.声明格式    public  boolean equals(Object obj) 其比较规则为:当参数obj引用的对象与当前对象为同一个对象时,就返回true,否则返回false. 比如以下两个对象animal1和animal2,引用不同的对象,因此用==或equals()方法比较的结果为false;而an

java中equals和==的区别 (转)

java中equals和==的区别  值类型是存储在内存中的堆栈(以后简称栈),而引用类型的变量在栈中仅仅是存储引用类型变量的地址,而其本身则存储在堆中. ==操作比较的是两个变量的值是否相等,对于引用型变量表示的是两个变量在堆中存储的地址是否相同,即栈中的内容是否相同. equals操作表示的两个变量是否是对同一个对象的引用,即堆中的内容是否相同.  ==比较的是2个对象的地址,而equals比较的是2个对象的内容. 显然,当equals为true时,==不一定为true: 一.String中

java 笔记 == , equals , hashcode()的区别

== , equals , hashcode()的区别: 基本数据类型:比较用==, 比较他们的值 复合数据类型:用==比较时,比较的是它们在内存中存放的地址,除非是同一个new出来的对象,他们的比较后果为true,否则为false. object基类中定义了equals()方法,它的初始行为是比较它们的内存地址(就是和==功能一样),但在有些类库中覆盖了,比如String类的equals()方法是比较它们的内容.equals()方法在object类中定义如下:   public boolean

java中equals方法和“==”的比较

equals 方法是 java.lang.Object 类的方法. 有两种用法说明: (1)对于字符串变量来说,使用“==”和“equals()”方法比较字符串时,其比较方法不同. “==”比较两个变量本身的值,即两个对象在内存中的首地址. “equals()”比较字符串中所包含的内容是否相同. 比如: String s1,s2,s3 = "abc", s4 ="abc" ; s1 = new String("abc"); s2 = new St

Java中equals、==和instanceof的区别

本文转自http://blog.csdn.net/t12x3456/article/details/7341515 (1)对于字符串变量来说,使用“==”和“equals()”方法比较字符串时,其比较方法不同. “==”比较两个变量本身的值,即两个对象在内存中的首地址. “equals()”比较字符串中所包含的内容是否相同. 比如: String s1,s2,s3 = "abc", s4 ="abc" ; s1 = new String("abc"

Java中equals和hashcode的区别?

Java中equals和hashcode方法是在Object对象中的,所以每个对象都有这两个方法,大多数时候我们为了实现特定需求需要重写这两个方法 equals和hashcode方法常用在同一个类中用于比较,尤其是在set集合中比较元素是否重复 equals方法返回true的两个对象,hashcode一定相同 hashcode相同的两个对象,equals方法不一定返回true 原文地址:https://www.cnblogs.com/qf123/p/8671141.html