• 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.intern method

 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I use following code -

Is the memory allocated on heap is saved? I understand that it would add the literals to the string pool, moreover s1==s2 will result in true.
But does it really have any advantage as far as memory used to create an object (a String object in this case) is concerned? Also, where can I get to read more abt this in details besides RHE?
TIA,
- Manish
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another advantage is speed of comparations between strings. If several strings to compare are interned, they can be compared with == which is faster than compareTo. Because == only compares that the memory address of both String objects is the same. They are the same because subsequent calls to intern simply return the same addres of a string allocated in the string pool that has the same content.
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Manish,
Sorry, but I didn't understood why you're using intern() in your code...? (s1==s2 will give you false) What's the advantage of this?
Jamal.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

this would return true,as intern returns a interned string,better check JLS for intern,it could be amazing.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jamel,
If String objects having the same data are created explicitly with the new operator or their values are computed at runtime, their references will be different.
Literal strings will represent the same reference if they are created by explicitly using the intern() method and the resulting string is already in the string pool.
Tina
 
Please do not shoot the fish in this barrel. But you can shoot at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic