Home arrow static arrow Java Programming [Archive] - String handling (equals() and ==)
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - String handling (equals() and ==)
This topic has 6 replies on 1 page.

Posts:66
Registered: 1/12/04
String handling (equals() and ==)  
Jul 21, 2004 10:45 PM



 
public class Sting
{
public static void main(String ar[])
{
String s="JAVA";
String x="JAVA";
String y= new String("JAVA");

System.out.println(s==y); //will return false
System.out.println(s==x); //will return true
System.out.println(s.equals(x));//will return true
System.out.println(s.equals(y));//will return true

}
}

What is the difference between String s="JAVA"; and String y= new String("JAVA");

What is actually happens when we assign String y=new String(s);

i need answer pls dont reply like, go for search.

 

Posts:2,909
Registered: 13.8.2003
Re: String handling (equals() and ==)  
Jul 21, 2004 10:54 PM (reply 1 of 6)



 
Well, searching would be good since this is an often asked question.

Anyway, when you use == you're comparing object references. It means that you're checking if a reference is pointing to the same object as the other reference. It doesn't cause much problems with other objects, but it's an easy mistake to do with Strings.

In your example s==x will return true because of some optimizations made by Java.
This has to do with things like string pool and so forth.

You should always use .equals() for string comparison.
 

Posts:66
Registered: 1/12/04
Re: String handling (equals() and ==)  
Jul 21, 2004 11:01 PM (reply 2 of 6)



 
Thankyou kayaman

very kind of you.
 

Posts:195
Registered: 04-05-24
Re: String handling (equals() and ==)  
Jul 21, 2004 11:04 PM (reply 3 of 6)



 

"equals()" compares the content in both the objects, and if they're equal, returns TRUE.
On the other hand, "==" checks whether both the references point to the same object i.e the same memory location.

System.out.println(s==y); //will return false
It returns FALSE, since both s and y refer to different memory locations.

System.out.println(s==x); //will return true
For Strings in Java, if two different objects have the same content, they both are referred to a common memory location. This is the memory optimization technique with Java. Hence, it returns TRUE as both s and x point to same memory location.

System.out.println(s.equals(x));//will return true
As said earlier, since both the objects have the same content, it returns TRUE.

System.out.println(s.equals(y));//will return true
Same as above.

fun_one
 

Posts:28
Registered: 7/2/04
Re: String handling (equals() and ==)  
Jul 22, 2004 2:36 AM (reply 4 of 6)



 
That was a nice explaination ,but can you justify this :

String s = new String("kiran");
String s1= new String("kiran");
(s==s1) leads to false ,according to what you said objects having same content have common memory location should be true.

And String s= "kiran" ,here s is just a variable of string or do u refer it as object.
According to you irrespective of being objects and variables of strings (if they have same content then they will have common memory location ) and s==s1 will be true.
 

Posts:2,909
Registered: 13.8.2003
Re: String handling (equals() and ==)  
Jul 22, 2004 2:47 AM (reply 5 of 6)



 
new String() always creates a new String object, when using string literals (i.e. "foobar") if the string in question isn't in the string pool, it's added there. If it already is in the pool, the reference is made to point in the pool.
 

Posts:453
Registered: 11/6/04
Re: String handling (equals() and ==)  
Jul 22, 2004 2:52 AM (reply 6 of 6)



 
String s = new String("kiran");
String s1= new String("kiran");
(s==s1) leads to false ,according to what you said
objects having same content have common memory
location should be true.

No, not if you are explicitly creating a new object of String
However,
String s = new String("kiran").intern();
String s2 = "kiran";

s==s2 will give you true. When you explicitly create a new string object, it won't be the same as another string with the same content. However when you call intern on the string, the string is added to the pool and when you compare it as shown above, you get true , for the second string will be initialized to the value taken from the pool


And String s= "kiran" ,here s is just a variable of
string or do u refer it as object.

Yep, its an object. Such a statement will merely creates a reference to the string "kiran" if it's present in the pool. If it's not in the pool already , the string literal is added to the pool.
 
This topic has 6 replies on 1 page.