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 is the difference between static (class) method and instance method?

572


What is java life cycle?

539


What are instance variables?

612


What are access specifiers available in java?

579


What are the elements of java?

565






What is r in java?

595


Can we convert integer to string in java?

598


What is a function easy definition?

529


Why are generics used?

522


Can we increase array size dynamically in java?

513


write a program that list all permutations of ABCDEF in which A appears before B?

2013


Can you sort a string in java?

494


What class of exceptions are generated by the java run-time system?

637


How can you generate random numbers in java?

589


describe synchronization in respect to multithreading? : Java thread

505