Flat Preloader Icon

String Classes, Buffer And Builder

In Java, the String class, String-Buffer, and String-Builder are classes used to work with text data, but they have different characteristics and are suited for different scenarios.

System

  • java.lang.System is a final class exposing utility functions
  • Some fields & methods:-
    • public static final java.io.PrintStream out;
    • public static final java.io.PrintStream err;
    • public static final java.io.InputStream in;
    • public static long currentTimeMillis();
    • public static Sring getProperty(String propName);
				
					String str = "Hello";
str = str + " World"; 
// Creates a new string object

				
			

String Classes : String

  • java.lang.String Represents a piece of text or set of characters.
  • Immutable, hence thread safe.
  • String pooling
  • Some methods
    • public int length()
    • public String subString(int beginIndex)
    • public String[] split (String regex)

String Classes : StringBuffer

  • java.lang.StringBuffer is used for string manipulation. .
  • It is mutable, unlike java.lang.String.
  • It is thread-safe, hence little expensive to work with.
				
					StringBuffer buffer = new StringBuffer
("Hello");
buffer.append(" World");
// Modifies the existing buffer

				
			
Java counts positions from zero. 0 is the first position in a string, 1 is the second, 2 is the third …

String Classes : StringBuilder

  • java.lang.StringBuilder is used for string manipulation.
  • It is also mutable, like java.lang.StringBuffer.
  • It is not thread-safe, hence useful where we don’t need shared resources.
  • Some methods are :-
    • public int capacity()
    • public StringBuilder append(String args)
    • public StringBuilder insert (int offset, String args)
				
					StringBuilder builder =
new StringBuilder("Hello");
builder.append(" World");
// Modifies the existing builder