Mystry of Java String.intern

Mystery of Java String.intern

We have all learn about how Java has optimised handling string objects. The String class has been made immutable and string literal definition makes sure that existing instance from string pool will be returned instance creating new one.
Here is simple code snippet that explains it

String firstName = "John";
String firstNameWithNew = new String("John");
String duplicateFirstName = "John";

//All there strings are same
System.out.println(firstName.equals(duplicateFirstName) && firstName.equals(firstNameWithNew));//=> true
//Since they defiend using string iterals, even there memory references are same
System.out.println(firstName == duplicateFirstName);//=> true
//Since one of the object has defined explictly using new operator, there memory references are different.
System.out.println(firstName == firstNameWithNew);//=> false

This is very basic understanding that everyone has. Then I came across String.intern() As per java docs

When the intern method is invoked, if the pool already contains a
string equal to this String object as determined by the
equals(Object)
method, then the string from the pool is returned. Otherwise, this
String object is added to the pool and a reference to this
String object is returned.

It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.

When java string literal statement internally takes care of referring any existing same object from pool,

  • why do we need explicit function for the same?
  • where and how is it going to be used ?

Lets look at below code snippet

String jafullName = "John Andreson";
String jmFullName = "John Miller";

String jaFirstName = jafullName.substring(0, 4);
String jmFirstName = jmFullName.substring(0, 4);

//They have common first name
System.out.println(jaFirstName.equals(jmFirstName)); //=> true

//Are both references same ?
System.out.println(jaFirstName == jmFirstName);//=>False

In above snippet, even if both person’s first name are same, their references are different. Since String is immutable any operations will create new instance instate of updating existing.
If a system process large number of string objects and frequency of many string objects are very high then String pool will full of duplicate objects.

So how do you optimise above code snippet ?
String jafullName = "John Andreson";
String jmFullName = "John Miller";

String jaFirstName = jafullName.substring(0, 4).intern();
String jmFirstName = jmFullName.substring(0, 4).intern();

//They have common first name
System.out.println(jaFirstName.equals(jmFirstName)); //=> true

//Are both references same ?
System.out.println(jaFirstName == jmFirstName);//=> true

Notice the usage of intern in statement #3 & #4, it makes sure that any existing reference get returned if any same content object available in the String pool. This way the program makes sure that no duplicate strings are occupying space in String Pool.

Applications where String.intern matters
  1. Cache implementation
  2. Application that process/manipulates large number of string objects.

Comments

  1. Thanks for sharing this invaluable amazing website, your own writing skills are exemplary, you should maintain writing this type of weblog.
    First Commercial Web Browser : Netscape Navigator


    Microsoft fights botnet after Office 365 malware attack

    ReplyDelete
  2. if you are facing issues while activating the cash card
    Cannot Activate Cash Card

    ReplyDelete
  3. I am reading the whole article it is very useful and there is a lot to learn. I bookmarked your blog. Waiting for the next post. Know about new technology visit our website Top 10 in history

    ReplyDelete
  4. Presentations are an integral part of business. Everyone is concerned about giving the right presentation to the client or the people who you are giving it. No need to look further as I have come across a more efficient and easy way to PowerPoint presentation design by which you can give flawless presentations. Wheather you are giving a Corporate presentation design or want to build an animated explainer videos Pitchworx has it all.

    ReplyDelete
  5. The website loading speed is amazing. It sort of feels that you’re doing any distinctive trick. Moreover, The contents are masterwork. you have performed a excellent activity in this matter!
    Also Read - bootstrap navbar close on click, input type file css

    ReplyDelete
  6. Nice Article Bro

    Visit how to delete telegram account permanently to get tips and tricks of Technology, Android, Websites and others.llds

    ReplyDelete
  7. Simply the article is awesome
    Visit Tech News to get tips and tricks of Technology, Android, Websites and others.

    ReplyDelete
  8. Simply the article is awesome

    Visit convert images to PDF by phone to get tips and tricks of Technology, Android, Websites and others.

    Visit turn off comments on Instagram posts to get tips and tricks of Technology, Android, Websites and others.

    Visit restore deleted Instagram posts to get tips and tricks of Technology, Android, Websites and others.

    Visit reopen recently closed tabs on chrome tech news to get tips and tricks of Technology, Android, Websites and others.

    Visit tech news to get tips and tricks of Technology, Android, Websites and others.

    ReplyDelete
  9. Hello I like your website very much. Because I liked your content very much. And also I liked your template very much. And I keep visiting your site. I liked your website very much. Please keep writing such content. thank you

    ReplyDelete
  10. Your article is unmatchable read mine too on Tech News

    ReplyDelete
  11. Offroad outlaws is a new mod apk that was just released that allows you to drive around in your car or on a motorcycle off the beaten path. The app is full of different locations that you can explore, and there are lots of different vehicles to choose from. There are also a variety of challenges that you can complete to earn rewards. For more Info visit us at https://peerapk.com/offroad-outlaws-mod-apk/

    ReplyDelete
  12. Very well Explained . Thank you for published this content. visit:- https://sco.lt/8iTAnI
    https://tr.ee/EkzzAQoWjn
    https://www.pearltrees.com/hakosharad#item571304372
    https://folkd.com/link/Macbook-Logic-Board-Repair-in-Nehru-Place?
    https://public.sitejot.com/Fixmyapple.html
    https://padlet.com/fixmyapple/laptop-repair-service-center-w9ieih5irjo08o1t/wish/2856932519
    https://www.bibsonomy.org/url/3edc8cef21938ad93728cb673eb0b089
    https://diigo.com/0t2b8k

    ReplyDelete
  13. https://sco.lt/8iTAnI

    https://tr.ee/EkzzAQoWjn

    https://www.pearltrees.com/hakosharad#item571304372

    https://folkd.com/link/Macbook-Logic-Board-Repair-in-Nehru-Place?

    https://public.sitejot.com/Fixmyapple.html

    https://padlet.com/fixmyapple/laptop-repair-service-center-w9ieih5irjo08o1t/wish/2856932519

    https://www.bibsonomy.org/url/3edc8cef21938ad93728cb673eb0b089
    https://diigo.com/0t2b8k

    ReplyDelete

Post a Comment

Popular posts from this blog

Composite Design Pattern by example

State Design Pattern by Example

Eclipse command framework core expression: Property tester