Skip to content
Home » Java Programing Language » String and String classes

String and String classes

  1. String is an array of character.
  2. To store string java provided classes like String, StringBuffer, StringBuilder.

String Class

  1. String class is a build-in (Pre-define class) class in java.
  2. String is used to store array of character.
  3. String class has multiple methods to perform operation on exiting String.
  4. String class is a final class.
  5. String class is present inside lang package.
  6. To use String class, have to object.
    1. String object_name = new String(“value”);
    2. String object_name = “value”’
  7. The String object which is created without new operator will be store inside String Constant Pool (SCP).
  8. String is an immutable object, Once we assign the values to String objects its value never change.
  9. SCP is one of the areas inside Heap memory.
  10. 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.

String SOP

String In Memory

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

  1. To Store String value can use StringBuilder Class.
  2. This class is also present inside lang package.
  3. Object of StringBuilder is mutable.
  4. StringBuilder has methods to perform operation on exiting string.
  5. To use StringBuilder you have to create object of this class.
  6. Default Capacity of the StringBuilder is 16. This capacity change once it full.
  7. In StringBuilder methods are non-synchronized.
  8. Can be access by multiple thread at a atime.
  9. 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

  1. To Store String value can use StringBuffer Class.
  2. This class is also present inside lang package.
  3. Object of StringBuffer is mutable.
  4. StringBuffer has methods to perform operation on exiting string.
  5. To use StringBuffer you have to create object of this class.
  6. Default Capacity of the StringBuffer is 16. This capacity change once it full.
  7. All methods if StringBuffer are synchronized.
  8. At a time only one thread can access the object of StringBuffer.
  9. StringBuffer is slower than StringBuilder

 

Join Telegram : Click Here

 

All Full Stack Java Study Material

 

Java the programing language

 

Job’s For Fresher

 

Share This Information To Your Friends and Your College Group’s, To Help Them !