In Java, each object has an address space, in which the value of the object is preserved. Equals compares the value, = = compares the address and the value.
01:public class Stringexample
02: {
03:public static void Main (String args[])
04: {
05:string S0 = "Programming";
06:string S1 = new String ("Programming");
07:string s2 = "program" + "Ming";
08:
09:system.out.println ("S0.equals (S1):" + (S0.equals (S1)));
10:system.out.println ("S0.equals (S2):" + (S0.equals (s2)));
11:system.out.println ("S0 = = S1:" + (S0 = = S1));
12:system.out.println ("S0 = = S2:" + (S0 = = s2));
13:}}
This example contains 3 string variables, of which two are assigned the constant expression "programming" and the other is an instance of the string class that is assigned a new value of "programming". Use Equals (...) The comparison method and the "= =" operator produce the following results:
S0. Equals (S1): True
S0. Equals (S2): True
S0 = = S1:false
S0 = = S2:true
The String.Equals () method compares the contents of a string, using equals (...). Method compares all characters in a string one by one, and returns true if it is exactly equal. In this case all strings are the same, so the return value we get when the string s0 is compared to S1 or S2 is true. the operator "= =" compares a reference to a string instance.
The Equals () method in the String class