String the difference between = = and equals, remember __android and Java

Source: Internet
Author: User

The role of the "= =" operator

1, for the basic data type comparison

2, to determine whether the reference point to the same address heap memory.

equals Location:

In the object class, and object is the parent of all classes, included in the JDK, but not for most scenarios, it is often necessary to override

public boolean equals (Object obj) {return
        (this = = obj);
    }

The role of equals:

Used to determine whether two variables are references to the same object, that is, whether the contents of the heap are the same and the return value is a Boolean type

Basic use of equals:

Boolean B = Obj1.equals (OBJ2);

String types compare the content of different objects to the same, and you should use equals, because = = is used to compare reference types and compare basic data types with different capabilities.

The analysis is as follows:

String as an object to use

Example one: objects are different, content is the same, "= =" returns False,equals returns True

string S1 = new String ("Java");
String s2 = new String ("Java");

System.out.println (S1==S2);            False
System.out.println (s1.equals (S2));    True

Example two: The same object, "= =" and equals result in the same

string S1 = new String ("Java");
String s2 = S1;

System.out.println (S1==S2);            True
System.out.println (s1.equals (S2));    True

String as a basic type to use the

If the value is not the same, the object is not the same, so "= =" is the same as the equals result

String S1 = "Java";
String s2 = "java";

System.out.println (S1==S2);            True
System.out.println (s1.equals (S2));    True

If there is no string object with the same value specified in the string buffer pool, the virtual machine creates a new string object for this purpose and is stored in a string buffer pool.

If there is a string object with the same value specified in the string buffer pool, then the virtual machine will not create a new string object for this purpose and return a reference to an existing string object directly.

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.