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

  • SEARCH
  • Home
  • 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 6328869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:32:28+00:00 2026-05-24T17:32:28+00:00

How do String objects work in Java? How does term immutable exactly apply to

  • 0

How do String objects work in Java? How does term “immutable” exactly apply to string objects? Why don’t we see modified string after passing through some method, though we operate on original string object value?

  • 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-24T17:32:31+00:00Added an answer on May 24, 2026 at 5:32 pm

    a String has a private final char[] . when a new String object is created, the array is also created and filled. it cannot be later accessed [from outside] or modified [actually it can be done with reflection, but we’ll leave this aside].

    it is “immutable” because once a string is created, its value cannot be changed, a “cow” string will always have the value “cow”.

    We don’t see modified string because it is immutable, the same object will never be changed, no matter what you do with it [besides modifying it with reflection]. so “cow” + ” horse” will create a new String object, and NOT modify the last object.

    if you define:

    void foo(String arg) {
      arg= arg + " horse";
    }
    

    and you call:

    String str = "cow";
    foo(str);
    

    the str where the call is is not modified [since it is the original reference to the same object!] when you changed arg, you simply changed it to reference another String object, and did NOT change the actual original object. so str, will be the same object, which was not changed, still containing "cow"

    if you still want to change a String object, you can do it with reflection. However, it is unadvised and can have some serious side-affects:

        String str = "cow";
        try { 
        Field value = str.getClass().getDeclaredField("value");
        Field count = str.getClass().getDeclaredField("count");
        Field hash = str.getClass().getDeclaredField("hash");
        Field offset = str.getClass().getDeclaredField("offset");
        value.setAccessible(true);
        count.setAccessible(true);
        hash.setAccessible(true);
        offset.setAccessible(true);
        char[] newVal = { 'c','o','w',' ','h','o','r','s','e' };
        value.set(str,newVal);
        count.set(str,newVal.length);
        hash.set(str,0);
        offset.set(str,0);
        } catch (NoSuchFieldException e) {
        } catch (IllegalAccessException e) {}
        System.out.println(str);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm puzzled as to why get(Object obj) does not work with instance variables (instance
Possible Duplicate: Java generics and array initialization How does one instantiate an array of
I'm trying to remove values of datatype properties of an instance through an interface
Consider the following simple code import java.util.*; public class MainTest<T extends Object1<?,?>> { List<T>
I am writing Web app in java using Spring Web MVC framework. Somehow validation
I have a tri-state Boolean attribute (true, false, and null) in of one of
i have a database table viewmodule with a FK to itself (parent_id) to allow
Consider the following Scala code: package scala_java object MyScala { def setFunc(func: Int =>
well, i needed to convert a project made with eclipse to an SBT project.

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.