= = is an operator, equals is the method of the string object.
= = Compare values are equal when comparing base data types, and compare addresses when comparing reference objects. Equals () is used only to compare values for equality.
1. Basic data type Comparison
= = You can compare int,double types or string types. Equal to True, otherwise false.
Equals () is a method of a string object and can only be used to compare the value of a string variable. Equal to True, otherwise false.
Example 1:
Code:
public static void Main (string[] args) {TODO auto-generated Method Stubint a=11;int b=11;String c= "ABC";String d= "ABC";if (a==b)System.out.println ("true");ElseSystem.out.println ("false");if (C==d)System.out.println ("true");ElseSystem.out.println ("false");}
Results:
Truetrue
2. Comparison of reference objects
= = compares the address in the stack memory for equality, equal to true otherwise false. Equals compares the value, equal to True, otherwise false.
Example 2:
Code:
public static void Main (string[] args) {String d= "ABC";String E=new string ("abc");if (E==d)System.out.println ("true");ElseSystem.out.println ("false");}
Results:
False
True
JAVA = = differs from equals