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
Can you access the private method from outside the class?
What is the use of singleton class?
Objects or references which of them gets garbage collected?
Define reflection.
Is intellij better than eclipse?
Which graphs are functions?
What are the 6 functions?
Is array passed by reference in java?
what is a working thread? : Java thread
how to create multithreaded program? Explain different ways of using thread? When a thread is created and started, what is its initial state? Or extending thread class or implementing runnable interface. Which is better? : Java thread
What is the driver class?
What is the purpose of garbage collection in java? When is it used?
What is the file type?
What is meant by anonymous class?
Is there is any difference between a scrollbar and a scrollpane?