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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:34:06+00:00 2026-05-12T16:34:06+00:00

Consider the following example. String str = new String(); str = "Hello"; System.out.println(str); //Prints

  • 0

Consider the following example.

String str = new String();

str  = "Hello";
System.out.println(str);  //Prints Hello

str = "Help!";
System.out.println(str);  //Prints Help!

Now, in Java, Strings are immutable. Then how come the object str can be assigned with a different value like "Help!". Isn’t this contradicting the immutability of strings in Java? Can anybody please explain me the exact concept of immutability?

Edit:

Ok. I am now getting it, but just one follow-up question. What about the following code:

String str = "Mississippi"; 
System.out.println(str); // prints Mississippi 

str = str.replace("i", "!"); 
System.out.println(str); // prints M!ss!ss!pp! 

Does this mean that two objects are created again ("Mississippi" and "M!ss!ss!pp!") and the reference str points to a different object after replace() method?

  • 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-12T16:34:06+00:00Added an answer on May 12, 2026 at 4:34 pm

    str is not an object, it’s a reference to an object. "Hello" and "Help!" are two distinct String objects. Thus, str points to a string. You can change what it points to, but not that which it points at.

    Take this code, for example:

    String s1 = "Hello";
    String s2 = s1;
    // s1 and s2 now point at the same string - "Hello"
    

    Now, there is nothing1 we could do to s1 that would affect the value of s2. They refer to the same object – the string "Hello" – but that object is immutable and thus cannot be altered.

    If we do something like this:

    s1 = "Help!";
    System.out.println(s2); // still prints "Hello"
    

    Here we see the difference between mutating an object, and changing a reference. s2 still points to the same object as we initially set s1 to point to. Setting s1 to "Help!" only changes the reference, while the String object it originally referred to remains unchanged.

    If strings were mutable, we could do something like this:

    String s1 = "Hello";
    String s2 = s1;
    s1.setCharAt(1, 'a'); // Fictional method that sets character at a given pos in string
    System.out.println(s2); // Prints "Hallo"
    

    Edit to respond to OP’s edit:

    If you look at the source code for String.replace(char,char) (also available in src.zip in your JDK installation directory — a pro tip is to look there whenever you wonder how something really works) you can see that what it does is the following:

    • If there is one or more occurrences of oldChar in the current string, make a copy of the current string where all occurrences of oldChar are replaced with newChar.
    • If the oldChar is not present in the current string, return the current string.

    So yes, "Mississippi".replace('i', '!') creates a new String object. Again, the following holds:

    String s1 = "Mississippi";
    String s2 = s1;
    s1 = s1.replace('i', '!');
    System.out.println(s1); // Prints "M!ss!ss!pp!"
    System.out.println(s2); // Prints "Mississippi"
    System.out.println(s1 == s2); // Prints "false" as s1 and s2 are two different objects
    

    Your homework for now is to see what the above code does if you change s1 = s1.replace('i', '!'); to s1 = s1.replace('Q', '!'); 🙂


    1 Actually, it is possible to mutate strings (and other immutable objects). It requires reflection and is very, very dangerous and should never ever be used unless you’re actually interested in destroying the program.

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

Sidebar

Ask A Question

Stats

  • Questions 222k
  • Answers 222k
  • 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 {$ ...} are not just comments but they are compiler… May 13, 2026 at 12:28 am
  • Editorial Team
    Editorial Team added an answer created streams for each functional software package All developers for… May 13, 2026 at 12:28 am
  • Editorial Team
    Editorial Team added an answer It is probably due to the text analyzer. You should… May 13, 2026 at 12:28 am

Related Questions

Consider the following example: import string,cgi,time from os import curdir, sep from BaseHTTPServer import
I have found the following resources on Balanced Matching for .net Regexes: http://weblogs.asp.net/whaggard/archive/2005/02/20/377025.aspx http://blogs.msdn.com/bclteam/archive/2005/03/15/396452.aspx
I have a string indexed array that I would like to remove an item
I get the basic principles of closures and lambda expressions but I'm trying 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.