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

  • Home
  • SEARCH
  • 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 7187821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:55:55+00:00 2026-05-28T18:55:55+00:00

I was asked this question: String s = abc; // creates one String object

  • 0

I was asked this question:

String s = "abc"; // creates one String object and one
          // reference variable
In this simple case, "abc" will go in the pool and s will refer to it.
String s = new String("abc"); // creates two objects,
                 // and one reference variable*

Based on above details how many String objects and how many reference variables were created prior to the println statement of below code?

String s1 = "spring ";
String s2 = s1 + "summer ";
s1.concat("fall ");
s2.concat(s1);
s1 += "winter ";
System.out.println(s1 + " " + s2);

My answer was
The result of this code fragment is spring winter spring summer

There are two reference variables, s1 and s2. There were a total of eight String objects
created as follows: “spring”, “summer ” (lost), “spring summer”, “fall” (lost), “spring
fall” (lost), “spring summer spring” (lost), “winter” (lost), “spring winter” (at this point “spring” is lost).

Only two of the eight String objects are not lost in this process.

Is it correct?

  • 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-28T18:55:56+00:00Added an answer on May 28, 2026 at 6:55 pm

    Answer is: 2 references and 8 objects.

    String s1 = "spring "; //One reference and 1 object in string pool. (if it didn't exist already)
    
    String s2 = s1 + "summer "; //Two references and 3 objects
    
    s1.concat("fall "); //Two references and 5 objects
    
    s2.concat(s1); //Two references and 6 objects
    
    s1 += "winter "; //Two references and 8 objects
    
    System.out.println(s1 + " " + s2);
    

    Now your question: How does Java handle String objects in memory?

    Java provides two ways to create object of class String.

    1. String str1 = “OneString”;

    In this case, JVM searches the string pool to see if equivalent string exist already. if yes, returns the reference to same. if not, adds it to string pool and returns the reference. So a new object may be created OR may not be.

    1. String str1 = new String(“OneString”);

    Now, JVM has to create an object on heap. due to new. it doesn’t matter if OneString is already present in string pool.

    You can also put a string to pool:

    You can call intern() on a String object. This will put the String object in the pool if it is not already there, and return the reference to the pooled string. (If it was already in the pool, it just returns a reference to the object that was already there).

    You may like to look at following links:

    What is the Java string pool and how is "s" different from new String("s")?

    Questions about Java's String pool

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

Sidebar

Related Questions

I was asked this question in an interview: Is string a reference type or
Recently I asked this question: Removing up to 4 spaces from a string and
I asked this question yesterday about storing a plot within an object. I tried
I have asked this question about using the a Linq method that returns one
In this question I asked for a regex to turn /abc/def/ghi into { /abc/def/ghi
(I asked this question in another way , and got some interesting responses but
I asked this question before, Here however I think I presented the problem poorly,
I asked this question a while back but now I'm looking to implement an
I asked this question: Serial Port (rs232) in Mono for multiple platforms and this
I asked this question: Is ASP.NET MVC destined to replace Webforms? and a few

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.