Java Learning notes--"= =" and "Equals"

Source: Internet
Author: User

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 their storage address in memory. Therefore, unless it is the same new object, the result of their comparison 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 .
However, in some class libraries This method is overridden, 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.

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 (= =).

Note 1: Cache values for wrapper classes
The wrapper class object is immutable, so for some frequently used values, the system provides a cache of wrapper classes (pre-created frequently used wrapper class objects) that are taken directly from the buffer instead of creating a new wrapper class object.
All values of Boolean (True false)
All values of byte ( -128~127)
0~127 of Char values
-128~127 of the short value
The -128~127 of the int value
The -128~127 of a Long value

So
Character C1 = 127;
Character C2 = 127;
C1.equals (C2) for true//equals determine the basic type of data within the equality No
C1==C2 true//To determine the same address no

Character C1 = 128;
Character C2 = 128;
C1.equals (C2) is true
C1==C2 is False


NOTE 2: String buffer pool
The program creates a string buffer pool when it is running. When using S2 = "Monday" Such 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 the same value of S1

 Public class teststring {    publicstaticvoid  main (string[] args) {        = "Monday" ;         = "Monday";         if (S1 = = s2) {            System.out.println ("S1 = = s2");         Else {            System.out.println ("S1! = S2")        ;     }}}

In the following procedure, the new operator was used, and he understood 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, resulting in waste. ( cannot be changed after string creation )

 Public classteststring { Public Static voidMain (string[] args) {String S1= "Monday"; String S2=NewString ("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"); }    }}

Program:

 Public classteststring { Public Static voidMain (string[] args) {String S1= "Monday"; String S2=NewString ("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.
)

Reference: http://www.cnblogs.com/zhxhdean/archive/2011/03/25/1995431.html

Java Learning notes--"= =" and "Equals"

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.