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 3280152
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:40:20+00:00 2026-05-17T19:40:20+00:00

I understand the basic idea of java’s String interning, but I’m trying to figure

  • 0

I understand the basic idea of java’s String interning, but I’m trying to figure out which situations it happens in, and which I would need to do my own flyweighting.

Somewhat related:

  • Java Strings: “String s = new String(”silly“);”
  • The best alternative for String flyweight implementation in Java never quite got answered

Together they tell me that String s = "foo" is good and String s = new String("foo") is bad but there’s no mention of any other situations.

In particular, if I parse a file (say a csv) that has a lot of repeated values, will Java’s string interning cover me or do I need to do something myself? I’ve gotten conflicting advice about whether or not String interning applies here in my other question


The full answer came in several fragments, so I’ll sum up here:

By default, java only interns strings that are known at compile-time. String.intern(String) can be used at runtime, but it doesn’t perform very well, so it’s only appropriate for smaller numbers of Strings that you’re sure will be repeated a lot. For larger sets of Strings it’s Guava to the rescue (see ColinD’s answer).

  • 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-05-17T19:40:21+00:00Added an answer on May 17, 2026 at 7:40 pm

    Don’t use String.intern() in your code. At least not if you might get 20 or more different strings. In my experience using String.intern slows down the whole application when you have a few millions strings.

    To avoid duplicated String objects, just use a HashMap.

    private final Map<String, String> pool = new HashMap<String, String>();
    
    private void interned(String s) {
      String interned = pool.get(s);
      if (interned != null) {
        return interned;
      pool.put(s, s);
      return s;
    }
    
    private void readFile(CsvFile csvFile) {
      for (List<String> row : csvFile) {
        for (int i = 0; i < row.size(); i++) {
          row.set(i, interned(row.get(i)));
          // further process the row
        }
      }
      pool.clear(); // allow the garbage collector to clean up
    }
    

    With that code you can avoid duplicate strings for one CSV file. If you need to avoid them on a larger scale, call pool.clear() in another place.

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

Sidebar

Related Questions

I am digging into LINQ--trying to understand basic models (it seems pretty cool to
Could someone explain? I understand the basic concepts behind them but I often see
I'm guessing there's something really basic about C# inheritance that I don't understand. Would
I'm trying to understand why this does not work. (Basic example with no validation
I don't truly understand some basic things in C like dynamically allocating array of
I have some questions about basic CSS that I was unable to understand or
I understand what System.WeakReference does, but what I can't seem to grasp is a
I understand how I can change the dns settings for my domains by editing
I understand how JS is run and I think I understand most of the
I understand that some countries have laws regarding website accessibility. In general, what are

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.