Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7930921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:34:07+00:00 2026-06-03T20:34:07+00:00

What is String Interning in Java, when I should use it, and why ?

  • 0

What is String Interning in Java, when I should use it, and why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T20:34:08+00:00Added an answer on June 3, 2026 at 8:34 pm

    http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#intern()

    Basically doing String.intern() on a series of strings will ensure that all strings having same contents share same memory. So if you have list of names where ‘john’ appears 1000 times, by interning you ensure only one ‘john’ is actually allocated memory.

    This can be useful to reduce memory requirements of your program. But be aware that the cache is maintained by JVM in permanent memory pool which is usually limited in size compared to heap so you should not use intern if you don’t have too many duplicate values.


    More on memory constraints of using intern()

    On one hand, it is true that you can remove String duplicates by
    internalizing them. The problem is that the internalized strings go to
    the Permanent Generation, which is an area of the JVM that is reserved
    for non-user objects, like Classes, Methods and other internal JVM
    objects. The size of this area is limited, and is usually much smaller
    than the heap. Calling intern() on a String has the effect of moving
    it out from the heap into the permanent generation, and you risk
    running out of PermGen space.

    —
    From: http://www.codeinstructions.com/2009/01/busting-javalangstringintern-myths.html


    From JDK 7 (I mean in HotSpot), something has changed.

    In JDK 7, interned strings are no longer allocated in the permanent generation of the Java heap, but are instead allocated in the main part of the Java heap (known as the young and old generations), along with the other objects created by the application. This change will result in more data residing in the main Java heap, and less data in the permanent generation, and thus may require heap sizes to be adjusted. Most applications will see only relatively small differences in heap usage due to this change, but larger applications that load many classes or make heavy use of the String.intern() method will see more significant differences.

    — From Java SE 7 Features and Enhancements

    Update: Interned strings are stored in main heap from Java 7 onwards. http://www.oracle.com/technetwork/java/javase/jdk7-relnotes-418459.html#jdk7changes

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicates: String comparison and String interning in Java What is the difference between
I understand the basic idea of java's String interning, but I'm trying to figure
I am trying to understand string interning and why is doesn't seem to work
I tried to look at Java's String#intern() method, but it's public native String intern();
I am writing a translator, and have quite a few java String literals that
I have created a Java code for my Android App. String[] MovieName=new String[]{}; for
Building a multi-language application in Java. Getting an error when inserting String value from
I'm using Eclipse to help me clean up some code to use Java generics
I'm trying to use the example taken from the http://java.sun.com/products/jfc/tsc/articles/treetable2/index.html , in which I've
Possible Duplicate: Inserting a Java string in another string without concatenation? In Object C

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.