hi to all. well can you please tell me that why String class
is immutable?
Thanks in advance.
Answer Posted / mahi
Yes String cannot be modified because we can not append
another String to existing one.
Example:
1) String string=new String("abc");
2) String s2= string + "hai";
As shown in the above, 2nd line will create one new object
with the value "abchai" so it cannot be modified.
If the same thing if you do with StringBuffer class as
below we can modify
1) StringBuffer str=new StringBuffer("abc");
2) str.append("hai");
Here we can modify existing StringBuffer object - str
| Is This Answer Correct ? | 2 Yes | 1 No |
Post New Answer View All Answers
Why can we not override static method?
Differentiate between stringbuffer and string?
What is a two-pass assembler?
Why is it called a string?
When is the finalize() called?
How to implement an arraylist in java?
How many bits is a string in java?
What do you mean by thread safe?
What 5 doubled?
What is entry in java?
What is the final class modifier?
How can constructor chaining be done using this keyword?
What does main method?
How to do a true java ping from windows?
Does java runtime require a license?