The difference between = = and equal in Java

Source: Internet
Author: User

I saw a very good blog today, so I turned around.

Reprint Address: http://xiashengchao.iteye.com/blog/753409

A value type is a stack stored in memory (hereafter referred to as a stack), whereas a variable of a reference type is simply the address of a reference-type variable stored in the stack, which itself is stored in the heap.

The = = Operation compares the values of two variables for equality, and for a reference variable represents whether the two variables are stored in the same address in the heap, that is, whether the contents of the stack are the same.

Whether the two variables represented by the equals operation are references to the same object, that is, whether the contents of the heap are the same.

= = compares the addresses of 2 objects, while equals compares the contents of 2 objects.

Obviously, when equals is true, = = is not necessarily true;

equals and = = in a string

1.

public class TestString {

public static void Main (string[] args) {

String S1 = "Monday";

String s2 = "Monday";

}

}

How many objects are there in this procedure?

Check it out, change the program a little bit.

public class TestString {

public static void Main (string[] args) {

String S1 = "Monday";

String s2 = "Monday";

if (S1 = = s2)

System.out.println ("S1 = = s2");

Else

SYSTEM.OUT.PRINTLN ("S1! = s2");

}

}

Compile and run the program, output: S1 = = S2 Description: S1 and S2 refer to the same String object-"Monday"!

2. A little more change to the program, there will be more strange discovery:

public class TestString {

public static void Main (string[] args) {

String S1 = "Monday";

String s2 = new String ("Monday");

if (S1 = = s2)

System.out.println ("S1 = = s2");

Else

SYSTEM.OUT.PRINTLN ("S1! = s2");

if (s1.equals (S2)) System.out.println ("S1 equals S2");

Else

System.out.println ("S1 not equals S2");

}

}

We will create the S2 with the new operator

Program output:

S1! = S2

S1 equals s2

Description: S1 S2 cited two "Monday" string objects respectively

3. String buffer pool

Originally, the program at run time will create a string buffer pool when using S2 = "Monday" expression is to create a string, the program first in this string buffer pool to find the same value of the object, in the first program, S1 was put into the pool, so when the S2 was created, The program found a S1 with the same value

S2 refers to the object referenced by S1 "Monday"

In the second procedure, the new operator was used, and he understood the telling program: "I want a new one!" Don't be old! "So a new" Monday "Sting object is created in memory. Their values are the same, but the locations are different, one swimming in the pool and one resting on the shore. Alas, it is a waste of resources, obviously the same must be divided into what?

4.

To change the program again:

public class TestString {

public static void Main (string[] args) {

String S1 = "Monday";

String s2 = new String ("Monday");

S2 = S2.intern ();

if (S1 = = s2)

System.out.println ("S1 = = s2");

Else

SYSTEM.OUT.PRINTLN ("S1! = s2");

if (s1.equals (S2)) System.out.println ("S1 equals S2");

Else

System.out.println ("S1 not equals S2");

}

}

This time join: S2 = S2.intern ();

Program output:

S1 = = S2

S1 equals s2

Originally, (Java.lang.String's Intern () method "abc". The return value of the Intern () method is also the string "abc", which looks as if this method is of little use. But in fact, it did a little trick: Check the string pool for the existence of a string "abc", if present, return the string in the pool, if it does not exist, the method will add "ABC" to the string pool, and then return its reference.

A better approach:

Put all the strings intern () to the buffer pool.

It's best to do this when you use new.

String s2 = new String ("Monday"). Intern ();

Then you can compare the values of two strings with = =.

Ii. equals and = = in simple data types and encapsulation classes

Java provides a wrapper class for each simple data type, and each base data type can be encapsulated into an object type.

In addition to int (Integer) and char (Character), the remaining type is capitalized as a wrapper class type name. Double (double), float (float), long (long), short (short), Byte (Byte), Boolean (Boolean).

take int and integer as an example to illustrate

The differences between int and Integer in Java are as follows:

1.int is the basic data type, and the default value can be 0;2. Integer is the encapsulated class of int, the default value is Null;3.int and integer can represent a certain value, 4.int and integer cannot be interoperable because they have two different data types;

int a1=1;

int a2=1;

Integer B1 =new integer (1);

Integer b2 =new integer (1);

------------------------------

A1==A2 This is established, very simple, all know a1==b1 this is not tenable. The value of the expression is false, and they are different data types B1==B2 This is not true. The value of the expression is false, although it is the same data type, but they are two objects, = = The addresses of the 2 objects are compared, their addresses are unequal and the contents are equal to 1;

B1.equals (B2) ==true This is true, the value of the expression is. The same data type, two objects, the address is different, the content is the same, quals comparison is the content of 2 objects, so set up.

(A.equals (b), because equals compares two objects, a, B cannot be a base data type, or a compilation error occurs.) Similarly, other encapsulation classes and basic types are the same.

The difference between equals and = = in Java

= = compares the addresses of 2 objects, while equals compares the contents of 2 objects.

Third, other classes how to use equals and = =

Most of the classes in the API rewrite the Equals method, not the rewrite is generally their own writing class, if you define a class, the comparison of custom classes with equals and = = is the same, is the comparison handle address, because the custom class is inherited from Object, And the equals in object is realized with = =, you can see the source code.

Iv. What is the relationship between equals and Hashcode in Java

Just to maintain a regular contract for the Hashcode method, the hashcode of the two objects compared with equals is required. Equals () and hashcode () are all from java.lang.Object. Of course you can rewrite.

such as A.equals (b). Returns true only if the memory address of a is equal. Of course, such as String class has already rewritten this method, the comparison is no longer the memory address. The value of Hashcode () is also associated with memory addresses. Therefore, hashcode are equal only if the memory addresses are equal.

There are also many classes that rewrite this method, or string for example:

public int hashcode () {

int h = hash;

if (h = = 0) {

int off = offset;

Char val[] = value;

int len = count;

for (int i = 0; i < len; i++) {

H = 31*h + val[off++];

}

hash = h;

}

return h;

}

It is not related to the memory address. This is done to ensure that two objects that are returned as true are compared with equals, and that their hashcode are the same.

So when you rewrite equals, you rewrite Hashcode (). Of course, this is equivalent to a contract, a protocol. You don't do it right.

Wu, hashcode

In general applications you do not need to understand the use of hashcode, but when you use Hashmap,hashset and other sets of classes to pay attention to the hashcode.

You want to take HashMap's Value,hashmap by an object key. The way to work is to find the address in memory through the hashcode of your incoming object, When this address is found, then the Equals method is used to compare whether the contents of this address are the same as the one you put in, and the value is taken out.

So here's to match 2 parts, hashcode and equals but if you say you new object as key to take value is never the result, because each time new an object, the object's hashcode is always different, So we're going to rewrite hashcode, you can make your hashcode a constant in object, so you can always find the address of key through the hashcode of your object, and then you rewrite your Equals method to make the contents of the memory equal ...

The difference between = = and equal in Java

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.