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

Answers were Sorted based on User's Feedback



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

Answer / surya simhadri

Mutability means which can be changed. StringBuffer is a
mutable bcoz the contents of the SB object we can change,
it can't create new reference location even though we
change the value it already referenced. But in case of
String it reference location is changed every time if u
change the content.

Is This Answer Correct ?    58 Yes 2 No

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

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

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

Answer / rajender

if use
String s="raj";
here string object created in string content pool(scp).it
(scp)doesn't allow duplicate objects.so it is immutable

String s=s+"ramu";
here string object created in heap allows duplicate
objects.so it is immutable
in heap

String s=rajramu;

if use string buffer every time it will create object in
heap.heap allows duplicate object.so it is mutable
StringBuffer sb=new StringBuffer("raj");
StringBuffersb1=sb.append("ramu");

if u hav any questions related java just call me:9952942104

finally it create duplicate value in heap.

Is This Answer Correct ?    17 Yes 10 No

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

Answer / arjunkumar

Mutability means data which we can change.
StringBuffer class is mutable, because it can change its
data where as String class cant.
Example:
If you want to combine two strings we have two methods i.e
append() method and concat() method.
here append() is used to append the 2nd string to 1st string
and it can store in memory. where as concat() method is
combine the 2nd string to 1st string but it is not actuly
stored in memory it just show to us as combind

Is This Answer Correct ?    4 Yes 4 No

Post New Answer

More Core Java Interview Questions

By what default value is an object reference declared as an instance variable?

1 Answers   Wipro,


Can you use this() and super() both in a constructor?

0 Answers  


Is double bigger than float?

0 Answers  


What is meant by collection in java?

0 Answers  


Is java same as core java?

0 Answers  






Does java list allow null?

0 Answers  


Any one can explain how the inerface uses in java. give with example.

1 Answers   IBM,


Why stringbuffer is faster than string?

0 Answers  


A abstract class extending an abstract class.Super class has both abstract and non-abstract methods.How can we implement abstract and non-abstract mehtods? Explain with snippet

3 Answers  


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 is the need of "creating and throwing an UserdefinedException" when the "Exception" class is already available?

4 Answers  


What is the use of anonymous inner classes ?

12 Answers   DELL, HCL,


Categories