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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:23:39+00:00 2026-06-03T16:23:39+00:00

I read in Kathy Sierra book that when we create String using new operator

  • 0

I read in Kathy Sierra book that when we create String using new operator like String s = new String(“abc”) In this case, because we used the new keyword, Java will create a new String object in normal (nonpool) memory, and s will refer to it. In addition, literal “abc” will be placed in the pool.

intern() says that if String pool already contains a string then the string from the pool is returned Otherwise, the String object is added to the pool and a reference to this String object is returned.

If string “abc” when created using new also placed the string in the pool, then wht does intern() says that string from the pool is returned if String pool contains the string otherwise the string object is added to the pool.

Also I want to know if we create a String using new then actually how many objects get created?

  • 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-03T16:23:40+00:00Added an answer on June 3, 2026 at 4:23 pm

    TL;DR: If you ever really need to do new String("abc"), you’ll know you need to and you’ll know why. It’s so rare that it’s almost valid to say you never need to. Just use "abc".


    The long version:

    When you have the code new String("abc") the following things occur at various times:

    • When the class containing that code is loaded, if a string with the characters "abc" is not already in the intern pool, it’s created and put there.
    • When the new String("abc") code is run:
      • A reference to the "abc" string from the intern pool is passed into the String constructor.
      • A new String object is created and initialized by copying the characters from the String passed into the constructor.
      • The new String object is returned to you.

    If string “abc” when created using new also placed the string in the pool, then why does intern() says that string from the pool is returned if String pool contains the string otherwise the string object is added to the pool.

    Because that’s what intern does. Note that calling intern on a string literal is a no-op; string literals are all interned automatically. E.g.:

    String s1 = "abc";               // Get a reference to the string defined by the literal
    String s2 = s1.intern();         // No-op
    System.out.println(s1 == s2);    // "true"
    System.out.println(s1 == "abc"); // "true", all literals are interned automatically
    

    Also I want to know if we create a String using new then actually how many objects get created?

    You create at least one String object (the new, non-interned one), and possibly two (if the literal wasn’t already in the pool; but again, that bit happens earlier, when the class file’s literals are loaded):

    String s1 = "abc";            // Get a reference to the string defined by the literal
    String s2 = new String(s1);   // Create a new `String` object (guaranteed)
    System.out.println(s1 == s2); // "false"
    String s3 = s2.intern();      // Get the interned version of the string with these characters
    System.out.println(s1 == s3); // "true"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read this in Kathy Sierra's book: Local variables are sometimes called stack, temporary,
I read this question's answers that explain the order of the LINQ to objects
I read a long time ago about a rule that using an object we
Read this question carefully because I am not asking how to get rid of
I read this and got really interested: Validating date format using regular expression so
I read how this can be made to work using forward declarations. class A
I read all Questions on stackoverflow that could give me a hint with this
I read somewhere once that the modulus operator is inefficient on small embedded devices
I read in this article that a company has created a software capable of
Read in some blog that GC in Android happens on main(UI) thread, this may

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.