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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:08:29+00:00 2026-05-26T01:08:29+00:00

I am studying for the SCJP exam and I have a sample set of

  • 0

I am studying for the SCJP exam and I have a sample set of questions that I am working through.

One questions answer I am unsure about and was hoping someone here might be able to help me put this question to bed.

Here is the question,

Given:

11. public String makinStrings() {
12.    String s = "Fred";
13.    s = s + "47";
14.    s = s.substring(2, 5);
15.    s = s.toUpperCase();
16.    return s.toString();
17. }

How many String objects will be created when this method is invoked?

A. 1
B. 2
C. 3
D. 4
E. 5
F. 6

Thank you in advance for any help offered.
I greatly appriciate it.

  • 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-26T01:08:30+00:00Added an answer on May 26, 2026 at 1:08 am

    Let’s go through it line by line.

    Line 11

    An easy start, no strings created here.

    Line 12

    We’re assigning the String "Fred" to s. Although it looks like a String is created here, this string will live in the constant pool. The JVMS section 2.17.6 Creation of New Class Instances guarantees that the objects for string literals will at latest be created when the surrounding class is loaded, which by definition is before the method is invoked. So no new string objects are created on this line.

    Line 13

    The literal string "47" is referenced, which again will have been created statically (as above). However there’s also the invocation of the + operator, which will create a new String in order to hold the result of the concatenation. So that’s the first string created.

    Line 14

    The substring method does indeed create a new String. It shares the underlying character array with its parent – and so takes up hardly any extra memory – but since Strings are immutable, each different string representation requires a different String object. (This is probably a gotcha – my first instinctive response was “ah, string created by substring are special” but of course it still has to create a new object).

    Line 15

    As above – the uppercase representation is different, so a new String must be created to hold the result.

    Line 16

    Strings override the toString() method to simply return this – hence no additional String is created.

    The scores on the doors

    By my count that’s three String objects created during this method (with two of those objects sharing the same underlying character array, and with two pre-existing objects referenced for the string literals).

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

Sidebar

Related Questions

While studying about JMX, I have seen one of the important feature of it
I'm studying for Java Programmer Certification (SCJP) exam. A question about exceptions, when handle
I am studying the SCJP, and while studying I have found an exercise that
I'm studying for SCJP, and my book (Sierra's) says about the Formatter class that
I'm studying for the SCJP/OCPJP and I came across a sample question that seams
I started studying Haskell one week ago and have one strange problem. I created
While studying for the SCJP 6 exam, I ran into this question in a
Studying up for interviews and going through these questions . 8.Given a cube of
Studying C conversions I found a paragraph about Integer promotions that I haven't understand
im studying for my programming final exam. I have to write a program which

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.