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



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


samar
Java string buffer
1 Answer(s)      4 years ago
Posted in : Java Interview Questions


what is stringBuffer and StringBuilder?

Advertisement
View Answers

October 30, 2010 at 1:00 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

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 HelloJava 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

thanks,



Related Tutorials/Questions & Answers:
Java string buffer
Java string buffer  what is stringBuffer and StringBuilder
Java string buffer
Java string buffer  What is the basic difference between string and stringbuffer object
Advertisements
(ii) String and String Buffer Class
(ii) String and String Buffer Class  difference between String and String Buffer Class
(ii) String and String Buffer Class
String and String Buffer Class  differences between : String and String Buffer Class
(ii) String and String Buffer Class
String and String Buffer Class  difference between String and String Buffer Class... whereas String buffer objects are not constants but growable
Java code for buffer operation...!!!
Java code for buffer operation...!!!  Implement a buffer operation... BufferOperation { public static void main(String[] args) { StringBuffer buffer=new StringBuffer(); Scanner input=new Scanner(System.in
How to create a duplicate buffer of a long buffer in java.
How to create a duplicate buffer of a long buffer in java. In this tutorial, we will see how to create a duplicate buffer that shares the content of long... C:\>java DuplicateBuffer Content in original long buffer
explain the difference between string builder and string buffer classes
explain the difference between string builder and string buffer classes  Hi, Here is my code public static void main(String[] args) { String s1 = "abc"; StringBuffer s2 = new StringBuffer(s1); StringBuffer s3 = s2
How to clear a float buffer in java.
How to clear a float buffer in java. In this tutorial, we will  see how to clear a float buffer in java. ByteBuffer API: The java.nio.FloatBuffer class... C:\>java FloatBufferClear Before clear method data in buffer
How to rewind float buffer in java.
How to rewind float buffer in java. In this tutorial, we will see how to rewind float buffer in java. In this example, The rewind method reset...:\r>java RewindFloatBuffer Data in float buffer. 4.8 3.9 5.6 After
How to clear int buffer in java.
How to clear int buffer in java. In this tutorial, we will discuss how to clear int buffer in java.          ... C:\>java IntBufferClear Int value in the buffer. 22 23 24
How to allocate a short buffer in java.
How to allocate a short buffer in java.  In this tutorial, we will see how to allocate a new short buffer. ShortBuffer API: The java.nio.ShortBuffer...;} } Output C:\>java AllocateShort Limit of short buffer
How to allocate a float buffer in java.
How to allocate a float buffer in java.  In this tutorial, we will see how to allocate a new float buffer. FloatBuffer API: The java.nio.FloatBuffer... buffer of given capacity.  int limit() The limit
How to allocate a new int buffer in java.
How to allocate a new int buffer in java.  In this tutorial, we will see how to allocate a new int buffer. IntBuffer API: The java.nio.IntBuffer... allocate( int capacity)  The allocate() method allocate a int buffer
How to allocate a new long buffer in java.
How to allocate a new long buffer in java.  In this tutorial, we will see how to allocate a new long buffer. LongBuffer API: The java.nio.LongBuffer... buffer of given capacity.  Buffer API: The java.nio.Buffer
How to rewind the int buffer in java.
How to rewind the int buffer in java. In this tutorial, we will discuss how to rewind the int buffer in java.  IntBuffer API... buffer of specified capacity.  abstract IntBuffer put
ShortBuffer in java, How to rewind a short buffer in java.
ShortBuffer in java, How to rewind a short buffer in java. In this tutorial, we will see how to rewind a short buffer in java. ShortBuffer API... allocate a new short buffer.  ShortBuffer put(short [] array
How to create a read-only long buffer in java.
How to create a read-only long buffer in java. In this tutorial, we will see how to create a read-only long buffer that shares the content of another long... C:\>Java ReadOnlyBuffer Content in long buffer. 345678 8765433
How to check long buffer is direct or not in java.
How to check long buffer is direct or not in java. In this tutorial, we will discuss how to check long buffer is direct or not in java. LongBuffer API...() method create a long buffer of specified capacity. 
How to clean a buffer using clear method in java.
How to clean a buffer using clear method in java.      ...: C:\>java ByteBufferClear Before clear method data in buffer... in Buffer class.The ByteBuffer class is available in java.nio package
Java string
Java string  Strings are immutable.But String s="Hello"; String s1=s+"World"; S.O.P(s1); means printing "Hello World". How
ShortBuffer in java, Compare a short buffer with another short buffer.
C:\>java CompareToShortBuffer This short buffer is greater than...Compare a short buffer with another short buffer.  In this tutorial, we will see how to compare a short buffer with another short buffer
please clarify regarding capacity method of my below string buffer class example
please clarify regarding capacity method of my below string buffer class example  hi here is the program below: > import java.lang.*; public class > Compare{ public static void > main(String[] args) { >
Java MappedByteBuffer example, How to create mapped byte buffer in java.
Java MappedByteBuffer example, how to create mapped byte buffer in java. In this tutorial, you will see how to create mapped byte buffer in java. Code...;public static void main(String[] args) throws 
String Mirror in Java
String Mirror in Java  how to get a mirror image of(string) a given string in java   Hello Friend, Try this: import java.util.*; class StringMirror{ public static void main(String []args){ String str="hello
Java String
Java String  You are given two strings S1 and S2,Delete from S2 all those characters which occur in s1 also and create a clean S2 with the relevant characters deleted..write a pgm for this..pls send as soon as possible Thank you
How to read all value from int buffer in java.
How to read all value from int buffer in java. In this tutorial, we will discuss how to read all value from int buffer in java.  IntBuffer API... create a int buffer of specified capacity.  int
String and StringBuffer - Java Beginners
; Hi vijay Java provides the StringBuffer and String classes... buffer implements a mutable sequence of characters. A string buffer is like...String and StringBuffer  Dear Deepak Sir, What is String
ShortBuffer in java, Use of hasRemaining() method in short buffer.
ShortBuffer in java, Use of hasRemaining() method in short buffer. In this tutorial, we will see the use of hasRemaining() method in short buffer. ShortBuffer...:\>java ShortRemain Content is available in buffer. Content is not available
ShortBuffer in java, Transfer the content of a short buffer into another.
ShortBuffer in java, Transfer the content of a short buffer into another.  In this tutorial, we will see how to transfer the content of a short buffer...;java BufferToBuffer Transfer the content from one short buffer into another
ShortBuffer in java, Use of hashCode() in short buffer.
ShortBuffer in java, Use of hashCode() in short buffer. In this tutorial, we will see how to use of hashCode() in short buffer. ShortBuffer API...(..)method allocate a new short buffer. abstract ShortBuffer put
 

 

 

DMCA.com