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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:33:32+00:00 2026-06-14T01:33:32+00:00

How many String objects will the following code produce? String s1=Hello; //Hello is in

  • 0

How many String objects will the following code produce?

String s1="Hello"; //"Hello" is in String Pool Object #1
String s2 = s1.substring(2,3);//String object #2
String s3 = s1.toString();
String s4 = new StringBuffer(s1).toString(); //String Object #3

This is the question to one practice Java book I am reading.
There is no answer, so I am not sure if my answer is right. Are there 3 or 5 string objects created? Does toString() create a new String object? I looked up online and found that toString() “Returns a string representation of the object”. I don’t quite understand what this means.

  • 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-14T01:33:34+00:00Added an answer on June 14, 2026 at 1:33 am

    I think you are correct. Number of String objects would be 3 NOT 5.

    s1.toString(); javadoc says:

    This object (which is already a string!) is itself returned.

    new StringBuffer(s1)
    

    creates new String object.

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

Sidebar

Related Questions

I have the following line in my code: object[] inputs = new object[] {input1,
Possible Duplicate: How to know how many objects will be created with the following
My application builds many objects in memory based on filenames (among other string based
There are many ways of converting a String to an Integer object. Which is
I have the following string repeated many times (about 10 times) <br /> <a
I tried decoding this base64 string so many times but every time i get
you will think: "This problem has been solved many, many times. So why doesn't
How many String object are created I am studying for the SCJP I cant
Which of the following code is fastest/best practice for converting some object x? int
Imagine a method with the following signature: public void ExampleMethod(string id, object data, ref

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.