answersLogoWhite

0

That depends what you use them for. A StringBuffer (or StringBuilder) has better performance when you need to build a long string out of small parts, because it allows to change the StringBuffer by appending to it, rather than creating new objects for every change.

A String has better performance when modification is not necessary, because it has less overhead, and the JVM can assume it will never change.

User Avatar

Wiki User

16y ago

What else can I help you with?

Related Questions

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


Why you should use StringBuilder class compared to String class?

Both StringBuffer and StringBuilder are much faster when compared to the String class when it comes to manipulating string objects. But, the StringBuilder is usually faster than the StringBuffer because the StringBuffer object is internally synchronized so, due to the multi-threading overhead the StringBuilder is faster. So, if your application is multi-threaded and the object could be accessed/modified by multiple threads use the StringBuffer, else use the StringBuilder.


How do you encrypt a string in java by replacing the characters with the next alphabet?

A "for" loop, looping through each character, should work just fine. Just get one character at a time, add one to it, and add it to the end of the new string. Using a String object for the new string should work, but it is inefficient for large strings; in this case, a StringBuffer provides better performance. By the way, it should be noted that this is not a very secure encryption.


Compare and construct the string class with string buffer class?

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.


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 difference between String and String Buffer?

In Java, String is a class that represents an immutable string, that is a sequence of characters that can never change. Any modification to the String will have to create a new String object. A StringBuffer (and in Java 1.5 and up, a StringBuilder) is a mutable string object that can be modified at runtime. The advantages of using Strings are that the String is generally lighter and faster to use where the String isn't going to change. When building a long String of text by making changes to one object over time, you should use a StringBuilder or StringBuffer (in Java 1.4 or when synchronization is important).


What is a stringbuilder and stringbuffer?

Strings are extremely useful but at the same time resource intensive too. In programs where numerous strings are used which need to be altered every now and then, it is advisable to use the StringBuffer or the StringBuilder class.The java.lang.StringBuffer and java.lang.StringBuilder classes should be used when you have to make a lot of modifications to strings of characters. As we discussed in the previous chapter, String objects are immutable, so if you choose to do a lot of manipulations with String objects, you will end up with a lot of abandoned String objects in the String pool. On the other hand, objects of type StringBuffer and StringBuilder can be modified over and over again without leaving behind a great list of discarded String objects.StringBuffer vs. StringBuilderThe StringBuilder class was added in Java 5. It has exactly the same API as the StringBuffer class, except StringBuilder is not thread safe. In other words, its methods are not synchronized. (For now just know that syncrhonized is used for thread safety and causes an overhead in terms of performance) Sun recommends that you use StringBuilder instead of StringBuffer whenever possible because StringBuilder will run faster. So apart from synchronization, anything we say about StringBuilder's methods holds true for StringBuffer's methods, and vice versa.


Write a program in java show how different string Class construcotrs are used to create string objects?

public class Test { public static void main(String[] args){ String str = new String("Rocky"); String str1 = "Rocky"; StringBuffer sb = new StringBuffer(); sb.append("Rocky"); String str2 = new String(sb); } } Above are three commonly used ways of creating Strings. Apart from you you can also pass byte arrays and character arrays as arguments to the String constructor


What is the use of string buffer class?

Strings are extremely useful but at the same time resource intensive too. In programs where numerous strings are used which need to be altered every now and then, it is advisable to use the StringBuffer or the StringBuilder class. The java.lang.StringBuffer and java.lang.StringBuilder classes should be used when you have to make a lot of modifications to strings of characters. As we discussed in the previous chapter, String objects are immutable, so if you choose to do a lot of manipulations with String objects, you will end up with a lot of abandoned String objects in the String pool. On the other hand, objects of type StringBuffer and StringBuilder can be modified over and over again without leaving behind a great list of discarded String objects.


All types of String Functions in java?

Type the text String in your favorite IDE, and press F1 for help. You should get a list of all the methods available for the String class. Also check the StringBuffer class, which has some additional methods - for example, the capability of reversing a string. If you can't get the help this way, search the online Java documentation for String and StringBuffer. Here is the documentation of the Stringclass: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html


What is the use of StringBuilder in java?

The StringBuilder class was added in Java 5. It has exactly the same API as the StringBuffer class, except StringBuilder is not thread safe. In other words, its methods are not synchronized. Sun recommends that you use StringBuilder instead of StringBuffer whenever possible because StringBuilder will run faster. So apart from synchronization, anything we say about StringBuilder's methods holds true for StringBuffer's methods, and vice versa.


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.