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
Which class cannot be a subclass in java?
Is heap stored in ram?
What is an interface in java? Explain
Explain differences between collection api and stream api?
What are "methods" and "fields"?
What is difference between throw and throws ?
What is a "pure virtual" member function?
What happens if I remove static from main method?
What does three dots mean in java?
How do you initialize an arraylist in java?
How would overload a function based on return type?
why are wait(), notify() and notifyall() methods defined in the object class? : Java thread
What is singleton class in ruby?
What is the difference between a local variable and an instance variable?
Is a class an object?