A summary of the difference between = = and equals in Java _java

Source: Internet
Author: User
public class Compare { 
 
  /** 
   * = = equals () difference */public 
  static void Main (string[] args) { 
    String S1 = n EW String ("hello,world!"); Create two string object references; 
    String s2 = new String ("hello,world!"); 
     
    String s3 = S1;             Assigns the S1 object reference to the S3 
     
    System.out.println ("s2==s3 The result of the operation is:" + (S2==S3)); The address is not 
    the same System.out.println ("S1==S3 's operation result is:" + (S1==S3)); Address as 
        System.out.println ("S2.equals (S3) The result of the operation is:" + (S2.equals (S3));/content 
  . 

The result of the S2==S3 operation is: false
The result of the S1==S3 operation is: true
The result of S2.equals (S3) is: true

Summarize:

= = Equals () compares the contents differently, equals () is the method in the string class that compares the contents of two object references to be equal, and = = = Compares two object references for equal addresses. Because S1 and S2 are two different object references, the two are in different places in memory, and string S3 =S1 statements assign S1 references to S3, so S1 and S3 are equal to the two object references.

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.