==, Equal

Source: Internet
Author: User
Tags arabic numbers

=:

1. Compare whether the operands at both ends of the operator are the sameObject
2. The operands on both sides must be of the same type (between parent and child classes) to be compiled.
3. Compare the address. If it is a comparison of specific Arabic numbers, the value is true if it is equal, for example:
Int A = 10 and long B = 10l and double C = 10.0 are both the same (true), because they all point to the heap with the address of 10

Equals:
1. Compare whether the content of the two objects is the same

String S = "abce" is a very special form, essentially different from new. It is in JavaThe only way to generate objects without the need for new. Value assignment in the form of string S = "abce"; in Java is called a direct volume, which is placed in the constant pool rather than in the compression heap like new. This form of string is in the JVM's internal string detention. That is, when such a string is declared, the JVM will first find an object with no value of "ABCD" in the constant pool, if yes, it will be assigned to the current reference. that is, the original reference and the current reference are directed to the same object. If not, create a new "ABCD" in the constant pool. If the next time there is a string S1 = "ABCD "; it also points S1 to the "ABCD" object, that is, the string declared in this form,As long as the values are equal, any multiple references point to the same object..
String S = new string ("ABCD"); like any other object, each call generates an object as long as they are called.

It can also be understood as follows: String STR = "hello"; first, check whether there is a "hello" object in the memory. If yes, let STR point to the "hello ". if there is no "hello" in the memory, create a new object and save "hello ". string STR = new string ("hello") means that no matter whether the "hello" object exists in the memory or not, a new object is created and saved as "hello"

For details, refer to the followingCode:

 Public   Class  Test1 {  Public   Static   Void  Main (string [] ARGs) {string = New String ("AB "); //  A is a reference String B =New String ("AB "); //  B is another reference, and the object content is the same String AA = "AB "; //  Put in constant pool String BB = "AB "; //  Search from constant pool          If (AA = bb) //  True System. Out. println ("AA = BB" );  If (A = B) // False, not the same object System. Out. println ("A = B" );  If (A. Equals (B )) //  True System. Out. println ("aeqb" );  If (42 = 42.0 ){ //  True System. Out. println ("true" );}}} 

 

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.