• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

srting doubt on ==

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://leepoint.net/notes-java/data/expressions/22compareobjects.html
 
Ramakrishna Konanki
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 436
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 436
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).


 
Ranch Hand
Posts: 258
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try to run

and you would know how the code will be compiled and made the differences.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ramakrishna,

Please have a look at this.
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"==" will check whether two reference type point to a same object or not.
 
Ramakrishna Konanki
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.





 
A teeny tiny vulgar attempt to get you to buy our stuff
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic