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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:18:17+00:00 2026-05-10T23:18:17+00:00

While looking at online code samples, I have sometimes come across an assignment of

  • 0

While looking at online code samples, I have sometimes come across an assignment of a String constant to a String object via the use of the new operator.

For example:

String s; ... s = new String('Hello World'); 

This, of course, compared to

s = 'Hello World'; 

I’m not familiar with this syntax and have no idea what the purpose or effect would be. Since String constants typically get stored in the constant pool and then in whatever representation the JVM has for dealing with String constants, would anything even be allocated on the heap?

  • 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. 2026-05-10T23:18:17+00:00Added an answer on May 10, 2026 at 11:18 pm

    The one place where you may think you want new String(String) is to force a distinct copy of the internal character array, as in

    small=new String(huge.substring(10,20)) 

    However, this behavior is unfortunately undocumented and implementation dependent.

    I have been burned by this when reading large files (some up to 20 MiB) into a String and carving it into lines after the fact. I ended up with all the strings for the lines referencing the char[] consisting of entire file. Unfortunately, that unintentionally kept a reference to the entire array for the few lines I held on to for a longer time than processing the file – I was forced to use new String() to work around it, since processing 20,000 files very quickly consumed huge amounts of RAM.

    The only implementation agnostic way to do this is:

    small=new String(huge.substring(10,20).toCharArray()); 

    This unfortunately must copy the array twice, once for toCharArray() and once in the String constructor.

    There needs to be a documented way to get a new String by copying the chars of an existing one; or the documentation of String(String) needs to be improved to make it more explicit (there is an implication there, but it’s rather vague and open to interpretation).

    Pitfall of Assuming what the Doc Doesn’t State

    In response to the comments, which keep coming in, observe what the Apache Harmony implementation of new String() was:

    public String(String string) {     value = string.value;     offset = string.offset;     count = string.count; } 

    That’s right, no copy of the underlying array there. And yet, it still conforms to the (Java 7) String documentation, in that it:

    Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string. Unless an explicit copy of original is needed, use of this constructor is unnecessary since Strings are immutable.

    The salient piece being ‘copy of the argument string‘; it does not say ‘copy of the argument string and the underlying character array supporting the string’.

    Be careful that you program to the documentation and not one implementation.

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

Sidebar

Ask A Question

Stats

  • Questions 171k
  • Answers 171k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I created a custom base Controller class and added a… May 12, 2026 at 2:16 pm
  • Editorial Team
    Editorial Team added an answer This is typical of ActionServlet. It maps all exception to… May 12, 2026 at 2:16 pm
  • Editorial Team
    Editorial Team added an answer I found the problem ... I marked the class as… May 12, 2026 at 2:16 pm

Related Questions

I'm looking at making some mobile apps in my spare time and I want
A while back, online apps used to say, do not click submit more than
I have a java program that runs a bunch of queries against an sql
This is going to be a little long and rambly, but I want to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.