Detailed explanation of judging string equality = = and equal in Java

① If we define two strings:

String c="123"; String d="123";

System. out. println(c==d);

At this time, the return value is true; Because both the C and D strings we defined at this time are stored in memory and actually point to the same storage space. In the debug mode, we can clearly see that the IDs of the two strings are the same;

② If we instantiate two string objects, we will compare the two objects instantiated by a class. At this time, use = = to judge that they are not equal

We can see that the IDs of the two objects are different.

Finally, let's talk about two ways to judge Equality: = = and equal

==True is returned only when the two sides are completely consistent. Otherwise, false is returned

Equal is used to judge whether the contents of two objects are consistent, so in the second example, we use equal to judge, the return value is true, and use = = to judge, the return value is false

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>