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


Please Help Members By Posting Answers For Below Questions

What are the advantages of packages in java?

565


Can a final variable be manipulated in java?

518


Explain the difference between extends thread vs implements runnable in java?

523


What is the escape character in java?

500


Why does java have two ways to create child threads?

461






Can we assign the reference to this variable?

531


When will you define a method as static?

540


How many types of the indexof method are there for strings?

481


What is an i/o filter?

557


Does java set allow duplicates?

516


Can singleton class be inherited in java?

509


What is a methodologist?

524


How objects are stored in java?

539


What is variable explain with example?

562


What is the benefit of abstract class?

507