Click here to Skip to main content
15,908,768 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Java
String str1 ="abc";
 
String str2 ="abc";
 
 
if(str1 == str2)
System.out.println("Equal 1");

why we don't prefer this..?

i googled it find == compares only the identity of the objects.but it print right answer why?

and equal method compare the content .
Posted
Updated 23-Oct-12 3:42am
v2

Further to the previous answer.

Taking your example:
Java
String one = "abcd";
String two = "abcd";

System.out.println(one == two);


This will print out true as both reference a single constant. However changing it thus:
Java
String one = new String("abcd");
String two = new String("abcd");

System.out.println(one == two);

will result in an output of false. The two objects contain the same values [content is equal] but are different objects [occupy different memory addresses].

It is an important habit to get into in java, using .equals(Object) rather then ==.
 
Share this answer
 
Comments
Akkywadhwa 23-Oct-12 9:57am    
5ed!
ridoy 23-Oct-12 13:03pm    
+5
Java
== tests for reference equality.

.equals() tests for value equality.


Consequently, if you actually want to test whether two strings have the same value you should use .equals() (except in a few situations where you can guarantee that two strings with the same value will be represented by the same object eg: String interning).

Java
== is for testing whether two strings are the same object.

// These two have the same value
new String("test").equals("test") ==> true

// ... but they are not the same object
new String("test") == "test" ==> false

// ... neither are these
new String("test") == new String("test") ==> false

// ... but these are because literals are interned by
// the compiler and thus refer to the same object
"test" == "test" ==> true

// concatenation of string literals happens at compile time resulting in same objects
"test" == "te" + "st"  ==> true

// but .substring() is invoked at runtime, generating distinct objects
"test" == "!test".substring(1) ==> false

It is important to note that == is much cheaper than equals() (a single pointer comparision instead of a loop), thus, in situations where it is applicable (i.e. you can guarantee that you are only dealing with interned strings) it can present an important performance improvement. However, these situations are rare.
 
Share this answer
 
Comments
ravi1989h 23-Oct-12 9:45am    
can you please explain more about reference equality.?
ravi1989h 23-Oct-12 9:45am    
i understand equal method
ravi1989h 23-Oct-12 9:46am    
interning..? mean?
ravi1989h 23-Oct-12 9:49am    
// ... but these are because literals are interned by
// the compiler and thus refer to the same object

what is the meaning?
if this true what about this
"test" == "est" ==> false // why it is false
ravi1989h 23-Oct-12 9:51am    
interned strings meaning..>?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900