[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
- [Java Design Pattern] - Creational Patterns
- [Java] - The Nashorn Engine
- [Java] - Exception & Loging
- [Java Swing] - How to create a Menu Bar in Java UI.
- [Java Design Pattern] - Abstract class
- [Java Design Pattern] - Interface
- Spring + Thymleaf
- [Java] - ORM on Java
- [Java] - Run Java App as a Service in Cent OS
- [Oracle] - JAVA and another one related JAVA
- [Java] - Spring Frame A -> Z
- [Java Core] - hashCode method
Comments[ 0 ]
Post a Comment