what is the main difference between string and stringbuffer?
can you explain it with program?

Answers were Sorted based on User's Feedback



what is the main difference between string and stringbuffer? can you explain it with program?..

Answer / divya

String class is used to manipulate character strings that
cannot be changed.Simply stated,objects of the String are
read only and immutable.

StringBuffer class is used to represent characters that can
be modified.

Ex:Sting str=new String("core");
str +="Java";

StringBuffer str=new StringBuffer("core");
str.append("Java");

Is This Answer Correct ?    4 Yes 1 No

what is the main difference between string and stringbuffer? can you explain it with program?..

Answer / ashish ranjan

The main difference between String and StringBuffer is that String is Immutable, which means that we cannot modify the object which is created by the String.
As for Example.
String str = "abc";
now,
str = "abc" + "pqr";
the result is abcpqr. The previous value of str is not modified. It exists in the memory. Java Created new memory for str, which refers abcpqr.

now in case of StringBuffer
StringBuffer str = new StringBuffer("abc");
str.append("pqr");
it modifies in the same object.

Is This Answer Correct ?    2 Yes 0 No

Post New Answer

More Core Java Interview Questions

What are advantages of exception handling in java?

0 Answers  


What are the override methods in Object class?

2 Answers   Tech Mahindra, Wipro,


What is Session and cookies?Explain in detail with an example?

4 Answers   Accenture, CTS,


Is Java is 100% pure OOPS? Explain?

27 Answers   ANZ,


What is difference between hashset and hashmap in java?

0 Answers  






How the elements are organized in GridLayout?

2 Answers  


why to use transient variables when static variables can be used for Serialization

4 Answers  


Does chrome use java?

0 Answers  


How do you remove an object from an arraylist in java?

0 Answers  


Is there is any error if you have multiple main methods in the same class?

10 Answers   Infosys, Wipro,


What is the default value of the local variables?

0 Answers  


Explain Basics of OOP Language in java

0 Answers   Atos Origin,


Categories