The difference between = = and equals in Java

Source: Internet
Author: User

Package com.demo02;

public class Testobject {

/**
* @param args
*/
/*
* 1, the object class is the root class, inside the definition of = = and equals the same role, are compared to the reference address
* 2, and the string immutable class overrides the Equals method inside.
* = = is the comparison reference address, equals is the comparison content
* 3. What string pool is in the string class, and for a string object that can be shared, the lookup in the pool appears
* Whether the same string content exists (the same string), and if so, returns directly instead of creating a new
* String object to reduce memory consumption
*
*
* */


public static void Main (string[] args) {
TODO auto-generated Method Stub
Testobject T1 = new Testobject ();
Testobject t2 = new Testobject ();
System.out.println (T1==T2);//false
System.out.println (T1.equals (T2));//false

string S1 = new String ("abc");
String s2 = new String ("abc");
System.out.println (S1==S2);//false
System.out.println (s1.equals (S2));//true
String s3 = "Hello";
String s4 = "Hello";
System.out.println (S3==S4);//true
System.out.println (S3.equals (S4));//true

}

}

The difference between = = and equals 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.