- String is an array of character.
- To store string java provided classes like String, StringBuffer, StringBuilder.
String Class
- String class is a build-in (Pre-define class) class in java.
- String is used to store array of character.
- String class has multiple methods to perform operation on exiting String.
- String class is a final class.
- String class is present inside lang package.
- To use String class, have to object.
- String object_name = new String(“value”);
- String object_name = “value”’
- The String object which is created without new operator will be store inside String Constant Pool (SCP).
- String is an immutable object, Once we assign the values to String objects its value never change.
- SCP is one of the areas inside Heap memory.
- Before storing any value inside SCP, it will check whether same value is present inside memory or not, if not present the create new object else return the similar object.
Example:-
public class StringDemo_1 { public static void main(String args[]) { String studName = new String("Amit"); String empName = "Raj"; System.out.println(studName); System.out.println(empName); System.out.println(studName == empName); String s1 = new String("Hello"); String s2 = "Hello"; String s3 = new String("Hello"); String s4 = "Hello"; System.out.println(s1); System.out.println(s2); System.out.println(s3); System.out.println(s4); System.out.println(s1 == s2); System.out.println(s2 == s4); } } String Methods Example:- public class StringMethods { public static void main(String args[]) { String str1 = new String("Hello Program"); System.out.println("Original Value Of String is "+ str1); System.out.println("The Length Of String is "+ str1.length()); System.out.println("Is the string is empty "+ str1.isEmpty()); System.out.println("In this string char at 8th index is "+ str1.charAt(8)); System.out.println("The index of P is "+ str1.indexOf('P')); System.out.println("Is the string starts with Hello "+ str1.startsWith("Hello")); System.out.println("Is the string ends with Hello "+ str1.endsWith("Hello")); System.out.println("The original value of string after concatinatin is "+ str1.concat(" In java")); String str2 = str1.concat(" In java"); System.out.println("Original Value Of String 1 is "+ str1); System.out.println("Original Value Of String 2 is "+ str2); System.out.println("After substring "+ str2.substring(17)); System.out.println("After substring "+ str2.substring(6,13)); System.out.println("After replace a to A "+ str2.replace('a','A')); System.out.println("After replace all Program to String "+ str2.replaceAll("Program","String")); System.out.println("To Upper case "+ str2.toUpperCase()); System.out.println("To lower case "+ str2.toLowerCase()); String name = "AMIT"; String name2 = "amit"; System.out.println("Original Value Of String is "+ name); System.out.println("Original Value Of String is "+ name2); System.out.println("Are the both strings are equal "+ name.equals(name2)); System.out.println("Are the string equal when we ignore the case " + name.equalsIgnoreCase(name2)); } }
StringBuilder
- To Store String value can use StringBuilder Class.
- This class is also present inside lang package.
- Object of StringBuilder is mutable.
- StringBuilder has methods to perform operation on exiting string.
- To use StringBuilder you have to create object of this class.
- Default Capacity of the StringBuilder is 16. This capacity change once it full.
- In StringBuilder methods are non-synchronized.
- Can be access by multiple thread at a atime.
- Performance is faster than StringBuffer.
Example:-
public class StringBuliderDemo { public static void main(String args[]) { StringBuilder sbr = new StringBuilder("Hello"); System.out.println("Orignal Value is : "+ sbr); sbr.append("Program"); System.out.println("After Append : "+ sbr); sbr.insert(5, "Java"); System.out.println("After Insert : "+ sbr); sbr.replace( 6 , 10, "String"); System.out.println("After Replace : "+ sbr); sbr.delete(0,6); System.out.println("After Delete : "+ sbr); sbr.reverse(); System.out.println("After Reverse : "+ sbr); System.out.println("Lenght of string : "+sbr.length()); System.out.println("Capacity of string : "+sbr.capacity()); } }
StringBuffer
- To Store String value can use StringBuffer Class.
- This class is also present inside lang package.
- Object of StringBuffer is mutable.
- StringBuffer has methods to perform operation on exiting string.
- To use StringBuffer you have to create object of this class.
- Default Capacity of the StringBuffer is 16. This capacity change once it full.
- All methods if StringBuffer are synchronized.
- At a time only one thread can access the object of StringBuffer.
- StringBuffer is slower than StringBuilder
Join Telegram : Click Here
All Full Stack Java Study Material
Job’s For Fresher