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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:58:40+00:00 2026-05-29T10:58:40+00:00

After reading the android documentation about String, which includes this: This class is implemented

  • 0

After reading the android documentation about String, which includes this:

This class is implemented using a char[]. The length of the array may
exceed the length of the string. For example, the string “Hello” may
be backed by the array [‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘W’. ‘o’, ‘r’, ‘l’,
‘d’] with offset 0 and length 5.

Multiple strings can share the same char[] because strings are
immutable. The substring(int) method always returns a string that
shares the backing array of its source string. Generally this is an
optimization: fewer character arrays need to be allocated, and less
copying is necessary. But this can also lead to unwanted heap
retention. Taking a short substring of long string means that the long
shared char[] won’t be garbage until both strings are garbage. This
typically happens when parsing small substrings out of a large input.
To avoid this where necessary, call new
String(longString.subString(…)). The string copy constructor always
ensures that the backing array is no larger than necessary.

So my question is : What happens when we create a String as the substring of a StringBuilder? Are there any memory considerations as well?
Which leads to my other question :
Is the combo StringBuilder / String a good choice when selecting small parts of a large text and programming with rapidity considerations in mind?

Thank you for your attention and your time, have a good day!

  • 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-29T10:58:41+00:00Added an answer on May 29, 2026 at 10:58 am

    Here: AbstractStringBuilder.substring(int, int) (StringBuilder extends this class) in OpenJDK does exactly what’s described in your snippet: creates a new String to avoid retaining the memory for the whole string substring was called on. Let us hope that Dalvik’s implementation is the same 🙂

    Actually, you get the idea how to check this, here are instructions on how to get Dalvik sources: http://source.android.com/source/downloading.html

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

Sidebar

Related Questions

After reading the Android NDK documentation i am a bit confused on this issue.
After reading some posts like this one: Choose File Dialog It appears that Android
I am having difficulties understanding AsyncTask, even after reading everything about it on Developer.Android.
I'm new to Android. I've been reading several posts and documentation about Threads/Services, and
After reading a few articles about Android configuration, I still don't know how to
After reading the Head First Design Patterns book and using a number of other
After reading this question , I was reminded of when I was taught Java
After reading a bit more about how Gnutella and other P2P networks function, I
After reading Practical Common Lisp I finally understood what the big deal about macros
After reading this description of late static binding (LSB) I see pretty clearly what

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.