hi to all. well can you please tell me that why String class
is immutable?
Thanks in advance.
Answer Posted / akhil saxena
Immutable means which cant not be changed. whenever you call
a method though an instance of String class it will give you
the result as new String object. It wont change the existing
String object.
String class objects are made immutable so that if the
String is logically same,then more than one object can refer
the same single String in memory. in this way it also saves
memory.
Example: String a="ABC";
String b="ABC";
in above example JVM will create only one object with the
value "ABC" and both the reference variables a and b will
refer to the same. So it is saving memory.
If you need to change a String object very frequently, it is
not adviced to use a String object as it creates a new
object whenever you change its value. Its a best practice to
use StringBuffer. it is mutable.
For more info write to me at akhilsaxenain2000@yahoo.com
| Is This Answer Correct ? | 12 Yes | 0 No |
Post New Answer View All Answers
Is list ordered in java?
What is unicode datatype?
What is the final keyword denotes?
which class is the wait() method defined in? : Java thread
Can we print null in java?
How do you find the maximum number from an array without comparing and sorting?
Why webdriver is an interface?
Explain about the performance aspects of core java?
What about main thread in java?
What is double in java?
What is a method in java?
Does constructor return any value?
What is the importance of main method in Java?
What is lossy conversion in java?
What is the difference between the ">>" and " >>>" operators in java?