Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners 929-StringBuilder-v StringBuilder v/s StringBuffer

Have Programming Question? Ask it here!
 
 
 


Pooja
StringBuilder v/s StringBuffer
1 Answer(s)      6 years and 5 months ago
Posted in : Java Beginners


Hi All, I want to know the difference between StringBuilder & StringBuffer classes. I know that StringBuffer... to be created, which can be quite inefficient. StringBuilder (or StringBuffer


Advertisement
View Answers

April 11, 2008 at 7:07 PM


Hi friend,

Strings are immutable (can't be changed), so manipulating strings often causes many temporary string objects to be created, which can be quite inefficient. StringBuilder (or StringBuffer) are better choices in these cases. Their toString() method can be used get a String value. It isn't necessary to create them with a specific size, it will expand as necessary. Many StringBuilder methods return the same StringBuilder so that the call chaining style of programming can be used. StringBuilder is basically identical to the older StringBuffer, but is slightly faster because it isn't synchronized.


StringBuilder

import javax.swing.*;

public class ReverseExample {
public static void main(String[] args) {
String input; // Used for the input string.
String reversed; // Reversed form or the input string.

while(true){
input = JOptionPane.showInputDialog(null, "Enter a string");
if(input == null)
break;
reversed = "";
for(int i=0; i<input.length(); i++) {
reversed = input.substring(i, i+1) + reversed;
}
JOptionPane.showMessageDialog(null, "Reversed:\n" + reversed);
}
}
}


StringBuffer classes

import java.io.*;

public class stringBufferExam{
public static void main(String[] args) throws Exception{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String str;
try{
System.out.print("Enter your name: ");
str = in.readLine();
System.out.println("This is the example of SringBuffer class and it's functions.");

//Create a object of StringBuffer class
StringBuffer buff = new StringBuffer();
buff.append(str);
System.out.println(buff);
buff.delete(0,str.length());

//append()
buff.append("Hello");
buff.append("World"); //print HelloWorld
System.out.println(buff);

//insert()
buff.insert(5,"_Java "); //print Hello_Java World
System.out.println(buff);

//reverse()
buff.reverse();
System.out.print("Reversed string : ");
System.out.println(buff); //print dlroW avaJ_olleH
buff.reverse();
System.out.println(buff); //print Hello_Java World

}
catch(StringIndexOutOfBoundsException e){
System.out.println(e.getMessage());
}
}
}
------------------------------------

read for more information,

http://www.roseindia.net/java/beginners/StringBuffer.shtml




Related Tutorials/Questions & Answers:
StringBuilder v/s StringBuffer - Java Beginners
StringBuilder v/s StringBuffer  Hi All, I want to know the difference between StringBuilder & StringBuffer classes. I know that StringBuffer... inefficient. StringBuilder (or StringBuffer) are better choices in these cases
StringBuilder v/s StringBuffer - Java Beginners
StringBuilder v/s StringBuffer  Hi, Thank you for your prompt... Threads run simultaneously. But in case of BOTH StringBuilder & StringBuffer, the threads run in synchronized manner. AFAIK StringBuilder is NOT synchronized  
Advertisements
StringBuffer and StringBuilder
StringBuffer and StringBuilder  package org.day.strings; import...[] args) { StringBuffer sb1 = new StringBuffer(); sb1.append("Daya"); StringBuilder sbu2 = new StringBuilder(); sbu2.append("Daya
Differences between the String, StringBuilder, and StringBuffer classes
Differences between the String, StringBuilder, and StringBuffer classes StringBuffer versus String Java provides the StringBuffer and String classes...'s look at the bytecode generated for the example using StringBuffer
Servlet v/s Applets - Servlet Interview Questions
Servlet v/s Applets  what is difference between servlets & applets
spring controller V/S stuts Action - Spring
spring controller V/S stuts Action  we are going to use spring framework so what is better spring controller or struts action
StringBuffer
StringBuffer  What's the difference between these two formats? String string=args[0]; String reverse = new StringBuffer(string).reverse().toString(); String string=args[0]; StringBuffer rev = string.reverse(); String
StringBuffer related.
StringBuffer related.  how to make StringBuffer as immutable
StringBuffer - Java Beginners
StringBuffer  Hi, Thank you so much for answering my previous question regarding StringBuilder And StringBuffer. But,while using more than one...-threaded applications(i.e. StringBuffer)? Please give example(code) and explain
stringbuffer - Java Beginners
DeleteString { public static void main(String args[]) { StringBuffer sb = new StringBuffer("Rose India Tech"); //Removes the characters at index 10 to 15 in the StringBuffer. sb.delete(10, 15); System.out.println("After delete
StringBuffer - Java Beginners
StringBuffer  Can any one help with this naughty problem? Replacing a character with two characters in a StringBuffer. Replacing a character...;} converting the StringBuffer using charArray[] also worked well, but not where
String and StringBuffer - Java Beginners
; Hi vijay Java provides the StringBuffer and String classes.... Simply stated, objects of type String are read only and immutable. The StringBuffer... performance difference between these two classes is that StringBuffer is faster
String and stringbuffer object - Java Beginners
String and stringbuffer object  Hi, What is the basic difference between string and stringbuffer object? Thanks   Hi Friend... be changed while StringBuffer class is used to represent characters that can
'String' vs 'StringBuffer' - Java Beginners
'String' vs 'StringBuffer'  What should i use String or StringBuffer?  Hi, First you need to rectify the question. Because StringBuffers... StringBuffer when you are append different data to the same StringBuffer object
Java - StringBuffer class in Java
Java - StringBuffer class in Java       In this example you will learn about StringBuffer class. This example explains how you can use functions provided by the StringBuffer
Write a method which will remove any given character from a string?
. In this tutorial we have used StringBuilder is a mutable sequence of character. It is used as a replacement of StringBuffer. StringBuilder is not synchronized and uses... StringBuilder is used: The majority of the modification methods on this class
String reverse in java
StringBuffer and StringBuilder methods. For that we will do it by recursive way as well...; java API provides StringBuffer and StringBuilder  reverse() method which... String Buffer "); String reverses = new StringBuffer(word1).reverse
Creation of StringBuffer
Constructor with 16 characters set StringBuffer(String s) //String to String Buffer StringBuffer(int initialCapacity) //StringBuffer?s with initial... Creation of StringBuffer      
Java string buffer
Java string buffer  what is stringBuffer and StringBuilder
String Buffer insert and append example
with StringBuffer and append data to it. The StringBuffer is a class that implements...: StringBuffer(int len): This is the constructor of StringBuffer class... of StringBuffer. It takes following parameters:     pos
Appending String in java
. StringBuilder are in replacement for StringBuffer where synchronization is applied  that is used by single thread. StringBuilder principal operation is insert.... Methods summary for StringBuilder append() method which take any data type
String Reverse In Java Example
will demonstrate you about how to reverse a String using StringBuilder's... this String using StringBuilder's reverse() method. Then we will create... can use the reverse() method of StringBuilder class (jdk 1.5 and later
How To Append String In Java
append() method of either StringBuffer(prior to JDK 5) class or StringBuilder...; StringBuilder sb = new StringBuilder(); sb.append(str1).append(str2).append(str3
java swings - Java Beginners
substringofqueue[] = new String[50]; StringBuffer displayqmrlist = new StringBuffer(); String substringofqueuetemp = null; String st=""; ArrayList selected1...-destinations-service.xml"); Element root = doc.getDocumentElement(); StringBuilder
j - Java Beginners
+ ""; String substringofqueue[] = new String[50]; StringBuffer displayqmrlist = new StringBuffer(); String substringofqueuetemp = null; String...-service.xml"); Element root = doc.getDocumentElement(); StringBuilder strbuf
java swings - Java Beginners
substringofqueue[] = new String[50]; StringBuffer displayqmrlist = new StringBuffer..."); Element root = doc.getDocumentElement(); StringBuilder strbuf = new StringBuilder(); NodeList list = doc.getElementsByTagName("mbean"); for (int
java swings - Java Beginners
= (char) 34 + ""; String substringofqueue[] = new String[50]; StringBuffer displayqmrlist = new StringBuffer(); String substringofqueuetemp = null; String st... = doc.getDocumentElement(); StringBuilder strbuf = new StringBuilder(); NodeList list
java swings - Java Beginners
substringofqueue[] = new String[50]; StringBuffer displayqmrlist = new StringBuffer... = doc.getDocumentElement(); StringBuilder strbuf = new StringBuilder(); NodeList list... fileName = "c:\\qmanager.txt"; try { StringBuilder sb = new StringBuilder
java swings - Java Beginners
searchText3 = (char) 34 + ""; String substringofqueue[] = new String[50]; StringBuffer displayqmrlist = new StringBuffer(); String substringofqueuetemp = null... = doc.getDocumentElement(); StringBuilder strbuf = new StringBuilder(); NodeList list
String doubts - Java Interview Questions
String doubts  difference between String and StringBuffer
How many objects are created in the code below? Explain.
"; StringBuffer s2 = new StringBuffer(s1); StringBuffer s3= s2; StringBuffer s4 = new StringBuffer(s1
explain the difference between string builder and string buffer classes
= "abc"; StringBuffer s2 = new StringBuffer(s1); StringBuffer s3 = s2; StringBuffer s4 = new StringBuffer(s1); } in this example how many objects are created
java - Java Beginners
= (char)34 +""; //String searchText1 = "IBM"; StringBuilder sb =new StringBuilder(); //String osname = System.getProperty("os.name...(in)); StringBuffer str = new StringBuffer(); String strLine
java swings - Java Beginners
substr = null; String substrloop = null; StringBuffer displayqmrlist = new StringBuffer(); String st = ""; public static String xmlPath... root = doc.getDocumentElement(); StringBuilder strbuf = new StringBuilder
java swings - Java Beginners
+ ""; String searchText1 = "IBM"; StringBuilder sb = new StringBuilder...(in)); StringBuffer str = new StringBuffer(); String strLine; while ((strLine
java swings - Java Beginners
= "IBM"; StringBuilder sb = new StringBuilder(); try { Runtime rt...( new InputStreamReader(in)); StringBuffer str = new StringBuffer
can't read my xml file in java
: private String getResults(TopDocs docs) { StringBuilder htmlFormat = new StringBuilder(); htmlFormat.append("<html><body>...=getElementValue(node); StringBuffer buffer = new StringBuffer(); for (int i = 0
can't read my xml file in java
: private String getResults(TopDocs docs) { StringBuilder htmlFormat = new StringBuilder(); htmlFormat.append("<html><body>...=getElementValue(node); StringBuffer buffer = new StringBuffer(); for (int i = 0
can't read my xml file in java
: private String getResults(TopDocs docs) { StringBuilder htmlFormat = new StringBuilder(); htmlFormat.append("<html><body>...=getElementValue(node); StringBuffer buffer = new StringBuffer(); for (int i = 0
can't read my xml file in java
: private String getResults(TopDocs docs) { StringBuilder htmlFormat = new StringBuilder(); htmlFormat.append("<html><body>...=getElementValue(node); StringBuffer buffer = new StringBuffer(); for (int i = 0
can't read my xml file in java
: private String getResults(TopDocs docs) { StringBuilder htmlFormat = new StringBuilder(); htmlFormat.append("<html><body>...=getElementValue(node); StringBuffer buffer = new StringBuffer(); for (int i = 0
strings
]; } StringBuilder builder=new StringBuilder(reversedSt); builder.insert(0...[] args) { String st="Todays Date is ="; StringBuilder builder=new StringBuilder(st); String str="7/3/2011"; builder.insert(16, str
strings
]; } StringBuilder builder=new StringBuilder(reversedSt); builder.insert(0...[] args) { String st="Todays Date is ="; StringBuilder builder=new StringBuilder(st); String str="7/3/2011"; builder.insert(16, str
Java string buffer
Java string buffer  What is the basic difference between string and stringbuffer object
question
question  can we write calss A extend StringBuffer?   No you cannot extend
string reverse order
){ String s= " kalins naik" ; StringBuffer buffer = new StringBuffer(s...string reverse order  Hi I have string s= " kalins naik" then how can i print string s=" naik kalins" Thanks kalins naik   import
In Java I have append to an existing string efficiently
for this? Thanks   Hi, You should use the StringBuilder class for efficiently appending the strings. The StringBuilder class works faster in most
java exception handling - Java Interview Questions
java exception handling   hi to all , this afsar i have one doubt.please clarify it. while handling exception write try catch and finally blocks. the scenario is P v S main(---) throws exception { try
Java - Development process
Java  whats the difference string and stringbuffer?  Hi... stringBuffer{ public static void main(String[] args) throws Exception... and it's function."; //Create a object of StringBuffer class StringBuffer
Advertisements
 

 

 

DMCA.com