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




Have Programming Question? Ask it here!
 
 
 


samar
Java string buffer
1 Answer(s)      3 years and 11 months 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
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
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 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
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
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
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
Java String operations
Java String operations  hi, please suggest me how to write the java...*; class ReadFileIgnoringSomeLines { public static void main(String[] args..."); BufferedReader br=new BufferedReader(fr); StringBuffer buffer=new
ShortBuffer in java, How to reset the mark of short buffer.
ShortBuffer in java, How to reset the mark of short buffer. In this tutorial, we will see how to reset the mark of short buffer. ShortBuffer API...;} } Output C:\>java ResetMark Previous mark position of buffer : 3
ShortBuffer in java, Define the order of byte in short buffer.
ShortBuffer in java, Define the order of byte in short buffer. In this tutorial, we will see how to define the order of byte in short buffer. ShortBuffer API...(....) method wrapping an existing short array into short buffer
ShortBuffer in java, Use of remaining() method in short buffer.
ShortBuffer in java, Use of remaining() method in short buffer. In this tutorial, we will see how to use of remaining() method in short buffer. ShortBuffer... C:\>java ElementRemain Current position of buffer : 0 Current
Java String
Java String       In this section, you will learn about the java string. Java String is a set... to a string literal in the object stream. The Java String object contains a sequence
Java String - Java Beginners
Java String  How to seperate the string into characters.  there is a method called getChars() in String class,plz use that and separate into characters so plz verify the String api for related questions 
string - Java Beginners
string in java interview questions  What is string in java? Interview questions
String in Java - Java Beginners
]); } } } ---------------------------------------------------- I am sending simple code of String array. If you are beginner in java...String in Java  hiiiii i initialise a String array,str[] with a size... in advance   Hello Antony Insted Using String Array Use StringBuffer
ShortBuffer in java, Create a read-only short buffer that shares the content of short buffer.
Create a read-only short buffer that shares the content of short buffer. In this tutorial, we will see how to create a read-only short buffer that shares the content of old short buffer. ShortBuffer API: The java.nio.ShortBuffer
How to convert into to String in Java?
How to convert into to String in Java?  Hi, How to convert into to String in Java? Thanks
String immutable - Java Beginners
String immutable  Why is String immutable?  See here Why String is immutable in Java
 ShortBuffer in java, Transfer the array's elements into short buffer.
 ShortBuffer in java, Transfer the array's elements into short buffer... into short buffer. ShortBuffer API: The java.nio.ShortBuffer class... allocate(int capacity)  The allocate(..)method allocate a new short buffer
capitalizing string in java
capitalizing string in java  How to capitalizing string in Java?   String s = "some string"; String capitol = Character.toString(s.charAt(0)).toUpperCase(); String newString = capitol + s.substring(1,x
How to Append String In Java?
How to Append String In Java?  How to write program in in Java for appending to a String? How to Append String In Java? What is the best way to do... the StringBuilder class. Check the example at Appending String in java. You can
FTP Server: Get Buffer size
FTP Server: Get Buffer size This tutorial helps you to get the current internal buffer size on FTP server using java. Get Buffer Size : You can find out the current internal buffer size for your data socket by using the FTPClient
converting string to double in java
converting string to double in java  Please post an example to converting string to double in java. Thanks!   Convert String to Double Tutorial
java string vowel program
java string vowel program  java string vowel program   Please visit the following links: Display the word that begins with vowel Java Count Vowels
Java String Datatype
Java String Datatype  Weather String in java is class or Datatype,if it's clss then How can i assign String str="hello";   String is a class found in the package java.lang.*. When we represent string in double quotes
Core Java String
Core Java String  Hi , Here my code Class Test { String s1 = "null"; String s2 = "null"; String s3 = s1+s2; } what is the out put
Advertisements
 

 

 

DMCA.com