Skip to toolbar

Basic Java – 22 || Let’s learn more about String.

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.

  1. 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″;
  2. 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

Memory allocation for String Objects
Memory allocation for 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:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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:


Leave a Reply

Your email address will not be published. Required fields are marked *

Site Statistics

  • Users online: 0 
  • Visitors today : 4
  • Page views today : 4
  • Total visitors : 49,318
  • Total page view: 67,889

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.