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
Is set ordered?
What does the three dot emoji mean?
What is comparator in java?
What is busy spin, and why should you use it?
what is abstract class in Java?
Under what conditions is an object’s finalize() method invoked by the garbage collector?
what is the major difference between linkedlist and arraylist in java?
Can each java object keep track of all the threads that want to exclusively access it?
How do you do math powers in java?
What is the role of the java.rmi.naming class?
Give few difference between constructor and method?
Is hashset ordered java?
What is difference between local variable and global variable?
What is struts in java?
There are two classes named classa and classb. Both classes are in the same package. Can a private member of classa can be accessed by an object of classb?