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
Archived Comments
1. akzamaqi
View Tutorial By: akzamaqi at 2017-04-30 14:23:26
2. uqwevuferise
View Tutorial By: uqwevuferise at 2017-04-30 14:19:59
3. exedaci
View Tutorial By: exedaci at 2017-04-30 14:01:25
4. oaetkea
View Tutorial By: oaetkea at 2017-04-30 13:49:55
5. ojaxanoqotoqi
View Tutorial By: ojaxanoqotoqi at 2017-04-30 13:47:17
6. iuyawonuwenu
View Tutorial By: iuyawonuwenu at 2017-04-30 13:41:15
7. evowuvu
View Tutorial By: evowuvu at 2017-04-30 13:22:58
8. elobahogopva
View Tutorial By: elobahogopva at 2017-04-30 12:50:48
9. aviraluwu
View Tutorial By: aviraluwu at 2017-04-30 12:35:57
10. itesibodet
View Tutorial By: itesibodet at 2017-04-30 12:32:58
11. ovaaqew
View Tutorial By: ovaaqew at 2017-04-30 12:32:07
12. estimado lo que sucede es que solo cuando es String maneja un pool para que no se duplique los valor
View Tutorial By: hormiga at 2013-01-07 00:11:54
13. import java.util.*;
public class demo {
public static void main(String[] arg
View Tutorial By: Virudada at 2012-05-05 06:27:22
14. Thanks alot frnds..
The discussion was really helpful for me.
Regards
View Tutorial By: Aditya at 2012-03-28 19:34:27
15. s1.equals(s2) will yield true bcoz equals() compare the contents and s1==s2 will give give false as
View Tutorial By: Suren at 2012-03-15 11:31:53
16. Hi Guys, thanks for sharing this... I have some more doubt.
1. String s1= new String(
View Tutorial By: Fins at 2011-12-14 04:40:09
17. Hi Guys, thanks for sharing this... I have some more doubt.
1. String s1= new String(
View Tutorial By: Fins at 2011-12-14 04:25:54
18. Hi,it's really nice to see all the comments of the given topic equals() method and '==' .i would lik
View Tutorial By: sraban at 2011-10-13 12:13:38
19. What does Strings s1,s2 variable contains, when i try s1==s2,
does it contains address of the
View Tutorial By: Avinav at 2011-10-04 13:52:23
20. what ll be result for s1==s2 and s1.equals(s2) for following cases?
1)String s1="hello&q
View Tutorial By: vijay at 2011-08-21 15:09:41
21. Here is my approach for writing equals() method from <a href="http://javarevisited.blogspot.
View Tutorial By: Javin @ eclipse remote debugging at 2011-04-09 06:50:44
22. Its so very helpful
View Tutorial By: Anurag at 2011-02-08 04:27:03
23. Ku is quite correct. When you create any object, it goes to the string pool. If you are creating aga
View Tutorial By: Avnish at 2010-10-28 04:00:20
24. Hai Guys..
It’s nice to see your discussion on equals() and == operation
View Tutorial By: Santosh at 2010-09-19 08:17:52
25. Hello Friends ,
s1 == s2 will always give false . Because s1 == s2 gives refers to th
View Tutorial By: Gunjan at 2010-09-08 19:24:24
26. i am not sure of this line(depends on your jvm) , can you please explain in detail as how to get the
View Tutorial By: vpeddi at 2010-03-20 07:20:19
27. There is no need to fight. ...whatever ku is saying is correct and what arpita experienced is also c
View Tutorial By: saurabh at 2009-07-30 15:19:49
28. s1==s2 is not true. its false. run the program and check it.
View Tutorial By: arpita at 2009-05-18 23:50:11
29. When JVM find two string object with de same content, in order to save memory, insted of create two
View Tutorial By: ku at 2009-05-11 10:17:51
30. Thanks for the explanation.
It makes sense. However, I tried this:
St
View Tutorial By: Miguel JImenez at 2009-05-10 14:28:46
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
Related Tutorials
Java program to get location meta data from an image
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