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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:48:23+00:00 2026-06-06T06:48:23+00:00

As almost everybody knows strings in Java are immutable. Recently I discovered something that

  • 0

As almost everybody knows strings in Java are immutable. Recently I discovered something that might suggest that it’s not always true. Let’s try out this code:

System.out.println("-------- BEFORE MODIFICATIONS --------");
String beforeTest = new String("Original");
System.out.println(beforeTest);
java.lang.reflect.Field valueField = String.class.getDeclaredField("value");
valueField.setAccessible(true);
valueField.set("Original", "Modified".toCharArray());
System.out.println("-------- AFTER MODIFICATIONS --------");
System.out.println(beforeTest);
System.out.println("Original");
String test = new String("Original");
System.out.println(test);
String test2 = new String("Original 2");
System.out.println(test2);

the output would be:

-------- BEFORE MODIFICATIONS --------
Original
-------- AFTER MODIFICATIONS --------
Original
Modified
Modified
Original 2

How does this trick work? How does the JVM know which objects should be changed and which not? What mechanism are under the hood of this trick? Why already created beforeTest string was not changed? Does this trick really derogate the strings are immutable principle?

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

    String literals are interned into a pool. This means that when you write

    String s1 = "Foo";
    String s2 = "Foo";
    String s3 = new String("Foo");
    

    s1 and s2 refer to the same String object, and s3 refers to another one, backed by another char array.

    In your code, you violate String’s invariants by modifying the private char array holding the characters of the “Original” String literal instance. But since beforeTest refers to another String instance, it’s not modified.

    Immutability is achieved by keeping fields private into an object, and not providing any method to modify this private state. By using reflection, you break all the rules of encapsulation, and you can thus violate immutability.

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

Sidebar

Related Questions

I use the famous boilerplate code that almost everybody uses for handling cellForRowAtIndexPath: UITableViewCell
The main culprit behind this question is of course IE6, (almost) everybody agrees that
Almost every article I read told me that you can't have chdir in Java.
Almost finished my java web app. All working fine. Whats next. I need now
Almost every major news media outlet is reporting that Apple will stop using Google
Almost all of the examples in the jQuery tutorials that I've read, usually use
Almost any mature program that involves text implements double click to select the word
I get the feeling almost everybody uses an editor (Vim, Notepad++, etc.) for the
Almost unconsciously I hit the keyboard build macro that builds my entire solution. This
Almost everywhere I worked I met lots of people who didn't care that they

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.