answersLogoWhite

0

Java string types are immutable, meaning that once they are created they cannot be "grown" to accommodate longer strings. For example, the following code abandons and re-allocates the memory for resultString after every concatenation operation:

resultString = "foo"; // Set resultString

resultString += "bar"; // Abandon resultString, set as "foobar"

resultString += "bash"; // Abandon again, set as "foobarbash"

The point of stringbuilder is to have an object that can be grown as necessary and doesn't require reallocation. It is therefore more efficient then normal concatenation.

User Avatar

Wiki User

15y ago

What else can I help you with?

Related Questions

Write a program to revrse the given integer and print it backwords?

In Java, you might use the StringBuffer class. Convert the integer to a StringBuffer, use the method to revert it - I believe it is revert() or something; look it up in the documentation - then print it.In Java, you might use the StringBuffer class. Convert the integer to a StringBuffer, use the method to revert it - I believe it is revert() or something; look it up in the documentation - then print it.In Java, you might use the StringBuffer class. Convert the integer to a StringBuffer, use the method to revert it - I believe it is revert() or something; look it up in the documentation - then print it.In Java, you might use the StringBuffer class. Convert the integer to a StringBuffer, use the method to revert it - I believe it is revert() or something; look it up in the documentation - then print it.


Can you declare an instance variable of class StringBuffer in Java program?

Here's how to create a StringBuffer instance.StringBuffer sb = new StringBuffer();sb.append("Add this is to the buffer");// ...Note the StringBuffer has synchronized methods so if it is only accessed in a single thread context then a StringBuilder is preferred. StringBuffer and StringBuilder both implement the Appendable and CharSequence interfaces so can be used interchangeably.


Difference between stringbuffer and 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. (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.


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.


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.


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


How an exception subclass is created in java explain?

We can create a exception sub class by extending Exception class available in java


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


Extension file for Java?

Java source files have the .java extension, compiled Java class files have the .class extension.


Java programming to check a digit number is palindrome or not?

import java.io.*; public class chuva { public static void main(String[] args) throws Exception { BufferedReader x = new BufferedReader(new InputStreamReader(System.in)); int rem, quo, rev=0; System.out.println("Enter a number: "); int a = Integer.parseInt(x.readLine()); int b=a; for(int ctr=0; ctr<=a; ctr++) { rem = a%10; a = a/10; rev = rev*10 +rem; ctr=0; } System.out.println(+rev); if(b==rev) System.out.println("Palindrome"); else System.out.println("Not Palindrome"); } }


The actions in a java class are called?

The actions in a java class are called methods.


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