String is a sequence of characters places in double quotes (“ â€). Performing different operations on strings is called string handling. In java, objects of String are immutable which means a constant and cannot be changed in the same memory after they are created. Hance String is defined as immutable sequence of characters.
In Java we have following three class to store and perform operations on strings.
StringBuilder was introduced as a non thread safe class. All these three classes are subclasses of CharSequence interface.
All these three classes are final classes and subclasses of java.io.Serializable interface
Any data enclosed in “ †is treated by compiler and as an instance of java.lang.String
Example:String s1 = â€abcâ€; (valid) StringBuffer s1 = â€abcâ€; (invalid) StringBuilder s1 = â€abcâ€; (invalid)
There are two ways to create string in Java:
String literal String s = “JavaProgrammingâ€; Using new keyword String s = new String (“JavaProgrammingâ€);
WAP to perform all string operations
import java.util.*; class StringOperation { public static void main(String[] args) { String first="",second=""; Scanner sc=new Scanner(System.in); System.out.println("String Operation"); System.out.println(); System.out.print("Enter the first Sting: "); first=sc.nextLine(); System.out.print("Enter the second Sting: "); second=sc.nextLine(); System.out.println("The strings are: "+first+" , "+second); System.out.println("The length of the first string is :"+first.length()); System.out.println("The length of the second string is :"+second.length()); System.out.println("The concatenation of first and second string is :"+first.concat(" "+second)); System.out.println("The first character of " +first+" is: "+first.charAt(0)); System.out.println("The uppercase of " +first+" is: "+first.toUpperCase()); System.out.println("The lowercase of " +first+" is: "+first.toLowerCase()); System.out.print("Enter the occurance of a character in "+first+" : "); String str=sc.next(); char c=str.charAt(0); System.out.println("The "+c+" occurs at position " + first.indexOf(c)+ " in " + first); System.out.println("The substring of "+first+" starting from index 3 and ending at 6 is: " + first.substring(3,7)); System.out.println("Replacing 'a' with 'o' in "+first+" is: "+first.replace('a','o')); boolean check=first.equals(second); if(!check) System.out.println(first + " and " + second + " are not same."); else System.out.println(first + " and " + second + " are same."); } }
It is a thread safe mutable sequence of characters. It is like a String but can be modified in the same memory location.
Program to uses of constructor and methods
class Demo{ public static void main(String args[]) { StringBuffer sb1 = new StringBuffer(); System.out.println("sb1 length: " + sb1.length()); // prints 0 as there is not data in the buffer System.out.println("sb1 capacity: " + sb1.capacity()); // prints 16, the default capacity StringBuffer sb2 = new StringBuffer(50); System.out.println("sb2 length: " + sb2.length()); // prints 0 as there is not data in the buffer System.out.println("sb2 capacity: " + sb2.capacity()); // prints 50 String str1 = "hello"; StringBuffer sb3 = new StringBuffer(str1); System.out.println("sb3 length: " + sb3.length()); // prints 5, the length of hello System.out.println("sb3 capacity: " + sb3.capacity()); // prints 22; str1.length()+16 CharSequence seq1 = "hello"; StringBuffer sb4 = new StringBuffer(seq1); System.out.println("sb4 length: " + sb4.length()); // prints 5, the length of hello System.out.println("sb4 capacity: " + sb4.capacity()); // prints 22; str1.length()+16 } }
It is same as StringBuffer and the constructors are also same as StringBuffer.
Program to use of StringBuilderimport java.lang.StringBuilder; public class Program { public static void main(String[] args) { // Create a new StringBuilder. StringBuilder builder = new StringBuilder(); // Loop and append values. for (int i = 0; i < 5; i++) { builder.append("abc "); } // Convert to string. String result = builder.toString(); // Print result. System.out.println(result); } }When should we go for String, StringBuffer and StringBuilder?