Home Answers Viewqa Java-Beginners String and StringBuffer

 
 


vijayababu
String and StringBuffer
2 Answer(s)      5 years ago
Posted in : Java Beginners

View Answers

May 23, 2008 at 4:47 PM


Hi vijay


Java provides the StringBuffer and String classes, and the String class is used to manipulate character strings that cannot be changed. Simply stated, objects of type String are read only and immutable. The StringBuffer class is used to represent characters that can be modified.

The significant performance difference between these two classes is that StringBuffer is faster than String when performing simple concatenations.

StringBuffer is mainly used for the dynamic string concatenation which enhances the performance. A string buffer implements a mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.

----------------------

import java.io.*;
import java.io.IOException;

public class stringBuffer{
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();
str += ", This is the example of SringBuffer class and it's function.";

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

//append()
string.append("Welcome");
string.append(" to"); //print HelloWorld
string.append(" Roseindia");
string.append(".");
System.out.println(string);


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

--------------------------------
Read for more information.

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



October 5, 2012 at 9:34 AM


One more difference between String and StringBuffer in Java is that StringBuffer is not String and can not be used when later is expected.









Related Pages:
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
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... than String when performing simple concatenations. StringBuffer is mainly
StringBuffer and StringBuilder
StringBuffer and StringBuilder  package org.day.strings; import java.io.BufferedReader; public class Test { public static void main(String[] args) { StringBuffer sb1 = new StringBuffer(); sb1.append
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 modified. 2)StringBuffer is faster than String. 3)String Objects
'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
StringBuffer and StringBuilder
a string buffer. Extracting from StringBuffer c = ... compared to String Because a StringBuffer object is mutable (it can be changed... String dupl(String s, int times) { StringBuffer result = new StringBuffer(s
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
question regarding StringBuilder And StringBuffer. But,while using more than one...-threaded applications(i.e. StringBuffer)? Please give example(code) and explain   Hi public class MultiTread implements Runnable{ private String name
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... on the StringBuffer object, it must be converted back into a String
Java - StringBuffer class in Java
; StringBuffer is mainly used for the dynamic string concatenation which... Java - StringBuffer class in Java       In this example you will learn about StringBuffer
Creation of StringBuffer
Constructor with 16 characters set StringBuffer(String s) //String to String... the String buffer and capacity of StringBuffer. Therefore we get the following... Creation of StringBuffer      
StringBuilder v/s StringBuffer - Java Beginners
Character!"); String st = buff.readLine(); StringBuffer sb =new.... String str; int a = 42; StringBuffer buffer= new StringBuffer(40); str...... Threads run simultaneously. But in case of BOTH StringBuilder & StringBuffer
StringBuilder v/s StringBuffer - Java Beginners
between StringBuilder & StringBuffer classes. I know that StringBuffer... often causes many temporary string objects to be created, which can be quite inefficient. StringBuilder (or StringBuffer) are better choices in these cases
StringBuffer related.
StringBuffer related.  how to make StringBuffer as immutable
String Buffer insert and append example
String Buffer insert and append example   ... with StringBuffer and append data to it. The StringBuffer is a class that implements multiple sequence of characters. The following program construct a string buffer
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
(ii) String and String Buffer Class
between String and StringBuffer 1) String object is immutable whereas StringBuffer objects are mutable. 2) String objects are constants...String and String Buffer Class  difference between String and String
String reverse in java
String Buffer "); String reverses = new StringBuffer(word1).reverse...String reverse in java In this section we are going to discuss about how to reverse a sting in java. There are many ways to reverse a string in java 
Java string buffer
Java string buffer  what is stringBuffer and StringBuilder
STRING FUNCTIONS
String removeCharAt(String s, int pos) { StringBuffer buf = new StringBuffer...); String str1=removeCharAt(st,index1); String str=new StringBuffer(str1...STRING FUNCTIONS  HERE IS THE QUESTION THAT I HAVE IN MY INFORMATICS
String doubts - Java Interview Questions
String doubts  difference between String and StringBuffer
reverse string
=input.nextLine(); String reverse = new StringBuffer(str).reverse().toString...reverse string  how to reverse a string without changing its place...{ public static void main(String[]args){ Scanner input=new Scanner
Java string buffer
Java string buffer  What is the basic difference between string and stringbuffer object
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
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
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
Getting the string in reverse
(); StringBuffer buffer=new StringBuffer(); String str[]=st.split... String: "); String st=input.nextLine(); StringBuffer buffer=new StringBuffer(); String str[]=st.split(" "); for(int i=str.length-1
string - Java Beginners
(); StringBuffer sb= new StringBuffer(words); String reversedWords=sb.reverse... java.util.*; class CountPalindromes{ public static void main(String[] args){ int... sentence: "); String str=input.nextLine(); StringTokenizer stk=new
Summary - String
and interfaces for String class. StringBuilder and StringBuffer are much more...Java: Summary - String String Concatenation Following table shows how to perform the string concatination operations. "abc" + "def
String in Java - Java Beginners
in advance   Hello Antony Insted Using String Array Use StringBuffer...String in Java  hiiiii i initialise a String array,str[] with a size...; Hi friend, This is simple code of String Array. class StringArray
Java String operations
StringBuffer(); String str; while((str=br.readLine())!=null...Java String operations  hi, please suggest me how to write the java...*; class ReadFileIgnoringSomeLines { public static void main(String[] args
java fundamental question related to string
java fundamental question related to string  public class Myclass { Public static void main(String args[]) { String s=â??helloâ??; StringBuffer sb=new StringBuffer(s); Sb.reverse(); If(s==sb) system.out.println(â??aâ
string - Java Beginners
string  1)how to reverse a string without using methods in stringbuffer? 2) how to retrieve username when u entered a mail id?  Hi Friend...{ public static void main(String[]args){ Scanner input=new Scanner(System.in
How ro convert char into string??
main(String[] args) { StringBuffer buffer=new StringBuffer...How ro convert char into string??  Suppose i have one array like... char [] ch={'H','E','L','L','o'}; now i want to convert this arrar into string
String Reverse in Java
the input string by using the StringBuffer(String string) method, reverse...;StringBuffer(string). reverse().toString();   System.out.println... String Reverse in Java      
Example - Array to String
Java: Example - Array to String Here is a simple, but slow, program to concatenate all of the strings in an array, each separated by a specifed string...() // Convert an array of strings to one string. // Put the 'separator' string
Count Palindromes from the string
(stk.hasMoreTokens()) { String words = stk.nextToken(); StringBuffer sb = new StringBuffer(words); String reversedWords = sb.reverse().toString...Count Palindromes from the string In this section, we are going to find out
Read file into String
into the StringBuffer. At last, we have extracted the string from the StringBuffer...Read file into String In this section, you will learn how to read a file into the string.  Description of code: You can use any input stream
String intern()
String intern()      ... the intern() method of String class. We are going to use intern() method of String...; operator to determine which Strings are equal. Basically an intern string is the one
STRING.....
STRING.....  plzz sent me d code for counting vowels in a string... gui programme
string
string   difference detween "public static void main (String[] args) " and "public static void main (String args[])" in java but it executes both... "String args[]" can mean a "string array called args which is an array
string
string  String helloString = new String(helloArray); System.out.println(helloString); i am unable to understand this. could u plz explain
String
String  how to add spaces with string functions.?   Hi... String addSpaceToRight(String s, int n) { return String.format("%1$-" + n + "s", s); } public static String addSpaceToLeft(String s, int n) { return
string
string  a java program using string function to input any string... ArrangeStringAlphabetically { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.print("Enter string
String
characters in string?   import java.util.*; class RemoveDuplicateCharatcersFromString { public static String removeDuplicates(String s... < s.length(); i++) { String st = s.substring(i, i + 1
String
String  write down the code of remove any character from a given string without using any string function   please give me the code of remove any given character from a given string without using function
string
string  a java program to input a string and display the string...*; import java.io.*; public class FirstLetter{ public static String capitalizeFirstLetter( String str ) { final StringTokenizer st = new StringTokenizer( str
string
ExtractWords { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.print("Enter String: "); String st=input.nextLine(); String str[]=st.split(" "); for(int i=0
string
string   just i want to a program in a short form to the given string in buffered reader for example input string: Suresh Chandra Gupta output: S. C...; public class StringTest { public static void main(String [] args
string
string  write a program to accept the string and store the reverse stream into another array and print

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.