equals( ) Versus == in Java
By: Mashoud
It is important to understand that the equals( ) method and the == operator perform two different operations. As just explained, the equals( ) method compares the characters inside a String object. The == operator compares two object references to see whether they refer to the same instance. The following program shows how two different String objects can contain the same characters, but references to these objects will not compare as equal:
// equals() vs ==
class EqualsNotEqualTo {
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));
}
}
The variable s1 refers to the String instance created by "Hello". The object referred to by s2 is created with s1 as an initializer. Thus, the contents of the two String objects are identical, but they are distinct objects. This means that s1 and s2 do not refer to the same objects and are, therefore, not ==, as is shown here by the output of the preceding example:
Hello equals Hello -> true
Hello == Hello -> false
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Subscribe to Tutorials
Related Tutorials
Program using concept of byte long short and int in java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Archived Comments
1. Thanks for the explanation.
It makes sense
View Tutorial By: Miguel JImenez at 2009-05-10 14:28:46
2. When JVM find two string object with de same conte
View Tutorial By: ku at 2009-05-11 10:17:51
3. s1==s2 is not true. its false. run the program and
View Tutorial By: arpita at 2009-05-18 23:50:11
4. There is no need to fight. ...whatever ku is sayin
View Tutorial By: saurabh at 2009-07-30 15:19:49
5. i am not sure of this line(depends on your jvm) ,
View Tutorial By: vpeddi at 2010-03-20 07:20:19
6. Hello Friends ,
s1 == s2 will alway
View Tutorial By: Gunjan at 2010-09-08 19:24:24
7. Hai Guys..
It’s nice to
View Tutorial By: Santosh at 2010-09-19 08:17:52
8. Ku is quite correct. When you create any object, i
View Tutorial By: Avnish at 2010-10-28 04:00:20
9. Its so very helpful
View Tutorial By: Anurag at 2011-02-08 04:27:03
10. Here is my approach for writing equals() method fr
View Tutorial By: Javin @ eclipse remote debugging at 2011-04-09 06:50:44
11. what ll be result for s1==s2 and s1.equals(s2) for
View Tutorial By: vijay at 2011-08-21 15:09:41
12. What does Strings s1,s2 variable contains, when i
View Tutorial By: Avinav at 2011-10-04 13:52:23
13. Hi,it's really nice to see all the comments of the
View Tutorial By: sraban at 2011-10-13 12:13:38
14. s1.equals(s2) will yield true bcoz equals() compar
View Tutorial By: Suren at 2012-03-15 11:31:53
15. Hi Guys, thanks for sharing this... I have some mo
View Tutorial By: Fins at 2011-12-14 04:25:54
16. Hi Guys, thanks for sharing this... I have some mo
View Tutorial By: Fins at 2011-12-14 04:40:09
17. Thanks alot frnds..
The discussion was real
View Tutorial By: Aditya at 2012-03-28 19:34:27
18. import java.util.*;
public class de
View Tutorial By: Virudada at 2012-05-05 06:27:22
19. estimado lo que sucede es que solo cuando es Strin
View Tutorial By: hormiga at 2013-01-07 00:11:54
20. ovaaqew
View Tutorial By: ovaaqew at 2017-04-30 12:32:07
21. itesibodet
View Tutorial By: itesibodet at 2017-04-30 12:32:58
22. aviraluwu
View Tutorial By: aviraluwu at 2017-04-30 12:35:57
23. elobahogopva
View Tutorial By: elobahogopva at 2017-04-30 12:50:48
24. evowuvu
View Tutorial By: evowuvu at 2017-04-30 13:22:58
25. iuyawonuwenu
View Tutorial By: iuyawonuwenu at 2017-04-30 13:41:15
26. ojaxanoqotoqi
View Tutorial By: ojaxanoqotoqi at 2017-04-30 13:47:17
27. oaetkea
View Tutorial By: oaetkea at 2017-04-30 13:49:55
28. exedaci
View Tutorial By: exedaci at 2017-04-30 14:01:25
29. uqwevuferise
View Tutorial By: uqwevuferise at 2017-04-30 14:19:59
30. akzamaqi
View Tutorial By: akzamaqi at 2017-04-30 14:23:26