Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


hi to all. well can you please tell me that why String class
is immutable?

Thanks in advance.

Answers were Sorted based on User's Feedback



hi to all. well can you please tell me that why String class is immutable? Thanks in advance...

Answer / 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

hi to all. well can you please tell me that why String class is immutable? Thanks in advance...

Answer / hari

Well it is immutable because of the memory concern. For
every new string if there is new object then it will take
lot of memory and if there is such requirement then there
is StringBuffer class provided.

Is This Answer Correct ?    2 Yes 0 No

hi to all. well can you please tell me that why String class is immutable? Thanks in advance...

Answer / 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

hi to all. well can you please tell me that why String class is immutable? Thanks in advance...

Answer / rohit kumawat

String class actually kept immutable by developers of "Sun
microsytems" because of you use it in desktop application
not for mobile application where memory is short & less to
work

Is This Answer Correct ?    1 Yes 0 No

hi to all. well can you please tell me that why String class is immutable? Thanks in advance...

Answer / syed

Since, return type of the string class cannot be altered
with substrings or any ammendments to it .

Is This Answer Correct ?    0 Yes 2 No

Post New Answer

More Core Java Interview Questions

Is string is a data type in java?

0 Answers  


What is difference between string and stringbuffer?

0 Answers  


What is the difference between throw and throws in java?

0 Answers  


What does snprintf return?

0 Answers  


Is it safe for adding 10,00,000 records/objects to HashMap?, i.e is it safe to add millions of objects to HashMap?

2 Answers  


What is more advisable to create a thread, by implementing a Runnable interface or by extending Thread class?

5 Answers  


Can a static class have a constructor?

0 Answers  


What is meant by event handling?

2 Answers  


Why do we need to override equals() and hascode() method of object class?

1 Answers   Cognizant,


In Inheritance if we are implementing Multi level inheritance and all class having same name of variable and now i want to access each class variable and how it is possible?

2 Answers  


What do you understand by the term wrapper classes?

0 Answers  


What error occurs if a try-catch-finally statement sequence does not have a catch clause?

4 Answers  


Categories