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.