Java中的==和.equals之间的差异。 [英] Difference between == and .equals in Java.

查看:133
本文介绍了Java中的==和.equals之间的差异。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



因此,如果我有:

  String a =apple2e; 
String b =apple2e;

System.out.println(a == b?+ a == b);

我得到 FALSE



根据我的理解,这是因为 a b 是两个不同的引用 apple2e )。



所以我会有:

  a(reference_id 123)------ 
---------apple2e
b(reference_id 456)--- ---

现在,如果我只想比较内容两个字符串,我会使用 a.equals(b)



这意味着JVM简单地返回如果两个引用指向同一个对象?



感谢



EDIT

strong>



握住手机。感谢 delnan 指出 + 优先权!



当我把它改为:

  System.out.println(a == b); 

我真的得到 true



这更有意义。



EDIT 2



我不敢相信我没有抓住那个。 lol



我在做:

 a == b? + a == b 

这转换为

 a == b?apple2e==apple2e


$ b b

难怪这是错误的。

解决方案


a和b是对同一对象(apple2e)的两个不同引用。


strong>因为字符串实现 a b 是不同的引用相同的 code> object。






不幸的是,你的代码不能做你想象的。尝试此操作:

  String a =apple2e; 
String b =apple2e;

System.out.println(a == b?+ a == b); //false
System.out.println(a == b?+(a == b)); //a == b?true

Java自动实现所有字符串字面量。 是第二个sysout打印它的作用的原因。第一个sysout只打印false,因为字符串连接( + )的优先级高于 == ,因此它等效于:

  System.out.println b?apple2e==apple2e); 

我不认为这是你想测试的!



另一方面,这将给你两个单独的 String 实例:

  String a = new String(apple2e); 
String b = new String(apple2e);

System.out.println(a == b?+(a == b)); //a == b?false


$ b

  a(reference_id 123)---------------apple2e

b reference_id 456)---------------apple2e

并可以使用 String#intern()

  String a = new String(apple2e)。intern(); 
String b = new String(apple2e)。intern();

System.out.println(a == b?+(a == b)); //a == b?true

例如

  a(reference_id 123)------ + 
+ ---------apple2e
b )------ +


I know this has been covered but I've seen inconsistent arguments here on SO.

So if I have:

String a = "apple2e";
String b = "apple2e";

System.out.println("a==b? " + a == b);

I get FALSE.

As I understand it, it's because a and b are two different references to the same object (apple2e).

So I would have something like:

a (reference_id 123) ------
                           ---------  "apple2e"
b (reference_id 456) ------

Now, if I just want to compare the contents of the two strings, I would use a.equals(b)

Does that mean that the JVM is simply returning if the two references are pointing to the same object? So it's not really doing a character-by-character comparison?

Thanks

EDIT

Hold the phones. Thanks delnan for pointing out the + precedence!!!

When I change it to:

System.out.println(a == b);

I indeed get true.

This makes more sense.

EDIT 2

I can't believe I didn't catch that. lol

I was doing:

"a==b? " + a == b

Which translates to

"a==b? apple2e" == "apple2e"

No wonder it was false!!

解决方案

As I understand it, it's because a and b are two different references to the same object (apple2e).

Because of string interning, and only because of string interning a and b are different references to the same String object.


Unfortunately, your code does not do what you think it does. Try this:

String a = "apple2e";
String b = "apple2e";

System.out.println("a==b? " + a == b);    // "false"
System.out.println("a==b? " + (a == b));  // "a==b? true"

Java automatically interns all string literals. That is why the second sysout prints what it does. The first sysout prints only "false" because string concatenation (+) has higher precedence than ==, so it's equivalent to this:

System.out.println("a==b? apple2e" == "apple2e");

I don't think that's what you meant to test!

This, on the other hand, will give you two separate String instances:

String a = new String("apple2e");
String b = new String("apple2e");

System.out.println("a==b? " + (a == b));  // "a==b? false"

Which would schematically look like

a (reference_id 123) ---------------  "apple2e"

b (reference_id 456) ---------------  "apple2e"

and can be reduced to the original situation using String#intern():

String a = new String("apple2e").intern();
String b = new String("apple2e").intern();

System.out.println("a==b? " + (a == b));  // "a==b? true"

e.g.

a (reference_id 123) ------+
                           +---------  "apple2e"
b (reference_id 456) ------+

这篇关于Java中的==和.equals之间的差异。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆