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

Which containers may have a MenuBar?

1 Answers  


What is difference between hash mapping and hash table?

5 Answers   NDS, Wipro,


We are seeing so many videos/audios as many web sited. But question is these videos or audios are stored in Databases ( Oracle, Mysql, Sybase,... ) or stored any file directory from there they will give the link for that? Pls explain and give sample code to achieve this one? Thanks, Seenu.

0 Answers   Symphony,


What is an event?

2 Answers  


What is instanceof keyword?

0 Answers  






Can an object be garbage collected while it is still reachable?

3 Answers  


I have a String s = java; What is the output when I say s.replaceAll('j', 'k'); Also what is the value of s after replacing?

8 Answers   KPIT,


What is a subsequence of a string?

0 Answers  


Which class should you use to obtain design information about an object

2 Answers  


Can private class be extended java?

0 Answers  


Can you explain the final method modifier?

0 Answers  


When abstract methods are used?

1 Answers  


Categories