• 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

String

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I couldn't understand why sometimes new objects are created and some times not.
String a="hi";
String b="hi";
a==b - are point to the same object
but:
String a="hi";
String b="hiq";
b=b.substring(0,2);
b.equals(a)==true and a!=b - why?
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi,
First case you are creating two strings a,b with same value. First string "hi" will be created and 'a' will hold its reference and second time when you are creating string 'b' with the same value it just return reference of string "hi". Both a and b are referring to the same object.
Second case substring(0,2) method creates new string object that is why a==b will return false.
regards,
Usha
 
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 Yegor,
Bec'se in the 2nd case u r using a String method.
R E M E M B E R
"All string operations (concat, trim, replace, substring etc) construct and return new strings.....But a String can specify that its contents should be placed in a pool of unique strings for possible reuse, by calling intern() method. In programs that do a lot of String comparisons, ensuring that all Strings are in the pool, allows to use == comparison rather than the equals() method, which is slower"
Thanks.
<marquee>Ratul Banerjee </marquee>
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To add to Ratuls explanation,
new String objects are created if the above string operations change that string. And thats because Strings are immutable.

so if u say
String s1 = "Funny";
String s2 = s1.trim();
s1 and s2 are still at the same memory location ecause trimming has yielded the same string and so why waste a new memory chunk to store that.
This has more to do with how java compiler tries to optimize things. But honestly, in a real life situation, i guess, one need not use == and should always use .equals() only.
Any objections?
Vivek

 
Yegor Mikhaylov
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. It's a bit clear for me.
 
You ridiculous clown, did you think you could get away with it? This is my favorite tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic