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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:06:28+00:00 2026-06-04T06:06:28+00:00

String s = hello; String backup_of_s = s; s = bye; At this point,

  • 0
    String s = "hello";
    String backup_of_s = s;
    s = "bye";

At this point, the backup variable still contains the original value “hello” (this is because of String’s immutability right?).

But is it really safe to copy Strings with this method (which is of course not safe to copy regular mutable objects), or is better to write this? :

    String s = "hello";
    String backup_of_s = new String(s);
    s = "bye";

In other words, what’s the difference (if any) between these two snippets?


EDIT – the reason why the first snippet is safe:

Let me just explain things with a little more detail, based on the good answers already provided (which were essentially focused on the question of difference of performance between the 2 snippets):

Strings are immutable in Java, which means that a String object cannot be modified after its construction.
Hence,

String s = "hello"; creates a new String instance and assigns its address to s (s being a reference to the instance/object)

String backup_of_s = s; creates a new variable backup_of_s and initializes it so that it references the object currently referenced by s.

Note: String immutability guarantees that this object will not be modified: our backup is safe

Note 2: Java garbage collection mechanism guarantees that this object will not be destroyed as long as it is referenced by at least one variable (backup_of_s in this case)

Finally, s = "bye"; creates another String instance (because of immutability, it’s the only way), and modifies the s variable so that it now references the new object.

  • 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-04T06:06:30+00:00Added an answer on June 4, 2026 at 6:06 am

    Since strings are immutable, both versions are safe. The latter, however, is less efficient (it creates an extra object and in some cases copies the character data).

    With this in mind, the first version should be preferred.

    • 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 string: Hello &, how are you? I'm fine! Is
I have a string hello [world] this {is} a (test) I want to remove
Sorry as the question is bit lengthy. I have this string Hello how are
Consider this string hello awesome <a href= rel=external title=so awesome is cool> stuff stuff
I have following string hello[code:1], world [code:2], hello world I want to replace this
Let's say I have the string Hello. This string is obviously five characters in
I am trying to extract data from this String: Hello there. Blah blahblah blah
Button b1 = new Button(this); b1.setText(R.string.hello); RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.top_menu); //android:layout_alignParentRight=true layout1.addView(b1); Is
A table contains the string Hello world! Thinking of * as the ordinary wildcard
I have XML in the form of a String that contains <message>HELLO!</message> How can

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.