String is a sequence of characters. But technically in Java, String is treated as a class and we have to create an object of that class to generate a string.
String Object
There are two ways to create a string object.
- Direct initialization or via string literal: This is a very easy and most used way of creating string objects. This can be done by using double quotes “. Any character surrounded by double quotes will be treated as a string.
- String temp=”Computer”;
- String temp1=”ABC123″;
- Using the ‘new’ keyword: Like we create an object of the other class we can create an object of the String class.
- String temp=new String(“Computer”);
- String temp1=new String(“ABC123”);
Memory allocation of String Objects

As we know Heap memory is divided into two parts one is String Pool and another is for objects. Let’s understand the above diagram steps by step:
- Whenever we create any string using double quotes, they get stored in the String Pool. A string pool stores the only unique value in it. That’s why s1=”blogs” get stored as the first value in the string pool.
- Whenever we create an object using the new keyword it gets stored in the Heap memory but outside the string pool. It can store duplicate values as it belongs to different objects. So for s2=new String(“blogs”) statement although the value of s1 and s2 are the same, s2 will be stored outside the string pool.
- When we create another string using double quotes, first it checks all the values in the string pool and if it matches any the same location assigned to another reference object. Hence s3=”blogs” will not add a new entry in the string pool.
- But if we create another object with an existing value using a new keyword. It will allocate new memory to the new object in the Heap. Hence s4=new String(“blogs”) would be allocated new memory.
- Now if we manipulate the value in s1 using s1=”New “+s1, it will not update the existing entry or reference of s1. This process will create new entry in the string pool with value “New blogs” and memory reference would changed for s1 object.
String Code Example
public class StringExample {
public static void main(String[] args)
{
// Stores in String pool under Heap memory and create only one copy if value is same.
String s1="Computer";
// Stores in Heap memory outside the String Pool.
String s2=new String("Computer");
// First checks in the String pool and if value is same existing memory allocated to new object.
String s3="Computer";
// Although value is same, but it allocates new memory in the Heap
String s4=new String("Computer");
// "==" operators doesn't compares the values it always compares the address of objects.
System.out.println("Checks the reference address for s1 & s2 :"+(s1==s2)); // Here both the addresses will be different
System.out.println("Checks the reference address for s1 & s3 :"+(s1==s3)); // Both addresses will be same.
System.out.println("Checks the reference address for s2 & s4 :"+(s2==s4)); // Both the addresses will be different
// Comparison on values.
System.out.println("Comparison on values of s1 & s2: "+s1.contentEquals(s2));
}
}
Output:
Checks the reference address for s1 & s2 :false
Checks the reference address for s1 & s3 :true
Checks the reference address for s2 & s4 :false
Comparison on values of s1 & s2: true
Java Basics:
- Basic Java – 1 || Understand Java before start learning JAVA.
- Basic Java – 2 || Variables and Data Types used in JAVA.
- Basic Java – 3 || Understanding Class, Objects, Methods in Java.
- Basic Java – 4 || More on methods(Return Type and Parameters)
- Basic Java – 5 || Methods- Call by Value and Call by Reference in Java.
- Basic Java – 6 || Understanding of Constructor and Destructor in JAVA.
- Basic Java – 7 || Static Variables and Methods.
- Basic Java – 8 || Lets learn about Arrays in Java.
- Basic Java – 9 || Performing multiple operations using Java Operators.
- Basic Java – 10 || Conditions (If and Switch) in JAVA.
- Basic Java – 11 || for and for-each in Java. (Loops Part-1)
- Basic Java – 12 || Alternate looping concepts while and do-while. (Loops Part-2)
- Basic Java – 13 || Decimal values v/s Octal base(8) values in JAVA.
- Basic Java – 14 || Learn about String literals in Java.
- Basic Java – 15 || Runtime User Input using Scanner Class (Part-1).
- Basic Java – 16 || Runtime User Input using BufferedReader Class (Part-2).
- Basic Java – 17 || Runtime User Input using Console Class (Part-3).
- Basic Java – 18 || Difference between break and continue keywords.
- Basic Java – 19 || Sending Email using Java (Part-1).
- Basic Java – 20 || Sending Email with attachment using Java (Part-2).
- Basic Java – 21 || Stack memory and Heap memory in Java.
- Basic Java – 22 || Let’s learn more about String.
- Basic Java – 23 || String, StringBuffer & StringBuilder in Java.
- Basic Java – 24 || Exception Handling using Try Catch.