What are they ?
The java.lang.StringBuilder and java.lang.StringBuffer classes are final classes which are used to make a lot of modifications to Strings of characters. They are not immutable classes like String class, you can modify again and again without leaving behind a lot of new unused objects. Both of methods are doing same job but there are few differences between them.
Differences between StringBuilder and StringBuffer
When to use String, StringBuilder and StringBuffer ?
Think about String class(Go to String class & String Immutability post). I have said that Strings are immutable. So there is a disadvantage. It creates a heavy garbage. Because it uses more memory. But StringBuilder & StringBuffer classes are not immutable. It use a memory in efficient way. Try this program(In this program I have used append() method. You will learn later on this post. Just think it is like concat() method),
public class StringTest { public static void main(String args[]){ String s1 = "Nokia"; s1.concat(" lumia"); System.out.println(s1); s1= s1.concat("Lumia"); System.out.println(s1); StringBuffer s2 = new StringBuffer("Nokia"); s2.append("Lumia"); System.out.println(s2); } }
Now you can see outputs. Look at the following pictures here. First three pictures for String object creation in String class. In this way it creates two String objects. At last one object is useless without reference and it wastes memory.
Following pictures are for object creation in StringBuffer class. In this way, it creates one object and then it is updated. No memory wasting is here.
Important methods in StringBuffer class
append()
This method is used to update the value of a object that invoked the method. You saw it on above post also. This is doing same job that concat() method does. This method can be used not only Strings but also int, char, long, float...etc.
public class StringTest { public static void main(String args[]){ StringBuffer s1 = new StringBuffer("java"); System.out.println(s1.append("blog")); StringBuffer s2 = new StringBuffer("My age: "); System.out.println(s2.append(23)); } }
reverse()
This method is used to reverse a String. try this example and see how it works.
public class StringTest { public static void main(String args[]){ StringBuffer s1 = new StringBuffer("This is an apple"); System.out.println(s1.reverse()); } }
toString()
This method is used to return a value of a StringBuffer object that invoked the method call as a String.
public class StringTest { public static void main(String args[]){ StringBuffer s1 = new StringBuffer("This is an apple"); System.out.println(s1.toString()); } }
Important methods in StringBuilder class
delete()
This method is consist of two parameters(begin and end). It is used to remove any characters from original method as to the index you provide.
public class StringTest { public static void main(String args[]){ StringBuilder s1 = new StringBuilder("ABCDEF"); System.out.println(s1.delete(2,4)); } }
insert()
This method is used to insert a String into the provided StringBuilder object. There are also two parameters, first one for index(int type) and second one(String type) for characters you want to insert.
public class StringTest { public static void main(String args[]){ StringBuilder s1 = new StringBuilder("ABCDEF"); System.out.println(s1.insert(3," XXX ")); } }
String buffer & String builder classes
Reviewed by Ravi Yasas
on
8:41 AM
Rating:
No comments: