Difference between == and .equals in Java.

前端 未结 7 1269
太阳男子
太阳男子 2021-01-14 12:11

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.         


        
相关标签:
7条回答
  • 2021-01-14 12:49

    'a' and 'b' are the same reference, the problem is you are not comparing a and b. You should expect

    a==b? false
    

    but you just get

    false
    

    This is because + takes precedence over == so what you have is

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

    In the same way you would expect the following to print true.

    System.out.println(1 + 2 == 3);
    

    What you need is

    System.out.println("a==b? " + (a == b));
    
    0 讨论(0)
提交回复
热议问题