==、equals() 和 instanceof() 之间的区别
String s=new String("Computer");
if(s=="Computer")
System.out.print("equals A");
if(s.equals("Computer"))
System.out.print("Equal B");
输出是 Equal B
现在为什么 ==
不会产生 equals A
instanceof
是什么?
String s=new String("Computer");
if(s=="Computer")
System.out.print("equals A");
if(s.equals("Computer"))
System.out.print("Equal B");
Output is Equal B
Now why ==
doesnot produce equals A
what is instanceof
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
==
是引用等于,例如“这个对象与其他对象是完全相同的对象吗?”。当您创建
s
时,您创建了一个新对象,因此它也不会与您要比较的字符串相同的对象(取决于您使用的 JVM"foo" == "foo"
将为 true,因为它将在内部使用相同的对象)。当您调用
.equals()
时,它(字符串s
)运行它自己的逻辑来确定它是同一个对象。对于字符串,它将逐个字符进行比较。instanceof
完全是另一回事,它会告诉您一个对象是否是某种类型对象的实例,例如"foo" instanceof String
为 true。==
is referential equals, as in, 'is this exact object the exact same object as they other object?'.When you made
s
you made a new object, so it won't be the same object as the string you're comparing it too (depending on the JVM you're using"foo" == "foo"
will be true because it will use the same object internally).When you called
.equals()
it (the Strings
)ran it's own logic to determine that it's the same object. In the case of String, it will compare it character by character.instanceof
is something else entirely, and will tell you if an object is an instance of a certain type of object, e.g."foo" instanceof String
is true.如果两个字符串具有相同的值,
equals()
方法将返回true
。如果两个字符串都指向同一个底层对象,则==
运算符才会为true
。因此,当通过 equals(Object) 方法测试时,保证表示相同内容的两个字符串相等;而当使用==
运算符进行测试时,只有当它们引用相同的Object
时才会成立。 因此,我们应该始终使用equals()
进行String
比较。instanceof
关键字可用于测试是否对象具有指定类型。例如,如需了解更多信息,请参阅此处。
The
equals()
method will returntrue
, if two Strings have the same value. The==
operator will only betrue
, if both Strings point to the same underlying Object. Hence two Strings representing the same content are guaranteed to be equal, when tested by theequals(Object)
method; whereas when tested with the==
operator, it will only be true with they refer to the sameObject
. So, we should always useequals()
forString
comparison.The
instanceof
keyword can be used to test if an object is of a specified type. For example,For more insight, see here.
同意这可能与另一个问题重复,但无论如何我都会给出答案。
== 运算符用于确定两个对象或原始数据类型(如 int)是否相同。
因为 String 是一个对象,所以使用 == 来比较两个 String 是试图查看对象本身是否相同,但事实并非如此。为了获得“等于 A”结果,您需要将 s 与其自身进行比较。
String 的
.equals()
和.equalsIgnoreCase()
是比较 String 对象的文本值的方法,这正是您尝试执行的操作。instanceof
关键字用于发现对象的类型(例如,对象是否属于 String 类型或 Integer 类型)。Agree that this possibly a duplicate of another question, but I'll throw an answer in anyway.
The == operator is to determine whether two objects or raw data types (like an int) are identical.
Because a String is an object, using == to compare two Strings is attempting to see if the objects themselves are identical, which they are not. For you to get the "equals A" result, you would need to compare s with itself.
A String's
.equals()
and.equalsIgnoreCase()
are methods that compare the text value of the String object, which is what you are attempting to do.The
instanceof
keyword is used to discover an object's type (e.g. if an object is of type String or of type Integer).如果您将字符串定义为 s="Computer" 而不是创建新对象,它将返回 true。这是因为 java 将字符串文字存储在特殊的内存空间中,因此 s 和“Computer”字符串将指向相同的内存位置。
instanceof 是一个运算符,如果左侧对象属于右侧指定的类的类型,则该运算符返回 true。
例如,
前面的代码将返回 true。
It would return true if you had defined the string as s="Computer" instead of creating a new object. this is because java stores string literals in a special memory space so s and "Computer" string would point to the same memory location.
instanceof is an operator that returns true if the left side object is of the type of the class specified by the right side.
E.g.
The prevoius bit of code would return true.
x instanceof ClassName 返回 true 当且仅当 x 是类 ClassName 或其任何子类的对象
所以: new String("abc") == new String("abc") 是 false - 相同的值但不同的对象,类似地 "abc" == new String("abc") 是 false,但 "abc" == "abc" 为 true,因为如果一个 String 常量在 .class 文件中出现多次,它就会被转换为一个 String 对象。
x.equals(y) 默认情况下(在 Object.equals 中)实现为 x == y,但许多子类会覆盖它,例如对于字符串,x.equals(y) 检查这些字符串是否具有相同的值,无论它们是否是相同的对象,因此: new String("abc").equals(new String("abc")) 为 true,并且 "abc".equals("abc") 为 true
但是,如果要比较的所有字符串都已“实习”,例如: x = x.intern(); y = y.intern(),那么 x == y 就可以了。例如 "abc" == new String("abc").intern() 给出 true。
x instanceof ClassName returns true if, and only if x is an object of class ClassName or any subclass of it
x == y works differently for value types (char, int, float, etc) and reference types (Object, Integer, YourClass, int[], ...): for value types returns true if x and y are just the same value, whereas for reference types returns true if x and y denote the same object - for two objects with different values the result can be false. Just treat == on reference type like comparison of pointers in C++ - the result is true if integers representing pointers are equal, ie pointers denote to the same object.
So: new String("abc") == new String("abc") is false - the same value but different objects, similarly "abc" == new String("abc") is false, but "abc" == "abc" is true, because if a String constant appears many times in a .class file, it is converted into one String object.
x.equals(y) is by default (in Object.equals) implemented just as x == y, but many subclasses override it, for example for Strings, x.equals(y) checks if those Strings have the same value, despite if they are same objects or not, so: new String("abc").equals(new String("abc")) is true, and "abc".equals("abc") is true
however, if all your Strings to compare have been 'interned', like: x = x.intern(); y = y.intern(), then x == y will work. So for example "abc" == new String("abc").intern() gives true.