What is an Immutable Class :confused: will any one explain in details with example :)
Printable View
What is an Immutable Class :confused: will any one explain in details with example :)
Usually an immutable object/class is one which has all its fields declared as final, though more loosely, the term is sometimes used to mean any object that doesn't provide methods to change its state once instantiated (more strictly, you could use the term "unmodifiable" for this).
The distinction is important in concurrent programming because if a field is declared final, then the JVM guarantees that it is safe to access that field from other threads without synchronization (as of Java 5).
Immutable means not subject to change i.e static.
eg:String objects are constants and immutable whereas String Buffer/String Builder(JDK 5.0) not , they are growable and modifiable objects.
try this u will get the answer:
String s = "abc"
s.concat("123")
s.o.p(s)
StringBuffer sb = new StringBuffer()
sb.append("abc123")
s.o.p(sb.toString())
1->abc
2->abc123
nw to correct first use
s = s.concat("123") rather than s.concat("123") , it does not store anything.
Rocky... a small comment... since your posting in the "New to Java" sub-forum, it's not a good idea to abriviate code like "s.o.p" (which I assume means "System.out.println") since it could confuse the new posters to this forum.
Thanks,
CJSL