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.

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


Please Help Members By Posting Answers For Below Questions

Which class cannot be a subclass in java?

1005


Is heap stored in ram?

944


What is an interface in java? Explain

1164


Explain differences between collection api and stream api?

1150


What are "methods" and "fields"?

999


What is difference between throw and throws ?

1122


What is a "pure virtual" member function?

1078


What happens if I remove static from main method?

998


What does three dots mean in java?

1109


How do you initialize an arraylist in java?

1023


How would overload a function based on return type?

948


why are wait(), notify() and notifyall() methods defined in the object class? : Java thread

970


What is singleton class in ruby?

995


What is the difference between a local variable and an instance variable?

1083


Is a class an object?

987