• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Comparing string

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Running the following code gives "x and z - different obj", which I don't understand. Thanks for help.

My understanding is z would point to the same String obj "abc" in the pool as x does.

-----------------------

String x="abc";
String z = "ab";
z = z + "c";

if(x == z) System.out.println("x and z - same obj");
else System.out.println("x and z - different obj");

-----------------------
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello Friend !

String x="abc";
String z = "ab";
z = z + "c";

if(x == z) System.out.println("x and z - same obj");
else System.out.println("x and z - different obj");

As, x and z are different objects allocated in memory, if you compare them with == operator, the compiler compares the two memory locations, and it may found different addresses. So, you are getting the out put of x and z -different objects.

feel free to ask any doubt in this concern.

ganesh..
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai
String x="abc";
String z = "ab";
z = z + "c";

if(x == z) System.out.println("x and z - same obj");
else System.out.println("x and z - different obj");

As, x and z are different objects allocated in memory, if you compare them with == operator, the compiler compares the two memory locations. For this you have to check this one also

String a="abc";
String b = "abc";
if(a ==b ) System.out.println("a and b - same obj");
else System.out.println("a and b - different obj");

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What exactly are you trying to do? Do you want to compare the object memory locations, or the values. If you want to compare the values, you need to use the String.equals(String) method.
 
Mike Vo
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all your replies.

I was trying to understand how String object gets created.

As mentioned in the K&B's book: "When the compiler encounters a String literal, it checks the pool to see if an identical String already exists. If a match is found, the reference to the new literal is directed to the existing String, and no new String literal object is created."

Actually, Rakesh's code gives: "a and b - same obj", which makes sense to me. However, I am still wondering in my code why z is not refering the existing "abc" in the pool.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mike,

you seem to know a little bit more about the answer to your question than those who have replied!

The simple answer is that the '+' operator will always return a new String reference, unless you invoke the 'intern()' method:

Here, I take the new String reference returned by c + "c" and I force the JVM to look in the existing String pool, returning a reference to any match it finds. If you compare a and c now, they are equal, because the JVM did indeed find a match for the String "abc".

If it had found no match, then a new String would be added to the pool, and a reference to that would have been returned
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic