I am learning about difference between String and StringBuffer
classes. In a book it sayes that a String class is immutable, that
means that one an instance of String class is created, the string it
contais cannot be changed, only reference will change to point to a
different string. So results will be different in the following code
depending is a, b are defined as String or StringBuffer:
a = new String("test") (or new StringBuffer("t est")
b = a
a = a.any_method_th at_modifies_a
out.println(b)
My question: how do I know if a class is Immutable? How to define an
Immutable class?
Thanks,
Zalek
classes. In a book it sayes that a String class is immutable, that
means that one an instance of String class is created, the string it
contais cannot be changed, only reference will change to point to a
different string. So results will be different in the following code
depending is a, b are defined as String or StringBuffer:
a = new String("test") (or new StringBuffer("t est")
b = a
a = a.any_method_th at_modifies_a
out.println(b)
My question: how do I know if a class is Immutable? How to define an
Immutable class?
Thanks,
Zalek
Comment