equals() Versus == In Java Example
Chapter:
String Handling
Last Updated:
15-07-2016 10:36:30 UTC
Program:
/* ............... START ............... */
public class JavaEqualsExample {
public static void main(String args[]) {
String s1 = "Hello";
String s2 = new String(s1);
System.out.println(s1 + " equals " + s2 + " -> " + s1.equals(s2));
System.out.println(s1 + " == " + s2 + " -> " + (s1 == s2));
}
}
/* ............... END ............... */
Output
Hello equals Hello -> true
Hello == Hello -> false
Notes:
-
The equals( ) method compares the characters inside a String object.
- The == operator compares two object references to see wherther it is from same instance.
Tags
equals() Versus ==, Java