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
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 |
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 |
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 |
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 |
Explain about instanceof operator in java?
What are the types of relation?
What is the symbol for space?
How many inner classes can a class have?
Why is it called buffering?
What is a class ?
What is the difference between Stream Reader and Stream Writer?
What are the three types of design patterns?
Why is java not 100% pure oops?
What are different types of arrays?
What is Distributed Application and what is its usage?
How many ways can we create singleton class?