Java interview for job search = = vs equals () difference

Source: Internet
Author: User


The difference between equals and = = in Java
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, apply the double equals sign (= =) and compare their values.
2. Composite data type (Class): when they are used (= =) For comparison, the comparison is that they are in memory of the storage address

Therefore, unless it is the same new object, their comparative result is true, otherwise the result is false. All of Java's classes inherit 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,date in these classes where equals has its own implementation, and is no longer a comparison class in the heap memory of the storage address.

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

A value type is a stack stored in memory (a stack), whereas a variable of a reference type stores the address of a reference-type variable in the stack, which itself is stored in the heap.

So. = = Comparison is the address in the stack. And equals is the content of the heap inside the stack where the address is pointing.



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 execute the program, output: S1 = = S2 Description: S1 and S2 refer to the same String object-"Monday"!

A little bit of a 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 S2 use the new operator to create//program output://S1! = s2//s1 equals s2//description: S1 s2 references two "Monday" string objects respectively


3. String buffer pool
Originally. A string buffer pool is created when the program executes, when using S2 = "Monday" is the time to create a string. The program first looks for objects of the same value in this string buffer pool, and in the first program, S1 is first placed in the pool, so when S2 is created, the program finds S1 with the same value.
S2 refers to the object referenced by S1 "Monday"
In the second procedure, using the new operator, he explicitly tells the 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.


Summary for example the following:

= = is compared to the address of 2 objects, while equals is the content of 2 objects.

Java interview = = and equals () difference

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.