Difference between String & StringBuffer

Answers were Sorted based on User's Feedback



Difference between String & StringBuffer..

Answer / niranjanravi

String objects are constants and immutable where as
StringBuffer objects are not.StringBuffer Class supports
growable and modifiable string where as String class
supports constant strings. Strings once created we cant
modify them. Any such attempt will lead to the creation of
new strings.Where as StingBuffer objects after creation
also can be able to delete oo append any characteres to
it.String values are resolved at run time where as
StringBuffer valuesc are resolved at compile time.So if you
know the exact size of the string to be appended it is
better to go for String objects.

Is This Answer Correct ?    194 Yes 19 No

Difference between String & StringBuffer..

Answer / ramanareddy333

strings are immutable where as string buffer is a
mutable,that means strings having the fixed size and string
buffer size is varied(growable).

Is This Answer Correct ?    119 Yes 16 No

Difference between String & StringBuffer..

Answer / navneet raushan

The great difference between String and string buffeer is
that String buffer is thread safe where as String is not
thread safe as well as string is immutable and String
buffewr is mutable.

Is This Answer Correct ?    77 Yes 12 No

Difference between String & StringBuffer..

Answer / vijayakumar chinnasamy

String :
1.Content does not change –immutable

2.final class (cant subclass)

3.compareTo() - compare the string
result : < , > , = --- dictionary order

4.reverse() not available

5.is not thread safe

Criteria to choose among String
If your text is not going to change use a string Class
because a String object is immutable.



StringBuffer:

1. Content can be change Mutable

2.Final class.

3. compareTo() Not available


4.reverse() available

Allocates room for 16-addition character space when no
specific length is specified.

5.is thread safe

Criteria to choose StringBuffer

If your text can changes, and will be accessed from
multiple threads, use a StringBuffer because StringBuffer is
synchronous

Is This Answer Correct ?    52 Yes 9 No

Difference between String & StringBuffer..

Answer / nilesh bhil( mca) amravati

String is immutable.
It means that we cant change the content of String once created. If we try append a new string using + operator
then it creates new String object.
ex.
string s = new String("Java");
s = s + "bean" ;// this statement creates new object.
String class does not have method that append new String to old String in object.


where as StringBuffer class is mutable. It means that we can add new String to it using append() method.
ex.
StringBuffer sb = new StringBuffer("Java");
sb.append("Bean");

Is This Answer Correct ?    38 Yes 3 No

Difference between String & StringBuffer..

Answer / ravikiran(aptech mumbai)

String is immutable where as StringBuffer is mutable.Means
until the newly created string object is reffered by a
reference variable it's value is uncahangable

Is This Answer Correct ?    34 Yes 7 No

Difference between String & StringBuffer..

Answer / bubun

string is slower in case of concatenation but stringBuffer
is faster than string.

Is This Answer Correct ?    23 Yes 2 No

Difference between String & StringBuffer..

Answer / devang bhagdev

in string class that is synchronized is not allows and i stringbuffer class synchronized is allows us

Is This Answer Correct ?    17 Yes 3 No

Difference between String & StringBuffer..

Answer / kunal chwala

It means that we cant change the content of String once created. If we try append a new string using + operator
then it creates new String object.
ex.

Is This Answer Correct ?    13 Yes 5 No

Difference between String & StringBuffer..

Answer / tamanna reshmi

The main difference of string class and StringBuffer is
that....while STRING class creates strings
fixed_length,STRINGBUFFER creates strings of flexible length
that can be modified in terms of both length and
content.........

Is This Answer Correct ?    3 Yes 0 No

Post New Answer

More Core Java Interview Questions

What is a java predicate?

0 Answers  


Name few java.lang classes introduced with java 8 ?

0 Answers  


Difference between nested and inner classes ?

0 Answers  


What is the difference between a choice and a list?

0 Answers  


Where are the card layouts used?

0 Answers  






what is mean by String and StringBuffer? What is mean by Methooverriding and Overloading?

3 Answers   Satyam, STI,


The following program reads data (details of students) from a file named students.txt and converts it into e-mail addresses. The results are written to a file named studentemail.txt. students.txt consists of a number of lines, each containing the data of a student in colon delimited format: Last Name:First Name:Student Number Each input record is converted to an e-mail address and written to studentemail.txt in the following format: the first character of the last name + the first character of the first name + the last four digits of the student number + “@myunisa.ac.za” import java.io.*; public class EmailConverter { public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader(new FileReader ("students.txt")); PrintWriter output = new PrintWriter(new FileWriter ("studentemail.txt")); String line = input.readLine(); while (line != null) { // Extract the information for each student String[] items = line.split(":"); // Generate the email address String email = "" + items[0].charAt(0) + items[1].charAt(0) + items[2].substring(4,8) + "@myunisa.ac.za"; email = email.toLowerCase(); // Output output.println(email); line = input.readLine(); } input.close(); output.close(); } } Rewrite the class so that it handles possible errors that may occur. In particular, it should do the following: • It should catch at least three appropriate exceptions that might occur, and display suitable messages. • At this stage, the program will not run correctly if there is an empty line in the input file. Change the program so that if an empty line is encountered, an exception is thrown and the empty line is ignored. This exception should be handled with the display of a suitable error message. • Before the e-mail address is added to the output file, check if the student number has 8 digits. If not, throw an InvalidFormatException (which the program should not handle itself)

0 Answers  


1).Is Object class abstract or not? 2).Is main method(public static void main(String args[])low priority thread or high priority thread?

3 Answers   TCS,


What is the difference between exception and error in java?

0 Answers  


how many types of Inheritance?

0 Answers   Impetus,


what is hashmap& hashtable with example?

1 Answers   CTS,


Does importing a package imports its sub-packages as well in java?

0 Answers  


Categories