Java Getting Started concept personal understanding from string comparison to = = and equals method difference

Source: Internet
Author: User

Let's look at the code first

String str1 = new String ("Hello"); String str2 = "Hello"; System.out.println ("STR1==STR2:" + (STR1==STR2)), \\1System.out.println ("Str1.equals (STR2):" + str1.equals (str2)); \ \2

Output Result:

Str1==str2:falsestr1.equals (STR2): True


Regarding = = and equals, we need to know the types of data in Java, which 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 (= =).

Because string belongs to the data type, it should be used equals, if we use = = comparison, must be compared to their memory address, so the results of \\1 \\2 is obvious

To determine whether two objects are equal, it is not possible to compare the references of two objects for equality, which is never equal, because two objects are never equal, so the correct comparison method is to directly compare the two objects, comparing the two objects is not the same as the essence, That is, the contents of these two objects are not the same, and determine whether the two objects are equal by comparing their property values.

The object class provides a Equals () method to compare the contents of two objects, so we can use this method to compare whether two objects are logically "equal". such as: C1.equals (C2); Here is a call to the Equals () method inherited from the object class, and the Equals method in the object class is defined by the Lookup API documentation as follows:

public boolean equals (Object obj)


Note that when overriding the Equals method, there are several rules that need to be designed as follows:

1 reflexivity: The return value of X,x.equals (x) for any reference value must be true.
2 symmetry: For any reference value x, Y, the return value of X.equals (Y) must be true if and only if Y.equals (x) returns a value of true;
3 transitivity: If X.equals (y) =true, y.equals (z) =true, then X.equals (z) =true
4 Consistency: If the object participating in the comparison does not change, then the result of the object comparison should not have any change
5 non-nullability: any non-null reference value x,x.equals (NULL) The return value must be false

Look at the code again:

String str2 = "Hello"; String str3 = "Hello"; System.out.println ("STR3==STR2:" + (STR3==STR2)), \\3System.out.println ("Str3.equals (STR2):" + str3.equals (str2)); \ \4

  

Output Result:

Str3==str2:truestr3.equals (STR2): True

And why are the outputs true, because they are all taken out of the buffer pool, and because the string class is special, the JDK specifically caches optimizations.

Originally, the Java runtime maintains a string pool. The string pool is used to hold the various strings produced in the runtime, and the contents of the strings in the pool are not duplicated. The normal object does not exist in this buffer pool, and the object created only exists in the stack area of the method.
This means that you need to look at how string is created:

1 when using any way to create a string object s, the Java runtime (the running JVM) takes this x to find out if there is a string object of the same content in the string pool, and if it does not, creates a string s in the pool, otherwise it is not added in the pool.
2 in Java, whenever you use the New keyword to create an object, you must create a new object (in the heap or stack area).
3 When you create a string object using either direct or string concatenation, only the strings in the string pool are checked for maintenance, and there is no one in the pool that is created in the pool. However, the string object will never be created in the stack area again.
4 Creating a String object with an expression that contains a variable will not only examine the maintenance of the string pool, but also create a string object in the stack area.

In addition, the Intern () method of string is a local method, defined as public native String intern (); The value of the Intern () method is to allow the developer to focus on the string pool. When the Intern method is called, if the pool already contains a string equal to this string object (which is determined by the Equals (object) method), the string in the pool is returned. Otherwise, this string object is added to the pool, and a reference to this string object is returned.

Java Getting Started concept personal understanding from string comparison to = = and equals method difference

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.