• 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

Strings

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if("String".replace('g','G') == "String".replace('g','G'))
System.out.println("Equal");
else
System.out.println("Not Equal");

--> The above code gives "Not equal" where as the below code gives "Equal"
if("String".replace('G','g') == "String".replace('G','g'))
System.out.println("Equal");
else
System.out.println("Not Equal");

If i give

if("String".replace('t','T') == "STring")

System.out.println("Equal");
else
System.out.println("Not Equal");


It gives Not equal

Not understanding this variation.Can someone please explain it..

Thanks,
Tashi
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As you probably know, comparing Strings with == yields true only when both are the same String instance.
When you have duplicate String literals in your code, they are resolved to a single String instance. That is the case for the various "String" in that code, they are the same instance.
In the first piece of code, method replace() returns a new String instance that is the result of applying that replacement on the original instance. You have two calls to replace() on the same original instance, yielding 2 new String instances with the results. The results are "equal" but not the same instance, therefore == yields false.
In the second piece of code, replace() finds nothing to replace, and so it returns the original String instance, not a new one. Both results are then the same original instance, so == yields true.
In the last piece, replace() returns a new instance. The literal "STring" and the result are equal() but they're separate instances. Therefore == yields false.
If it still doesn't make sense to you, make sure to learn how Java works with Strings in general, literal Strings in particular, and the operator ==.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Greg

if("String".replace('g','G') == "String".replace('g','G'))

in above code

1. first operand "String".replace('g','G') == --> This will create new Object ie "StrinG"
2. second operand "String".replace('g','G') --> This will also create object ie "StrinG"
My doubt is the object created in line 2 allready exist in the pool.In that case will it create a new object
 
greg buela
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankith,
Strings resolved at runtime and Strings explicitly created with new (even if the initial value is a literal that is in the pool) are not automatically interned (mapped to the pool). They are distinct instances, unless the intern() method is called.
Method replace() is probably using new behind the scenes!
 
Tashi Rautela
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks greg for the explaination...
this means replace function make the new instance always if changes are there & what about rest of the functions like substring,concat etc. Do they also behave in same manner.
Also intern() method is new thing for me...can please tell me what is it...

Thanks,
Tashi
 
greg buela
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it's pretty safe to say that substring and such methods create new instances too, you can make a simple test to compare the results (with ==) against a String literal that has the same contents.
The intern() method gives you a reference to the String pool instance that is equal() to your String. I don't remember what happens if it doesn't exist, I have never actually used it. But look it up, you will quickly find more valuable information than anything I can say here
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greg,

if("String".replace('g','G') == "String".replace('g','G'))
System.out.println("Equal");
else
System.out.println("Not Equal");

if("String".substring(2,4) == "String".substring(2,4))
System.out.println("Equal");
else
System.out.println("Not Equal");


if("String".concat("s") == "String".concat("s"))
System.out.println("Equal");
else
System.out.println("Not Equal");

if("String ".trim() == "String ".trim() )
System.out.println("Equal");
else
System.out.println("Not Equal");


I ahve tested all the above, all prints "Not Equal" so it means, the methods substring,replace, trim and concat are creating new instances behind the scenes!

Is this seem good?
 
greg buela
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, if they fail the == test, they're definitely separate instances.
 
reply
    Bookmark Topic Watch Topic
  • New Topic