String is immutable?

Posted on 2011-10-17
Last Modified: 2013-11-23

It says print should have -->   B: TEST, A: TEST

But I get -->   B: TEST, A: Test

Q1: What am I missing?
Q2: Could you explain why and how String is immutable?

Thank you.
public class StringTest {
    public static void main(String[] args){
        // at:
        // it says print line should have-->   B: TEST, A: TEST
        // but I get -->   B: TEST, A: Test
        String A = "Test";
        String B = "Test";
        //System.out.println("B: "+B+", A: "+A+"\n");
        System.out.println("B: "+"Test".toUpperCase()+", A: "+A+"\n");

Open in new window

Question by:Mike Eghtebas
    LVL 47

    Expert Comment

    What else can you have:
     String A = "Test";
     System.out.println("B: "+"Test".toUpperCase()+", A: "+A+"\n");
    Of course:
    B: TEST,A: Test

    LVL 47

    Expert Comment


    I ran it - nothing else can be printed - mutable or immutabe:
     String A = "Test";
     System.out.println("B: "+"Test".toUpperCase()+", A: "+A+"\n");


    B: TEST, A: Test
    LVL 47

    Expert Comment

    They write here: "Imagine" - and how bad it would have been.
    Nothing like that happens in fact.
    1)IMAGINE StringPool facility without making string immutable , its not possible at all because in case of string pool one string object/literal e.g. "Test" has referenced by many reference variables , so if any one of them change the value others will be automatically gets affected i.e. lets say

    String A = "Test"
    String B = "Test"

    Now String B called "Test".toUpperCase() which change the same object into "TEST" , so A will also be "TEST" which is not desirable.
    LVL 47

    Accepted Solution

    No imagine our life if i'm writing:

    String A = "Test;
    String B = "Test";

    and then

    B = B.toUpperCase();

    and after that A would also become uppercase?

    No, that's absolutely not possible.

    Java does mainatin some pool of strings and may reuse some memory,
    and initially they can be represented by one area in memory
    but it would take care that it would not happen this way, no worries.
    So it woul create another string ion another place
    would set it to upper case and then repoint the pointer for B,, but not for A.

    So we should not worry about it.

    LVL 33

    Author Closing Comment

    by:Mike Eghtebas
    LVL 47

    Expert Comment

    And immutable jsut means that
    when it does
    B = B.toUpperCase();

    it does not go to every charcater and chnege the particular memory cell
    from lower to upper, but rather creates a new String in a new place
    with uppercase chracters and then changes tha pointer, so that in our code pointer
    associated with B will be now pointing to uppercase string in iits new place
    (so our varuiable in the program continues to be B, but teh actual numeric memory address
    form this point will in fact be different).
    Well, this is of course some simplification relative tio the physical processes with jvm interperting, etc,
    but this undderstanding reflect the reality sufficiently well, so that
    we could understand what is going on with our string variables
    when we are writing java code

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Suggested Solutions

    Title # Comments Views Activity
    hash value 2 29
    countPairs challenge 7 36
    Exception after setting jdbc session management 2 16
    word0 challenge 4 37
    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
    This video teaches viewers about errors in exception handling.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now