File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes srting  doubt on == Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "srting  doubt on ==" Watch "srting  doubt on ==" New topic
Author

srting doubt on ==

Ramakrishna Konanki
Greenhorn

Joined: Apr 18, 2008
Posts: 20


class StringTest
{
public static void main(String[] args)
{
String s1 = new String("change me");
String s2 = new String("change me");
System.out.println(s1==s2);
String s3 = "change me";
System.out.println(s1==s3);
String s4 = "change me";
System.out.println(s3==s4);
String s5 = "change "+"me";
System.out.println(s4==s5);
String s6 = "change ";
String s7 = s6 + "me";
System.out.println(s4==s7);
final String s8 = "change ";
String s9 = s8 + "me";
System.out.println(s4==s9);
}
}

output
-------
false
false
true
true
false
true

here s7=s6+"me";
s4 and s7 are equal but it is showing false...why...

one of my friend told s7 stores in heap.....
my doubt is s7 is normal variable....how it is going to store in heap
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 433
    
    2
What does the == operator do?

What is a String? Where in memory does it live?

What do you want to do with the two Strings?

Is what you want to do the same thing the == operator does?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Ramakrishna wrote:
here s7=s6+"me";
s4 and s7 are equal but it is showing false...why...

one of my friend told s7 stores in heap.....


your friend is right. the above statement is interpreted by compiler as

here, you are creating a new String.

hth
Bobby Smallman
Ranch Hand

Joined: Sep 09, 2010
Posts: 107
http://leepoint.net/notes-java/data/expressions/22compareobjects.html


Everyday in every way, we get a little better.
Ramakrishna Konanki
Greenhorn

Joined: Apr 18, 2008
Posts: 20
What does the == operator do?
== operator compares two references of a string.

What is a String? Where in memory does it live?
String is a set/combinations of characters.

String s; s="ab"; //this will store in stack
String s= new String("ab"); //this will store in heap
.............
this is my expectation
.................

What do you want to do with the two Strings?
just i want to compare two strings.

Is what you want to do the same thing the == operator does?
String s4 = "change me";
String s5 = "change "+"me";

s4==s5
the result is true..

my doubt is
String s6 = "change ";
String s7 = s6+"me";
s4==s7
should be true....
but it is getting false....what would be the reason.......
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 433
    
    2
Bobby Smallman wrote:http://leepoint.net/notes-java/data/expressions/22compareobjects.html


Thank you for pointing to this. It can be really helpful - even if I quite don't like the first line for == and objects: "Compares references, not values." (in bold!). Hm... == compares values here, too. The values of the two references. (And next in this cinema: Does Java "call by value" or "call by reference, sometimes" ).
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 433
    
    2
Ramakrishna wrote:What does the == operator do?
== operator compares two references of a string.


Generally speaking, it compares two primitive values. If the type is int, the primitive value itself represents the value, so it this is the same as a comparision of the contents (i.e 4==5 is a comparision of primitive types).

When used with objects it is like the identity operator. When used with reference variables it checks if both point to the same object (i.e. "object at heap position 4"=="object at heap position 5").


What is a String? Where in memory does it live?
String is a set/combinations of characters.

String s; s="ab"; //this will store in stack
String s= new String("ab"); //this will store in heap
.............
this is my expectation
.................


Ah, you are wrong here.

My question was pointing at the nature of String. Look at the Javadocs: java.lang.String. A String is an java.lang.Object.

This is true for every String, regardless of how it was created.

String s="ab"; could be seen just a shortcut for s=new String("ab"); Strings are often used, so they have some extra syntax, that other objects don't have. You can add two String objects with +. That is operator overloading, not possible for other objects.

And now the catch: s="ab" does a little more magic than only replace it with s=new String("ab"), which is where you problems occur.




What do you want to do with the two Strings?
just i want to compare two strings.


You want to compare the content. You don't want to check the identity. And as we saw == used on Objects is the identity operator. Strings are always Objects.


my doubt is
String s6 = "change ";
String s7 = s6+"me";
s4==s7
should be true....
but it is getting false....what would be the reason.......


And this is where the magic I mentioned before kicks in.

The Java virtual machine holds a cache for Strings. If you do a:

String a = "one";
String b = "one";

than there will be no object created for the second String. It will be reused from the cache. So a and b point to the same object, the identity operator seems to work.

When doing

String a = new String("one");
String b = new String("one");
String c = "on"+"e";

new objects will be created. The Strings have equal contents, but they are different objects.

In consequence: When you want to compare the contents of two Strings, never use ==, only use the equals method.
Sudipta Laha
Ranch Hand

Joined: Aug 23, 2010
Posts: 49
Hi,,

There are two places one stringpool and the other is the string object in heap.

Now when we use


a is stored in stringpool.
But now if we create


It will point to the same string pool object. And the == is true as references are true.

Now lets take two other conditions:


there will be two objects created in heap for the new operator, So == will return false as references are not same.

One intern() method is there , it will return a object from global string pool. and the == will be true in that case (in your situation).



Regards,
Sudipta Kumar Laha
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

Try to run

and you would know how the code will be compiled and made the differences.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
"Ramakrishna" it is not possible to send you a private message to warn you about the naming policy, but you are still in violation of it.
Correct your displayed name to match the policy, or we shall withdraw your privilege of using this website.
Ninad Kuchekar
Ranch Hand

Joined: Jan 05, 2010
Posts: 64
Hi Ramakrishna,

Please have a look at this.


Don't walk as if you rule the world, walk as if you don't care who rules it...
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

"==" will check whether two reference type point to a same object or not.


SCJP6.0,My blog Ranchers from Delhi
Ramakrishna Konanki
Greenhorn

Joined: Apr 18, 2008
Posts: 20
sorry for voilating rules.
could you please explain the naming policy rules....


Campbell Ritchie wrote:"Ramakrishna" it is not possible to send you a private message to warn you about the naming policy, but you are still in violation of it.
Correct your displayed name to match the policy, or we shall withdraw your privilege of using this website.




 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: srting doubt on ==