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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:30:24+00:00 2026-06-04T19:30:24+00:00

I always thought if I do String s = Hello World.substring(0, 5) , then

  • 0

I always thought if I do String s = "Hello World".substring(0, 5), then I just get a new string s = "Hello". This is also documented in the Java API doc: “Returns a new string that is a substring of this string”.

But when I saw the following two links, I began to doubt.

What is the purpose of the expression "new String(…)" in Java?

String constructor considered useless turns out to be useful after all

Basically, they say if I use String s = "Hello World".subString(0, 5), I still get a String which holds “Hello World”‘s char array.

Why? Does Java really implement substring in this way? Why in this way? Why not just return a brand new shorter 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-04T19:30:25+00:00Added an answer on June 4, 2026 at 7:30 pm

    It’s supposed to be an efficiency measure. i.e. when you’re taking a substring you won’t create a new char array, but merely create a window onto the existing char array.

    Is this worthwhile ? Maybe. The downside is that it causes some confusion (e.g. see this SO question), plus each String object needs to carry the offset info into the array, even if it’s not used.

    EDIT: This behaviour has now changed as of Java 7. See the linked answer for more info

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

Sidebar

Related Questions

Let's say I have this code in Java: HashSet<String> wordSet = new HashSet<String>(); String
I always thought that a look-behind assertion in Java's regex-API (and many other languages
I always thought that *&p = p = &*p in C. I tried this
I always thought an std::string was implemented as an STL wrapper for a C
Don't get me wrong, I want them to get saved. But I always thought
The comment to this answer got me wondering. I've always thought that C was
One of columns(id) in Salesforce custom object is String type though it always contains
I always thought that the nullable types of the .NET framework where nothing but
I always thought in regex, ^ and # like 'verb' in must follow something,
I always thought they were about the same thing but someone pointed me out

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.