What is Strings in Java || String Class

Mainly, String is a sequence of characters. Basically, in java, objects of String are immutable which means a constant and cannot be changed once created.

There are two ways to create a string in Java

  • String literal
  • String Objects

Strings in Java (Literals):

Mainly, in Java String literal is basically a sequence of characters from the source character set used by Java programmers to populate string objects or to display text to a user. And these characters can be letters, numbers or symbols which are enclosed within two quotation marks.

Example: String str = “CodedecProjects”;

String Objects

Mainly, in Java, a string object is one that lets you work with a series of characters. It basically wraps all Java string primitive data type which a number of helper methods. The main function of Java is it automatically converts between string primitives and string objects.

String buffer class in Java

Mainly, in Java StringBuffer is a peer class of String that provides much of the functionality of strings. Basically, this type of string represents the fixed-length, immutable character sequences while StringBuffer represents only growable and writable character sequences.

And it may have characters and some sub-strings which inserted in the middle or appended to the end and it will automatically grow to make room for such additions and often has more characters which preallocated than are actually needed, to allow room for growth just like that.

 StringBuffer Constructors :

  • StringBuffer( ): It basically reserves room for 16 characters without reallocation.
  • StringBuffer( int size): It mainly accepts an integer argument that explicitly sets the size of the buffer.

String Tokenizer class in Java

Basically, StringTokenizer is a class in Java which used to break a string into tokens.

Example: Hello Code into “Hello”, “Code”

Mainly, the StringTokenizer object internally maintains only the current position within the string to be tokenized. And some operations advance this current position past the characters processed and the token is returned by taking a substring of the string that was used to create the StringTokenizer object.

Difference B/T String builder and string buffer class.

Basically, Java provides only three classes to represent a sequence of characters: String, StringBuffer, and StringBuilder and the string class is an immutable class whereas StringBuffer and StringBuilder classes are mutable.

There are so many differences between StringBuffer and StringBuilder. The StringBuilder class was firstly introduced in JDK 1.5.

Below is a list of differences between StringBuffer and StringBuilder are given below:

StringBuffer: it synchronized i.e. thread-safe and two threads can’t call the methods of StringBuffer simultaneously. And StringBuffer is less efficient than StringBuilder.

Example:

//Java Program to demonstrate the use of StringBuffer class.  


public class BufferTest{  

 public static void main(String[] args){  

StringBuffer buffer=new StringBuffer("hello");  

buffer.append("java");  

System.out.println(buffer);  } }  

StringBuilder: StringBuilder is non-synchronized i.e. not thread-safe and two threads can call the methods of StringBuilder simultaneously.StringBuilder is more efficient than StringBuffer.

Example:

//Java Program to demonstrate the use of StringBuilder class.  

public class BuilderTest{  

    public static void main(String[] args){  

        StringBuilder builder=new StringBuilder("hello");  

       builder.append("java");  

        System.out.println(builder);  

    }  

}