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...

what is mutability?which one is mutable String or StringBuffer?and why?give
examples of each which shows the mutability of each String or StringBuffer

Answer Posted / harinath.b (sai sudhir p.g co

The following programe of mine simply gives ans.

class Sample5{
public static void main(String ask[]){

String s1=new String("Harinath");
System.out.println("s1 : "+s1);
System.out.println("s1 address : "+s1.hashCode());
String s2=new String("Simple");
s1=s1.concat(s2);
System.out.println("s1 : "+s1);
System.out.println("s1 address : "+s1.hashCode());


System.out.println("--------------------------------------------------");

StringBuffer s3=new StringBuffer("Harinath");
System.out.println("s3 : "+s3);
System.out.println("s3 address : "+s3.hashCode());
StringBuffer s4=new StringBuffer("Simple");
s3=s3.append(s4);
System.out.println("s3 : "+s3);
System.out.println("s3 address : "+s3.hashCode());
}
}

output:-
D:\>javac Sample5.java

D:\>java Sample5
s1 : Harinath
s1 address : 185903223
s1 : HarinathSimple
s1 address : -1382461175
--------------------------------------------------
s3 : Harinath
s3 address : 17523401
s3 : HarinathSimple
s3 address : 17523401


means whenever we go for String class ,String allows
to add new String object.But JVM creates new String object
and stores old and new String objects in it,assigns new
address for it.See the above address if you doubt,
s1 : Harinath
s1 address : 185903223
This is s1 address before adding
s1 : HarinathSimple
s1 address : -1382461175
This is the s1 address after adding .
Means new object is created with s1 & s2 , This new
object address is assigned to s1 variable . Now s1 is
reffering to the new object.


Whereas in StringBuffer class this not happens.Because
StringBuffer allows to add new contents in old object
only.It modifies only the old object, it doesn't create new
Object.That is why address is not modified.

Note:-
String allows concat() method.
StringBuffer allows append() method.
If change either of this sequence it will be error.

The above answer is wrong.Because if u use append() on
StringBuffer,concat() on String objects definitly the actual
object is modified.No matter about heap.Our programe is
showing clearly the modifiwd content.

Is This Answer Correct ?    26 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are the different ways of implementing thread? Which one is more advantageous?

938


What is generics in java interview questions?

1000


How does callback work in java?

994


What is map and hashmap in java?

1026


What are the steps in the jdbc connection?

959


What are the core java topics?

990


What is boolean query?

918


Why chararray() is preferred over string to store the password?

958


What is broken and continue statement?

942


Is 0 true or false in java?

950


What is array class in java?

1006


What do you mean by chromounits in java8?

895


What does flagged out mean?

1001


Is ruby built on java?

897


How do you clear a list in java?

1040