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 is the specification of ?CODEBASE? in an applet?

1 Answers  


How will you create the class for the following scenario? Employees under one employee?

2 Answers   Bally Technologies,


Explain about assignment statement?

0 Answers  


What primitive Java types? Howmany are they and what are their names?

3 Answers  


I have one POJO class(Java bean class), it has two variables for that it has setters and getters. Now i have created two objects for that class and i have set the data for those variables through this two objects. Now question is i want check whether those two objects have same data or not, for this write a program? Thanks, Bose.

2 Answers   Oracle,


What is Collection interface?

1 Answers   Zensar,


Is it possible to make an array volatile?

4 Answers  


Describe method overriding

0 Answers  


What are disadvantages of java?

0 Answers  


Can you explain the Java thread lifecycle?

1 Answers  


What are the six ways to use this keyword?

0 Answers  


What is the life cycle of Thread ?

12 Answers   HCL, Varnar Softech,


Categories