Parsing of equals and = = in Java

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)
(1) 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.
(2) for the equals comparison between composite data types , the comparison between them is based on the address value of their location in memory without the Equals method being covered. Because the Equals method of object is compared with the double equals sign (= =), the result of the comparison is the same as the double equals sign (= =).

3. String Buffer pool
(1) Originally, the program at run time will create a string buffer pool when using Param2_1 = "Lowi" expression is to create a string, the program first in this string buffer pool to find the same value of the object, Param2_1 was put into the pool first, so in param2 When the _1 is created, the program finds the param1_1 with the same value
S2 refers to the object referenced by S1 "Monday"
(2) using the new operator, he understood the telling program: "I want a new one!" Don't be old! "So a new" Lowi "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.Java.lang.String's Intern () method

Java.lang.String's Intern () method "Lowi". The return value of the Intern () method is also the string "Lowi", which looks as if this method is of little use.

In fact, do a program processing: Check the string pool for the existence of "Lowi" a string, if present, return the string in the pool, if not, the method will add "Lowi" to the string pool, and then return its reference.

The code is detailed as follows:

 Public classTestequals { Public Static voidMain (string[] args) {//the objects created in this way are params1_1 and params2_1, because the values are the same, so that only one place is stored in memoryString params1_1= "Lowi"; String Params2_1= "Lowi"; //the objects created in this way are params1_2 and params2_2,//objects created by new mode, such as Sting objects, are created in memory, their values are the same, but the locations are differentString params1_2=NewString ("Lowi"); String params2_2=NewString ("Lowi"); //compare using ""        if(params1_1==params2_1) {System.out.println ("Params1_1==params2_1"); }Else{System.out.println ("Params1_1!=params2_1"); }                if(Params1_1.equals (params2_1)) {System.out.println ("Params1_1 and Params2_1,equals"); }Else{System.out.println ("Params1_1 and Params2_1,not equals"); }        /**//Results one params1_1==params2_1 params1_1 and params2_1,equals **/        //The comparison uses the "" and the new object: a        if(params1_1==params1_2) {System.out.println ("Params1_1==params1_2"); }Else{System.out.println ("Params1_1!=params1_2"); }        if(Params1_1.equals (params1_2)) {System.out.println ("Params1_1 and Params1_2,equals"); }Else{System.out.println ("Params1_1 and Params1_2,not equals"); }        /**//Results two params1_1!=params1_2 params1_1 and Params1_2,equals*/        //compare creating objects with new        if(params1_2==params2_2) {System.out.println ("Params1_2==params2_2"); }Else{System.out.println ("Params1_2!=params2_2"); }        if(Params1_2.equals (params2_2)) {System.out.println ("Params1_2 and Params2_2,equals"); }Else{System.out.println ("Params1_2 and Params2_2,not equals"); }        /**//Results three params1_2!=params2_2 params1_2 and Params2_2,equals*/        //comparing objects created with new: two//The java.lang.String method intern is used to check if there is a string in the string pool that is "Lowi", and if so, returns the string in the pool;//If it does not exist, the method adds "Lowi" to the string pool, and then returns its reference. Params2_2 =Params2_2.intern (); if(params2_1==params2_2) {System.out.println ("Params2_1==params2_2"); }Else{System.out.println ("Params2_1!=params2_2"); }        if(Params2_1.equals (params2_2)) {System.out.println ("Params2_1 and Params2_2,equals"); }Else{System.out.println ("Params2_1 and Params2_2,not equals"); }        /**//Results four params2_1==params2_2 params2_1 and Params2_2,equals*/    }}

Parsing of equals and = = 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.