The difference between equals method and = =
1.equals is a method of a string object and can be passed "." Call.
2.== is an operator.
Second, the common use of comparative
1, the basic data type comparison.
equals and = = Both compare two values for equality. Equality is true, and inequality is false.
2. Comparison of reference objects.
equals and = = Both compare the address in the stack memory for equality. Equality is true, and inequality is false.
Three, easy wrong point
1, string is a special type of reference. For a comparison of two strings, either = = or equals, the strings are the same.
2. When you create two string objects, the addresses in memory are not the same, and you can assign the same values.
So the contents of the string are the same. Referenced addresses are not necessarily the same.
3, the basic data type comparison (except string) = = and equals are comparison values.
Iv. Examples of demonstrations
Comparing String objects: Ex
Public class testequals { publicstaticvoid main (String args[]) { new String ("Hello"); New String ("Hello"); System.out.println ("S1 = = S2 The result is:" + (S1 = = s2)); // false The result of System.out.println ("S1.equals (S2)" is: "+s1.equals (S2)); // true }}
The base data type is omitted here (the effect is the same).
Java easy to mix small knowledge--equals method and = = Difference