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
What are the advantages of packages in java?
Can a final variable be manipulated in java?
Explain the difference between extends thread vs implements runnable in java?
What is the escape character in java?
Why does java have two ways to create child threads?
Can we assign the reference to this variable?
When will you define a method as static?
How many types of the indexof method are there for strings?
What is an i/o filter?
Does java set allow duplicates?
Can singleton class be inherited in java?
What is a methodologist?
How objects are stored in java?
What is variable explain with example?
What is the benefit of abstract class?