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 the two environment variables that must be set in order to run any java programs?

0 Answers  


how to deploy apache tomcat server to weblogic server in java

0 Answers   IBM,


What is the char data type?

0 Answers  


Can we have this () and super () together?

0 Answers  


How do I enable java in safari?

0 Answers  






What is the size of a string in java?

0 Answers  


What is collections framework?

0 Answers  


Can you give names of Container classes?

0 Answers   Ordain Solutions,


Can we have two main methods in a java class?

0 Answers  


What are the differences between string and stringbuffer?

0 Answers  


which method is used to know the status of the Thread?

9 Answers   Honeywell,


Is there any way to skip finally block of exception even if some exception occurs in the exception block?

0 Answers  


Categories