Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


String is immutable?

Posted on 2011-10-17
Medium Priority
Last Modified: 2013-11-23
At: http://javarevisited.blogspot.com/2010/10/why-string-is-immutable-in-java.html

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: http://javarevisited.blogspot.com/2010/10/why-string-is-immutable-in-java.html
        // 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
  • 5
LVL 47

Expert Comment

ID: 36982392
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

ID: 36982404

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

ID: 36982450
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.
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 47

Accepted Solution

for_yan earned 2000 total points
ID: 36982531
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 34

Author Closing Comment

by:Mike Eghtebas
ID: 36982572
LVL 47

Expert Comment

ID: 36982598
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

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month15 days, 11 hours left to enroll

580 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