answersLogoWhite

0

The main difference is that a String is an immutable type, and a StringBuffer is designed to have its contents changed. Let's look at an example:

String str = "hello";

str += " world!";

The first line of code creates a new String object with the data "hello" in it. The second line appears to simply add on some more characters to the string, but what is actually happening is that an entirely new String object is being allocated with "hello world!" as the contents. This is not ideal.

For many applications, this can be good enough, but if you are modifying your strings a lot, consider using a StringBuffer instead.

StringBuffer str = new StringBuffer("hello");

str.append(" world!");

This code ends up with the same result as the String example, but it doesn't need that extra step of completely recreating the String.

User Avatar

Wiki User

16y ago

What else can I help you with?

Related Questions

What is the difference among string and string buffer and string builder?

String is the immutable class that means the object f that class never be changed. String is the Sequence of character.


What is the user of stringbufer class explain how it is different from a string class?

A StringBuffer is similar to a String with a few differences:String objects are immutable while StringBuffer objects can be modifiedIf your code involves string manipulation, string buffer is faster than strings


What is string buffer in java?

StringBuffer is java class available in java.lang package which provides mutable String object where String is immutable class. The methods of this class like reverse(), append(),insert() gives facility to insert data of the same object.


How do you campaire Strings in java?

String class in Java has an 'equals' method that can be used to compare strings.


C plus plus programme to compare strings?

#include<iostream> #include<string> int compare (const std::string& a, const std::string& b) { int n=1; std::string::const_iterator ai=a.begin(), bi=b.begin(); for (int n=1; ai!=a.end() && bi!=b.end(); ++ai, ++bi, ++n) { char ca = *ai; char cb = *bi; if (ca<cb) return n * (-1); if (cb<ca) return n; } if (ai==a.end() && bi==b.end()) return 0; if (ai==a.end()) return n * (-1); return n; } int main() { std::cout << "When comparing strings, zero indicates the two strings are equal.\n" << "A negative value indicates the first string is less than the second string.\n" << "A positive value indicates the first string is greater than the second string.\n" << "The absolute value indicates the characters that differ in the strings. Thus\n" << "-10 or 10 indicate that the 10th characters show a difference.\n" << std::endl; std::string x = "This is a string."; std::string y = "This is another string."; std::string z = "This is another."; std::cout << "String x = "" << x << """ << std::endl; std::cout << "String y = "" << y << """ << std::endl; std::cout << "String z = "" << z << """ << std::endl; std::cout << std::endl; std::cout << "compare(x,x) = " << compare(x,x) << std::endl; std::cout << "compare(x,y) = " << compare(x,y) << std::endl; std::cout << "compare(x,z) = " << compare(x,z) << std::endl; std::cout << "compare(y,x) = " << compare(y,x) << std::endl; std::cout << "compare(y,y) = " << compare(y,y) << std::endl; std::cout << "compare(y,z) = " << compare(y,z) << std::endl; std::cout << "compare(z,x) = " << compare(z,x) << std::endl; std::cout << "compare(z,y) = " << compare(z,y) << std::endl; std::cout << "compare(z,z) = " << compare(z,z) << std::endl; std::cout << std::endl; }


What are some differences between String and StringBuffer in Java?

A String in Java refers to an immutable object that holds alphanumeric values. Everytime you try to modify the value held inside the String, a new object would be created.A StringBuffer refers to an object that is built to hold alphanumeric values for modification. StringBuffers were built for handling strings that need to be modified.Functionality wise both of them are similar.StringBuffer is mutable and faster with string manipulation operations, whereas Strings are immutable and are slower than StringBuffer for string operations.1) String objects are constants and immutable whereas StringBuffer does not2) String class supports constant strings. whereas String Buffer class supports growable and modified string3) Strings once we created we cannot modify them. whereas String Buffer objects after creation also can be able to delete to append any characters to it


What is the C plus plus program for string compare?

You don't need a program to compare strings since std::string already provides support for all the comparison operators (<, <=, >, >=, == and !=). To roll your own you must first create a string class and then provide operator overloads for the comparison operators. To compare strings, start at the first character in each string and compare. So long as they remain equal, move onto the next character. The comparison ends as soon as any character differs. You need only compare these two characters to decide which string is the lesser. To perform a case insensitive comparison, copy the two characters and convert the copies to lower case (or upper case, it doesn't matter). Then compare the copies. Do this for each character as you compare them rather than converting the entire string.


What is string class features in java?

String class is useful to accept inputs from commands prompt as string arguments


Is String are instances of the class string?

yes


How do you construct a class in Java?

class User{// AttributesString firstName;// Constructor (Optional)public User (){}// Methodspublic void setFirstName(String name){firstName = name;}public String getFirstName(){return firstName;}}Using the class from the main method Examplepublic classTest{public static void main(String [] args){// Declare User Object named uUser u;// Set first name in objectu.setFirstName("Harvey");// Print first nameSystem.out.println(u.getFirstName());}}


Define class string and implement all the functions related to strings?

define class string


What is the difference between string and class?

A string is a specific class that is used for dealing with text data