à®®ேலுà®®் பல புதிய டெக்னாலஜி பற்à®±ி தெà®°ிந்து கொள்ள, எங்கள் "டெக் டுப் தமிà®´் நாடு" யூட் டூஃப் சேனலை SUBSCRIBE செய்து கொள்ளவுà®®்.

Breaking

Sunday 8 October 2017

String in JAVA 4


String in JAVA 4

StringBuffer Class

Java StringBuffer class is used to created mutable (modifiable) string object. The StringBuffer class in java is same as String class except it is mutable i.e. it can be changed. 

Note : Java StringBuffer class is thread-safe i.e. multiple threads cannot access it simultaneously. So it is safe and will result in an order. 

StringBuffer Constructors

1. StringBuffer (  )

2. StringBuffer ( int size )

3. StringBuffer ( String str )

4. StringBuffer ( charSequence [ ]ch )


1) StringBuffer() creates an empty string buffer and reserves room for 16 characters.

2) StringBuffer(int size) creates an empty string and takes an integer argument to set capacity of the buffer.

3) StringBuffer(String str) creates a string buffer with the specified string


Example showing difference between String and StringBuffer:

class Test {
 public static void main(String args[])
 {
  String str = "sky";
  str.concat("nils");
  System.out.println(str);      // Output: sky

  StringBuffer strB = new StringBuffer("sky");
  strB.append("nils");
  System.out.println(strB);    // Output: skynils
 }
}

Important methods of StringBuffer class

The following methods are some most commonly used methods of StringBuffer class.

1) append( )

This method will concatenate the string representation of any type of data to the end of the invoking StringBuffer object. append() method has several overloaded forms.

StringBuffer append(String str)
StringBuffer append(int n)
StringBuffer append(Object obj)

The string representation of each parameter is appended to StringBuffer object.

StringBuffer str = new StringBuffer("test");
str.append(123);
System.out.println(str);
Output : test123

2) insert( )

This method inserts one string into another. Here are few forms of insert() method.

StringBuffer insert(int index, String str)
StringBuffer insert(int index, int num)
StringBuffer insert(int index, Object obj)

Here the first parameter gives the index at which position the string will be inserted and string representation of second parameter is inserted into StringBuffer object.

StringBuffer str = new StringBuffer("123");
str.insert(1, "test");
System.out.println(str);
Output : 1test23

3) replace( )

This method replaces the string from specified startIndex to the endIndex.

StringBuffer str = new StringBuffer("Hello World");
str.replace( 6, 11, "java");
System.out.println(str);
Output : Hello java

4) delete( )

The method deletes the string from the specified startIndex to endIndex. 

StringBuffer str=new StringBuffer("Hello"); 
str.delete(1,3); 
System.out.println(str);
Output : Hlo

5) reverse( )

This method reverses the characters within a StringBuffer object. 

StringBuffer str = new StringBuffer("Hello");
str.reverse();
System.out.println(str);
Output : olleH

6) capacity( )

This method returns the current capacity of StringBuffer object. 

StringBuffer str = new StringBuffer();
System.out.println( str.capacity() );
Output : 16

7) ensureCapacity( )

This method is used to ensure minimum capacity of StringBuffer object. 

StringBuffer str = new StringBuffer("hello");
str.ensureCapacity(10);

Difference between String and StringBuffer Class:

String Class StringBuffer Class
String class is immutable.StringBuffer class is mutable.
String is slow and consumes more memory when you concat too many strings because every time it creates new instance. StringBuffer is fast and consumes less memory when you cancat strings.
String class overrides the equals() method of Object class. So you can compare the contents of two strings by equals() method.StringBuffer class doesn't override the equals() method of Object class.

 

No comments:

Post a Comment

Hai , Post your comment . (required, Bugs, Errors )

You need to see


SUBSCRIBE NOW



Backlinks : Website Design Company in Coimbatore
Website Design Company in Tamil nadu
Website Design Company in Karur
Website Design Company in Erode
Website Design Company in ooty
Website Design Company in theni
Website Design Company in thoothukudi
Website Design Company in Chennai
Website Design Company in Sathy
Website Design Company in Coimbatore Tamilnadu
Cheap Website Design Company in Coimbatore
Adsense Website Design Company in Coimbatore
ecommerce Website Design Company in Coimbatore
school Website Design Company in Coimbatore
Academy Website Design Company in Coimbatore
Test Website Design Company in Coimbatore
Best Website Design Company in Coimbatore
Adsense Website Design Company in Coimbatore
Adsense Website Design Company in Tamil nadu
Adsense Website Design Company in Karur
Adsense Website Design Company in Erode
Adsense Website Design Company in ooty
Adsense Website Design Company in theni
Adsense Website Design Company in thoothukudi
Adsense Website Design Company in Chennai
Adsense Website Design Company in Sathy
Free SEO Tools online
Free SEO Tools online Website
Free SEO Tools online Plagiarism
Free SEO Tools online article rewriter tool
Free SEO Tools online article reading and writing criticism
Free SEO Tools online seo backlink
Free SEO Tools online Free backlink maker
Free SEO Tools online seo backlink strategy
Free SEO Tools online content writting
360 help SEO Tools online Plagiarism checker
360 help SEO Tools online seo backlink
360 help SEO Tools online Free backlink maker
360 help SEO Tools online content writting
360 help SEO Tools online article rewriter tool
360 help SEO Tools
360 help SEO Tools India
360 help SEO Tools Tamil
360 help SEO Tools website