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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:27:18+00:00 2026-06-04T04:27:18+00:00

Ok, this question is an extension of this question Java Strings: "String s =

  • 0

Ok, this question is an extension of this question

Java Strings: "String s = new String("silly");"

The above question asked the same question as this one, but I have a new doubting point.

According to Effective Java and the answers of above question, we should not do String s = new String("a new string");, because that will create unnecessary object.

I am not sure about this conclusion, because I think Java is doing automatic string interning, which means for a string, anyway there is only one copy of it in the memory.

So let’s see String s = new String("a new string");.

"a new string" is already a string which has been created in the memory.

When I do String s = new String("a new string");, then the s is also "a new string". So according to automatic string interning, s should be pointed to the same memory address of "a new string", right?

Then how can we say we create unnecessary objects?

  • 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-04T04:27:19+00:00Added an answer on June 4, 2026 at 4:27 am
    
    String a = "foo"; // this string will be interned
    String b = "foo"; // interned to the same string as a
    boolean c = a == b; //this will be true
    String d = new String(a); // this creates a new non-interned String
    boolean e = a == d; // this will be false
    String f = "f";
    String g = "oo";
    String h = f + g; //this creates a new non-interned string
    boolean i = h == a // this will be false
    File fi = ...;
    BufferedReader br = ...;
    String j = br.readLine();
    boolean k = a == j; // this will always be false. Data that you've read it is not automatically interned
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is an extension of this one . I have many values, as
This is an extension of question: Order of the initialization in Java so in
This question is an extension to the one raised here: Using factory_girl in Rails
This is an extension for this question asked an hour ago. We cannot modify
This is an extension / next step of this question I asked a few
This is an extension of this question: SQLite problem selecting two columns as one
this question is a extension of one made in Best Solution Architecture , basically
This is a continuation of the question Java rounded Swing JButton . I have
this question is very similar to this one but my case is a bit
Note: This is an extension of an earlier question I asked here: Do additional

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.