The equals () method in the String class, stringequals
In Java, each object has an address space in which the value of this object is saved. Equals compares the value, = the address and value of the comparison.
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 three String variables, two of which are assigned with a constant expression "Programming", and the other with a new value of the String class "Programming. Comparison Using the equals (...) method and the "=" operator produces the following results:
S0. Equals (s1): true
S0. Equals (s2): true
S0 = s1: false
S0 = s2: true
String. the equals () method compares the content of a string and uses equals (...) the method compares all characters in the string one by one. If they are completely equal, true is returned. In this case, all strings are the same, so when the string s0 is compared with s1 or s2, the returned value is true. The operator "=" compares the String instance references.