The difference between "= =" and equals in Java string (first blog)

Source: Internet
Author: User

both = = and equals have the effect of equality, but what is the difference between them?

Nonsense not much to say, direct poke focus

= = compares the address, equals compares the content

Note, however, that in a Java virtual machine that contains a string pool (directly within this pool), the same contents of the string directly correspond to the same object ... Is that a little abstract, for example, to be clear?


String s1= "Hello"; String S2=new string ("Hello"); s1== "Hello";//true, because all points to the direct amount "Hello" s1==s2;//false, because S1 points to a string in the string pool, and S2 is an instance of new. The objects they point to///are different, that is, their addresses are different

Equals is relatively easy to understand, as long as the content is the same, the return ture,== content is not necessarily true, beginners can easily make mistakes here, such as the above example

This article is from the "Kinghacker" blog, make sure to keep this source http://kinghacker.blog.51cto.com/7767090/1622735

The difference between "= =" and equals in Java string (first blog)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.