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 is the difference between static (class) method and instance method?
What is java life cycle?
What are instance variables?
What are access specifiers available in java?
What are the elements of java?
What is r in java?
Can we convert integer to string in java?
What is a function easy definition?
Why are generics used?
Can we increase array size dynamically in java?
write a program that list all permutations of ABCDEF in which A appears before B?
Can you sort a string in java?
What class of exceptions are generated by the java run-time system?
How can you generate random numbers in java?
describe synchronization in respect to multithreading? : Java thread