X

Play AlgoQuiz

Rate Us :



Share with Friends :

Menu

StringBuffer :

A StringBuffer is like a String but can be modified. At any point of time, it contain some particular sequence of characters but the length and content of the sequence can be changed through certain method calls. It is thread-safe and mutable sequence of characters.


StringBuffer() :-

It is a constructor which constructs a String buffer with no character in it and an initial capacity of 16 characters.

StringBuffer(String s) :-

It is a constructor which constructs a String buffer initialized to the content of the specified String.

Example 1 :

class StringExample { public static void main(String[ ] args) { StringBuffer s1 = new StringBuffer(); StringBuffer s2 = new StringBuffer("Algo"); System.out.println(s1); System.out.println(s2); } }

Output :

Algo

Example 2 :

class StringExample { public static void main(String[ ] args) { StringBuffer s = new StringBuffer("Algo"); s.append("Java"); System.out.println(s); } }

Output :

AlgoJava Note :

Once we have created a StringBuffer object then we can perform any changes in the existing object. This behaviour is nothing but mutability of StringBuffer object.


Methods of StringBuffer :


append() :-

It is a method which is overloaded in order to append various types of values.

  • append(int)




  • append(float)




  • append(double)




  • append(char)




  • append(String)




  • append(byte)




  • append(long)




  • append(char[ ])




insert() :-

It is a method which provides facility to insert value at given position. It is overloaded in order to insert various types of values.

  • insert(int offset , int)




  • insert(int offset , float)




  • insert(int offset , double)




  • insert(int offset , char)




  • insert(int offset , boolean)




  • insert(int offset , short)




  • insert(int offset , String)




Note :-

offset -> index number where value has to be inserted.


capacity() :-

It is a method which returns the current capacity (number of characters a String buffer can holds)


delete(int beginIndex , int endIndex) :-

It is a method which delete the substring starting from starting point(beginIndex) to one less than the ending point(endIndex) from a String.

Example :

class StringExample { public static void main(String[ ] args) { StringBuffer s = new StringBuffer(); System.out.println(s.length()); System.out.println(s.capacity()); s.append("Algo"); System.out.println(s); System.out.println(s.length()); System.out.println(s.capacity()); s.insert(4,"Java"); System.out.println(s); System.out.println(s.length()); System.out.println(s.capacity()); s.insert(8,"Programming"); System.out.println(s); System.out.println(s.length()); System.out.println(s.capacity()); s.delete(4,8); System.out.println(s); System.out.println(s.length()); System.out.println(s.capacity()); } }

Output :

0 16 Algo 4 16 AlgoJava 8 16 AlgoJavaProgramming 19 34 AlgoProgramming 15 34

StringBuilder :

StringBuilder is a mutable sequence of characters. This class provides an API(Application Programming Interface) compatibility with StringBuffer but with no guarantee of synchronization. This class is design for use as a drop-in replacement for StringBuffer in places where the String Buffer is used by a single thread.

Example :

class StringExample { public static void main(String[ ] args) { StringBuilder s = new StringBuilder("AlgoJava"); System.out.println(s); System.out.println(s.length()); System.out.println(s.capacity()); s.delete(4,8); System.out.println(s); System.out.println(s.length()); System.out.println(s.capacity()); } }

Output :

AlgoJava 8 24 Algo 4 24

Differences among String , StringBuffer and StringBuilder :


String :-

  • It is immutable.




  • It can be shared.




  • Methods of String are not synchronized.




  • It is not thread safe.




  • It is created in a constant pool of class context area.




  • It is use in single threaded applications.





StringBuffer :-

  • It is mutable.




  • It cannot be shared.




  • Methods of String Buffer are synchronized.




  • It is thread safe.




  • It is created in heap area.




  • It is use in multi-threaded applications.





StringBuilder :-

  • It is mutable.




  • It cannot be shared.




  • Methods of String Buffer are not synchronized.




  • It is not thread safe.




  • It is created in heap area.




  • It is use in single threaded applications.





StringTokenizer :

This class is a member of java.util package.

It allows an application to break a String into tokens.

The set of delimiters (the characters that separate tokens) may be specified either at the time of creation or on a per token basis.

Example :

"Algo Java" Here, the String has space as a delimiter, Algo and Java are tokens. "Algo,Java" Here, the String has (,) as a delimiter, Algo and Java are tokens.


StringTokenizer(String s) :-

It is a constructor which constructs a String tokenizer for the specified String.


StringTokenizer(String s , String delimiter) :-

It is a constructor which constructs a String tokenizer for the specified String by providing a particular delimiter.


String Tokenizer methods :


countTokens() :-

It is a method which calculate the number of times the tokenizer's nextToken() can be called before it generates an exception.

Example :

class StringExample { public static void main(String[ ] args) { String s1 = "Algo Java"; StringTokenizer s2 = new StringTokenizer(s1 , " "); System.out.println(s2.countTokens()); } }

Output :

2

nextToken() :-

It is a method which return the next token from the String tokenizer.

Example :

class StringExample { public static void main(String[ ] args) { String s1 = "Algo Java"; StringTokenizer s2 = new StringTokenizer(s1); String s3 = s2.nextToken(" "); String s4 = s2.nextToken(" "); System.out.println(s3); System.out.println(s4); } }

Output :

Algo Java

hasMoreTokens() :-

It is a method which test if there are more tokens available in the String tokenizer.

Example :

class StringExample { public static void main(String[ ] args) { String s1 = "Algo Java"; StringTokenizer s2 = new StringTokenizer(s1 , " "); while(s2.hasMoreTokens()) { String s3 = s2.nextToken(); System.out.println(s3); } } }

Output :

Algo Java


X

MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.