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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:28:59+00:00 2026-05-27T02:28:59+00:00

How many String object are created I am studying for the SCJP I cant

  • 0

How many String object are created

I am studying for the SCJP I cant seem to get my head round this String problem. I seem to see several possible answers depending on how i look at a question.

In the following initialization, how many string objects are created?

String s1 = "A" + "B" + "C" + "D";
System.out.println(s1)

Initially i thought 5 objects, i.e.

"A"
"B"
"C"
"D"
"ABCD"

But then thinking about it i am not really sure because for example will the compiler concatenate "A" + "B" as one object? i.e creating 7 objects?

"A"
"B"
"C"
"D"
"AB"
"ABC"
"ABCD" 

Also, how many objects will be created if the code was changed to be

String s1 = new String("A" + "B" + "C" + "D");
System.out.println(s1);

And finally how about:

String s1 = "A";
String s2 = new String("A");

In the above example i think only 2 objects will be created

object 1 - "A"
object 2 - a String object that refers to the "A" object above.

Is this correct or will they not be related? i.e. the object referred to from the constant pool will be different from the one referred to by the s2 reference.

Thanks

Edit

Also, please note i am interested in knowing the total number of objects created including those that are discarded not just those that eventually end up in the constant pool.

Edit

Looking at Jon’s answer i might have totally misunderstood the way the objects are created. I know that a String is created only once in the constant pool and it is reused but im not sure of the process that goes through when the ‘final’ string is constructed. Here is the section from the book i am reading which seems to suggest that temporary objects get created which is a complete opposite to the answers here. (Or maybe the book is wrong or i misunderstood the book)

The code sample was

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

The question was

What is the output? For extra credit, how many String objects and how
many reference varibles were created prior to the println statement.

And the answer

The result of this code fragment is spring water 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”, “falls”(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

Thanks

  • 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-27T02:29:00+00:00Added an answer on May 27, 2026 at 2:29 am

    The compiler will concatenate the whole of “A” + “B” + “C” + “D” into a single constant – so in your first example, only a single string ends up created at all. That same string will be reused if you execute the same code multiple times. The constant is put in the class file, and when the class is loaded the VM checks whether an equal string is already in the string pool – so it will reuse it even if you have the same code in multiple classes.

    You can verify that only a single string is in the constant pool within the class with javap:

    javap -v Test
    
    Constant pool:
       #1 = Methodref   #6.#17     //  java/lang/Object."<init>":()V
       #2 = String      #18        //  ABCD
       #3 = Fieldref    #19.#20    //  java/lang/System.out:Ljava/io/PrintStream;
    

    However, here:

    String s1 = "A";
    String s2 = new String("A");
    

    you do end up with two separate string objects. One (the constant) will be reused every time you execute the code (and is shared between the two statements), and a new one will be created due to the constructor call each time.

    So for example, this method:

    public static void foo() {
        for (int i = 0; i < 5; i++) {
            String s1 = "A";
            String s2 = new String("A");
        }
    }
    

    … will end up using six string objects – one for the constant, and five new ones created each time you call the method.

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

Sidebar

Related Questions

I tried decoding this base64 string so many times but every time i get
I have created a string builder JavaScript object and I'm using it with many
There are many ways of converting a String to an Integer object. Which is
I tried many things but I always get cannot convert string to membershipuser from
I was asked this question: String s = abc; // creates one String object
I have a many-to-many relation with a linked table. See (simplified) schema below. Created
My application builds many objects in memory based on filenames (among other string based
I've got a class with many string arrays. I'd like to have one generic
Many people string together find and sed, or perl, or any number of other
I need to replace many different sub-string in a string in the most efficient

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.