[Java] - The diferrence ==, equals
Monday, January 22, 2018
What is the difference between " ==" operator, .equals() and compare().
1. The difference == and .equals method
+ The == operator use for reference compare (Address Compare).
+ The .equals method use content comparison.
Example:
String helloWorld = new String("HELLO");
String helloWorld1 = new String("HELLO");
String s1 = "HELLO";
String s2 = "HELLO";
System.out.println(helloWorld == helloWorld1);
System.out.println(helloWorld.equals(helloWorld1));
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
Results:
false
true
true
true
1. The difference == and .equals method
+ The == operator use for reference compare (Address Compare).
+ The .equals method use content comparison.
Example:
String helloWorld = new String("HELLO");
String helloWorld1 = new String("HELLO");
String s1 = "HELLO";
String s2 = "HELLO";
System.out.println(helloWorld == helloWorld1);
System.out.println(helloWorld.equals(helloWorld1));
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
Results:
false
true
true
true
Bài liên quan
Comments[ 0 ]
Post a Comment