Difference between == and .equals in Java.

前端 未结 7 1274
太阳男子
太阳男子 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:26

    a and b are two different references to the same object -- PROBABLY

    "a==b? " + a
    

    evaluates to the string "a==b? apple2e" which is definitely not the same (either == or equals) as the string "apple2e"

    So your code:

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

    is just testing if "a==b? apple2e" is the same as "apple2e" which it is not (no matter how you define same).

提交回复
热议问题