On the difference between "= =" and Eqals in Java

Source: Internet
Author: User

When you are learning Java, you may often encounter the following code:

New String ("Hello"); New String ("Hello"); 3         4 System.out.println (str1==str2); 5 System.out.println (Str1.equals (str2));

Why is the output of lines 4th and 5th different? What is the difference between = = and the Equals method? If this problem is not clear when you are learning Java, you will cause some low-level errors when you write code later. Join us today to learn about the differences between = = and the Equals method.

The data types in Java can be divided into two categories:
1. The basic data type, also known as the original data type. Byte,short,char,int,long,float,double,boolean
The comparison between them, applying the double equals sign (= =), compares their values.
2. Composite data type (Class)
When they compare with (= =), they compare the storage address in memory, so unless it is the same new object, their comparison result is true, otherwise the result is false. All classes in Java are inherited from the base class of object, and a method of equals is defined in the base class in object, and the initial behavior of this method is to compare the memory address of the object, but in some class libraries This method is overwritten, such as String,integer, In these classes, date equals has its own implementation, rather than the comparison class's storage address in heap memory.
For the equals comparison between composite data types, the comparison between them is based on the address value of the location in memory where the Equals method is not covered, because the Equals method of object is compared with the double equals sign (= =). So the result of the comparison is the same as the double equals sign (= =).

There are 8 basic data types in the middle of Java:

Float type: float (4 byte), double (8 byte)

Integer: Byte (1 byte), short (2 byte), int (4 byte), Long (8 byte)

Character type: char (2 byte)

Boolean: Boolean (JVM specification does not explicitly specify the amount of space it occupies, only the literal value "true" and "false" is specified)

I. Understanding "= ="

For the equals comparison between composite data types, the comparison between them is based on the address value of the location in memory where the Equals method is not covered, because the Equals method of object is compared with the double equals sign (= =). So the result of the comparison is the same as the double equals sign (= =).

1 public class TestString {
2 public static void Main (string[] args) {
3 String S1 = "Monday";
4 String s2 = "Monday";
5 if (S1 = = s2)
6 {
7 System.out.println ("S1 = = S2");}
8 else{
9 System.out.println ("S1! = s2");}
10}
11}
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.

For variables of non-basic data types, variables in some books are referred to as reference types. For example, the above str1 is a variable of a reference type, and a variable of reference type stores not the "value" itself, but the address of its associated object in memory. For example, the following line of code:

String str1;

This statement declares a variable of a reference type, at which point it is not associated with any object.

Instead, a new String ("Hello") is used to produce an object (also known as an instance of the class String) and bind the object to STR1:

str1= New String ("Hello");

So str1 points to an object (many places also refer to str1 as a reference to the object), when the variable str1 stores the memory address of the object it points to, not the "value" itself, that is, the string "hello" that is not stored directly. The references in this are very similar to the pointers in C + +.

So when comparing s2 and s1 with = =, the result is false. So they point to different objects, which means they actually store different memory addresses.

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. 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.
)
Two. What is the comparison of equals?

The Equals method is a method in the base class object, so there is a method for all classes that inherit from object. For a more intuitive understanding of the role of the Equals method, look directly at the implementation of the Equals method in the object class.

But some friends will have questions, why the following section of the output of the code is true?

To know exactly, you can look at the specific implementation of the Equals method of the string class, also under this path, String.java is the implementation of the String class.

The following is a concrete implementation of the Equals method in the String class:

As you can see, the string class overrides the Equals method to compare whether the strings stored by the pointed string object are equal.

Other classes, such as Double,date,integer, have overridden the Equals method to compare whether the object being stored by the pointer is equal.

To summarize, say:

1) for = =, if the variables that are acting on the base data type are directly compared to the value of their stored values for equality;

If you are acting on a variable of a reference type, the address of the object to which you are pointing is compared

2) for the Equals method, note that the Equals method cannot act on a variable of the base data type

If the Equals method is not overridden, the address of the object to which the variable of the reference type is to be compared;

Classes such as String, date, and so on have overridden the Equals method to compare the contents of the object being pointed to.

On the difference between "= =" and Eqals in Java

Related Article

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.