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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:23:57+00:00 2026-05-30T05:23:57+00:00

In java’s documentation it says that in the below example, the condition will be

  • 0

In java’s documentation it says that in the below example, the condition will be true:

String a = new String("ABC");
String b = new String("ABC");

if (a.intern() == b.intern())
{
 ....
}

I wanted to know, if that is still true when considering that a and b are defined in different Threads, or even different ClassLoaders?

This question rose when I needed an ability to synchronize a block that loads a certain configuration based on an entity’s name, so I wanted to do something like:

synchronized (entityName.intern())
{
}

I’m not sure this is a good practice, so I’m probably not going to pursue this direction – but the question still interests me.

  • 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-30T05:23:58+00:00Added an answer on May 30, 2026 at 5:23 am

    If on different threads, yes, the condition will be true.

    If on different class loaders, I wouldn’t count on the condition being true. (But are you really loading two copies of String using different class loaders?) The documentation says that intern is implemented within String, using its own cache. From the String#intern documentation:

    Returns a canonical representation for the string object.

    A pool of strings, initially empty, is maintained privately by the class String.

    (My emphasis)

    So if you somehow loaded the String class twice using different class loaders (I’m not sure how you would do that, but I bet there’s a way), then the two String classes would each have its own cache — in theory. However, an implementation might not make that fine a distinction. intern is a native method in the Oracle JVM, using a symbol table implemented in C++. I haven’t followed the logic through closely enough to see whether in the edge case you’re talking about, the two instances of String in the same JVM would share the same symbol table or not. But at that point, we’re looking at implementation, which could vary. The documentation suggests no, they wouldn’t be the same string.

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

Sidebar

Related Questions

Java docs says that a TreeSet keeps its elements ordered internally. Here what does
Java has a convenient split method: String str = The quick brown fox; String[]
Java Newbie here. I have a JFrame that I added to my netbeans project,
Java SE 6 (64 bit only) is now on OS X and that is
Java is nearing version 7. It occurs to me that there must be plenty
Java design question. I have an object that needs to maintain sets of say
Java File for(..){ java.util.List opslist = new ArrayList(); opr.setOperationName(operation.getName()); //gets operation name (iterate and
Java Concurrency in Practice gives the following example of an unsafe class which due
Java Exception: http://hastebin.com/yiwecefifi.avrasm I have an object, that I call Category, that my fragment
I've got a string that has curly quotes in it. I'd like to replace

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.