> it prints testhow
[ _ ] You are aware that = is the assignment operator.
[ _ ] You know that "String1 + String2" compiles to "new StringBuffer(String1).append(String2).toString())"
The point of immutability is that you can not alter contents. Strings are immutable. Nothing you can do short of reflection changes a string's value.
consider string s
when u say "string s;".......that becomes a reference...
now wen u say s="test"....it creates an object(u need not say new String for this class)
now if u say .....
s="test22"
the reference of s now points to test22 and the earlier refernce of test is soon garbage collected...
StringBuffer is mutable ....coz u can append to it or update...or watever the methods allow u to do..
so...
StringBuffer s=new StringBuffer("pass some string");
s.append("hello");
so this now gives s as "pass some stringhello";
hope u get that :)
> now wen u say s="test"....it creates an object
No, it doesn't. Since "test" is a string literal, the String object was created when the class was loaded. The above statement simply assigns a reference to the already existing String to the variable s.
> now if u say .....
> s="test22"
> the reference of s now points to test22 and the
> earlier refernce of test is soon garbage
> collected...
No. References are not GCed. Only objects are GCed. String objects in the constant pool ("test", above) are not GCed.
> StringBuffer is mutable ....coz u can append to it or
> update...or watever the methods allow u to do..
> so...
Mutable means you can change the state. You can change the state of StringBuffer, but not of String.
Theres a thing in java called the literal pool where strings are stored, each time you create a string that is unique a new object is created.
So if you have a string that says test and new object with the content test is put in the literal pool, say you add the word new to it, then a new object in the literal pool will be created that says newpool and pointer will refer to that. The funny thing about the literal pool is, with int if you say int a = 1 and int b = 1 and you do the argument if int a = int b the answer will be false and they are two different variables, they are .equals though, but since strings are stored in the literal pool, if you say string a = pete and string b = pete and you says if string a = string b the answer will be true as they are both pointing to the same object in the literal pool. Bit confusing i know, but thats how it works.
> The funny thing about the
> literal pool is, with int if you say int a = 1 and
> int b = 1 and you do the argument if int a = int b
> the answer will be false and they are two different
> variables, t
The funny thing is that this is utterly wrong. ints are primitives, and thus == tests for equality and not for referential identity.
int a = 1;
int b = 1;
System.out.println(a == b); // will print true
> if you say string a = pete and string b = pete and you says if string a = > string b the answer will be true
String a = "pete";
String b = new String("pete");
System.out.println(a == b); // false..
Not to contradict, but to show the difference.