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


Please Help Members By Posting Answers For Below Questions

Can I import same package/class twice?

497


How do you achieve singleton?

533


What is the largest long allowed by java?

539


What is an infinite loop?

548


Differentiate between a constructor and a method? Can we mark constructors final?

678






How do you input a string in java?

503


Explain about class in java?

612


What do you mean by boolean?

580


Which is better singleton or static class?

509


What is static method with example?

620


What is == in java?

568


Why is core java important?

576


What is the difference between a window and a frame in java programming?

596


What is time complexity algorithm?

548


What is meant by distributed application? Why are we using that in our application?

552