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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:58:33+00:00 2026-06-05T23:58:33+00:00

If we take a look at the String#substring method implementation : new String(offset +

  • 0

If we take a look at the String#substring method implementation :

new String(offset + beginIndex, endIndex - beginIndex, value);

We see that a new String is created with the same original content (parameter char [] value).

So the workaround is to use new String(toto.substring(...)) to drop the reference to the original char[] value and make it eligible for GC (if no more references exist).

I would like to know if there is a special reason that explain this implementation. Why the method doesn’t create herself the new shorter String and why she keeps the full original value instead?

The other related question is : should we always use new String(...) when dealing with substring?

  • 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-05T23:58:34+00:00Added an answer on June 5, 2026 at 11:58 pm

    I would like to know if there is a special reason that explain this implementation. Why the method doesn’t create herself the new shorter String and why she keeps the full original value instead?

    Because in most use-cases it is faster for substring() to work this way. At least, that’s what Sun / Oracle’s empirical measurements would have shown. By doing this, the implementation avoids allocating a backing array and copying characters to the array.

    This is only a non-optimization if you have to then copy the String to avoid a memory leakage problem. In the vast majority of cases, the substrings become garbage in a relatively short period of time, and there is no long-term leakage of memory.


    Hypothetically, the Java designers could have provided two versions of substring, one which behaved as currently, and the other that created a String with its own backing array. But that would encourage the developer to waste brain-cycles thinking about which version to use. And then there’s the problem of utility methods that build on substrings … like the Pattern / Matcher classes for instance. So I think it is a good thing that they didn’t.

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

Sidebar

Related Questions

Take a look at this code: public class Test { public static void main(String...
Please take a look at this example: #include <iostream> #include <vector> #include <string> using
Take a look at this: http://thebekker.dk/_skole/GFeksamen/ You can see the 2nd menu item show
Take a look at this class: public class MemorialPoint:IMemorialPoint,IEqualityComparer<MemorialPoint> { private string _PointName; private
Please take a look at my simple three-methods Service class that streams audio and
Take a look at WordprocessingDocument class in the DocumentFormat.OpenXml.Packaging I saw the Create method
First take a look at this question: Bash or GoogleCL: new line in a
please take a quick look at this function that I have found on the
Take a look at the following C# code: byte[] StringToBytesToBeHashed(string to_be_hashed) { byte[] to_be_hashed_byte_array
please take a look at the two following c statements printf(a very long string);

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.