Mike Eghtebas
asked on
String is immutable?
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.
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");
}
}
I ran it - nothing else can be printed - mutable or immutabe:
String A = "Test";
System.out.println("B: "+"Test".toUpperCase()+", A: "+A+"\n");
output:
B: TEST, A: Test
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.
-----
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.
-----
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks
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
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
String A = "Test";
System.out.println("B: "+"Test".toUpperCase()+", A: "+A+"\n");
Of course:
B: TEST,A: Test