Java String
A string is basically an object that represents a sequence of characters. The Java String class provides a lot of methods for managing operations on strings, such as compare(), split(), length(), replace(), substring(), etc.

The string class implements mainly three interfaces.
01. Serializable Interface: Serialisation is a mechanism for converting an object into a byte stream. Serialisation is done by using an ObjectOutputStream. Deserialization is the reverse of serialisation. It is done by using an ObjectInputStream.
02. Comparable Interface: This interface defines the compareTo() method used to compare the objects. This method returns an integer value. If the length of the two strings is equal, this method returns 0. If the first string length is greater than the second one, then return 1. If the first string length is less than the second one, then return -1. Following are some examples of the compareTo() method.
/**
* The main class.
*
* @author Sachith Ariyathilaka
* @version 1.0.0
* @since 2024/12/15
**/
public class Main{
public static void main(String args[]){
String s1="Sachith";
String s2="Sachith";
String s3="Ariyathilaka";
System.out.println(s1.compareTo(s2)); // output is 0
System.out.println(s1.compareTo(s3)); // output is -1
System.out.println(s3.compareTo(s1)); // output is 1
}
}
03. CharSequence Interface: This interface is used to represent the sequence of characters. We can create strings by using the following three classes.
=> String: The String class is used to create string objects. A string is immutable. It means that once initialised, it cannot be changed. Whenever we change the string, a new instance will be created. We can do it mainly in two ways, as follows.
>> By String literal: Each time we create the string object using the string literal, the JVM checks the String Constant Pool. If only one particular string is not available in the pool, a new string is created. It means this pool does not hold two string objects from a string literal with the same value. Java String literals are created by using double quotes as follows:
String name = "Sachith Ariyathilaka"
>>By new Keyword: If we use the new keyword, the JVM will create the string object in the normal heap memory. As with the string literal, this heap memory does not hold two string objects with the same value created with the new keyword. Java objects can be created by using the new keyword as follows.
String name = new String("Sachith Ariyathilaka");
But we can hold the same value of a string by using the new keyword and the string literal as two independent variables. The following example will explain this concept.
/**
* The main class.
*
* @author Sachith Ariyathilaka
* @version 1.0.0
* @since 2024/12/15
**/
public class Main {
public static void main(String args[]){
String name1 = "Sachith Ariyathilaka"; //creating string by string literal
String name2 = new String("Sachith Ariyathilaka"); //creating string by new keyword
String name4 = new String("Sachith Ariyathilaka"); //creating string by new keyword
String name4 = "Sachith Ariyathilaka" //creating string by string literal
}
}
According to the above example, names 1 and 2 will be created, but names 3 and 4 will not. There are a lot of methods available in Java. Following are some examples of Java string methods.


=> StringBuffer: The StringBuffer class is mutable. It means the object can change after initialization without creating a new instance. StringBuffer was available after JDK 1.0. StringBuffer is synchronized. It means two threads can’t call StringBuffer methods at the same time. So StringBuffer is thread-safe. But StringBuffer is less efficient than StringBuilder. Following is an example of code for StringBuffer.
/**
* The main class.
*
* @author Sachith Ariyathilaka
* @version 1.0.0
* @since 2024/12/15
**/
public class Main{
public static void main(String[] args){
StringBuffer buffer = new StringBuffer("Hello");
buffer.append("Java");
System.out.println(buffer); // output is Hello Java
}
}
=> StringBuilder: The StringBuilder class is also mutable. It means the object can change after initialization without creating a new instance. StringBuilder was available after JDK 1.5. StringBuilder is non-synchronised. It means two threads can call StringBuilder methods at the same time. So StringBuffer is not thread-safe. But StringBuilder is more efficient than StringBuffer. Following is an example of code for StringBuilder.
/**
* The main class.
*
* @author Sachith Ariyathilaka
* @version 1.0.0
* @since 2024/12/15
**/
public class Mian{
public static void main(String[] args){
StringBuilder builder = new StringBuilder("Hello");
builder.append("Java");
System.out.println(builder); // output is Hello Java
}
}