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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:12:17+00:00 2026-06-10T03:12:17+00:00

Possible Duplicate: Java Strings and StringPool I created two Strings String s1=MyString; String s2=new

  • 0

Possible Duplicate:
Java Strings and StringPool

I created two Strings

String s1="MyString";
String s2=new String("MyString");
System.out.println(s1==s2);

it prints "false" . we know that String pool doesn’t create two Objects for same String literal.

Then what is happening here? It’s creating two different String Objects(literals) in String pool for same String literal “MyString”.

I know equals() method returns true here.

but when we use == it should compare two references and they should refer to the same
String Object in String constant pool.

Why It is not refering to the existing String object in the String pool even if it finds a match ?.

  • 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-10T03:12:18+00:00Added an answer on June 10, 2026 at 3:12 am

    The first one goes to the pool while the second one is stored on the heap.

    Use s2 = s2.intern(); to make it return true.

    When you do an intern() on the string, the JVM ensures that the string is present in the pool. If it doesn’t yet exist, it is created on the pool. Otherwise, the already existing instance is returned. I think this explains the == behaviour.

    String s1="MyString";
    String s2=new String("MyString");
    s2 = s2.intern();
    System.out.println(s1==s2);
    

    As a reference, here is what the String.intern() documentation says:

    /**
     * Returns a canonical representation for the string object.
     * <p>
     * A pool of strings, initially empty, is maintained privately by the
     * class <code>String</code>.
     * <p>
     * When the intern method is invoked, if the pool already contains a
     * string equal to this <code>String</code> object as determined by
     * the {@link #equals(Object)} method, then the string from the pool is
     * returned. Otherwise, this <code>String</code> object is added to the
     * pool and a reference to this <code>String</code> object is returned.
     * <p>
     * It follows that for any two strings <code>s</code> and <code>t</code>,
     * <code>s.intern()&nbsp;==&nbsp;t.intern()</code> is <code>true</code>
     * if and only if <code>s.equals(t)</code> is <code>true</code>.
     * <p>
     * All literal strings and string-valued constant expressions are
     * interned. String literals are defined in section 3.10.5 of the
     * <cite>The Java&trade; Language Specification</cite>.
     *
     * @return  a string that has the same contents as this string, but is
     *          guaranteed to be from a pool of unique strings.
     */
    public native String intern();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Java Strings: “String s = new String(”silly“);” I was going through some
Possible Duplicate: Java Strings: “String s = new String(”silly“);” What is the purpose of
Possible Duplicate: How do I compare strings in Java? String s1 = andrei; String
Possible Duplicates: Java String declaration Java Strings: String s = new String(silly); What is
Possible Duplicate: How do you compare two version Strings in Java? I've 2 strings
Possible Duplicate: Why can't strings be mutable in Java and .NET? Why .NET String
Possible Duplicate: What is the purpose of the expression “new String(…)” in Java? What's
Possible Duplicate: Strings are objects in Java, so why don't we use 'new' to
Possible Duplicate: Java String.equals versus == I know it' a dumb question but why
Possible Duplicate: Java: how to check that a string is parsable to a double?

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.