Curriculum
Course: Complete Java Programming Course
Login

Curriculum

Complete Java Programming Course

Text lesson

Introduction to String Handling in Java

In this lesson, you will learn

  • Understanding String in Java
  • StringBuffer and StringBuilder Class
  • Examples

 

Strings in Java

In Java, a String is a sequence of characters. Unlike primitive data types, String is a class, specifically java.lang.String. This means that strings are objects, and they have methods that can be used to manipulate them.

Key Characteristics of Java Strings:

  • Immutable: Once a String object is created, its value cannot be changed. Any operation that appears to modify a string actually creates a new String object.
  • Represented by char Array: Internally, a String is represented as an array of char (Unicode characters).
  • Part of java.lang: The String class is part of the java.lang package, so you don’t need to import it explicitly.

 

However, Java also offers classes for mutable strings: StringBuilder and StringBuffer.

Below is an explanation of these classes, including their mutability or immutability.

1. Immutable String: String Class

  1. In Java, a String is a sequence of characters that is treated as an object.
  2. Java strings are immutable, meaning their values cannot be changed once created.
  3. Instead, any modification to a string results in the creation of a new string object.

 

There are mainly two ways to create a String in Java.StringHandling

Creating Strings in Java:

1.1. String Literals

1. This is the most common method to create a string. When you create a string using a string literal, Java first looks in the string pool to check if an identical string already exists.

2. If a match is found, it returns a reference to that same instance; if not, a new string instance is created and placed in the pool.

Example

String s1 = "Hello";
String s2 = "Hello";
/* s1 and s2 refer to the same instance 
in the string constant pool */

 

Note: In the JVM, a special memory area, the string constant pool, is used to store string literals.

The figure below shows how string literals are stored in the String Constant Pool and refer to the same string objects.

String s1 = "Hello";
String s2 = "Hello";

 

If you append the string literal, “World”, to the string reference variable, s1, then a new string object, “HelloWorld”, is created in the memory, and the variable, s1, will refer to the new string object.

String s1="Hello";
s1=s1+"World";

 

However, the string object “Hello” still exists in memory but has no references, as shown in the following figure.

 

Note: Thus, every time you manipulate a string object, a new string object is created in the memory. Therefore, the String object is called an immutable object.

 

Example 1: Understanding and Creating a String

package ch11.l1;

public class StringExample1 {
	public static void main(String[] args) {

		String s1 = "Hello"; //use string literal
		String s2 = "Hello";
		System.out.println("s1 hashcode: "+s1.hashCode());
		System.out.println("s2 hashcode: "+s2.hashCode());
		//Modifying String s1
		s1+="World";
		/* New object is created for "HelloWorld" literal, 
		 * and now s1 is reffering to new object and s2
		 * still referring to "Hello"
		 */
		System.out.println("----------nAfter Modificationn----------");
		System.out.println("s1 hashcode: "+s1.hashCode());
		System.out.println("s2 hashcode: "+s2.hashCode());
	}
}

Output

s1 hashcode: 69609650
s2 hashcode: 69609650
----------
After Modification
----------
s1 hashcode: 439329280
s2 hashcode: 69609650

 

Understanding the ‘hashcode()’ function

  • In Java, a hash code is an integer value uniquely identifying an object. The hashCode() method returns the hash code for an object.
  • The hash code is based on the object’s content and memory address.
  • The hashCode() method is defined in the Object class.

1. If two objects are equal according to the equals(Object) method, then calling the hashCode method on each object must produce the same integer result.

2. It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects can improve hash table performance.

 

2. Creating a string using the ‘new’ Keyword

Strings created with the new keyword are stored in the heap memory, and a new string object is created even if an identical string already exists in the string pool.

Example

String s3 = new String("Hello");
/* s3 is a different instance in the heap, 
not in the string pool */

Note:

Heap memory is an area within the JVM where objects are stored.

The following figure shows the way to create a new string object using the ‘new’ keyword.

String s1 = "Hello";
String s2 = "Hello";
String s3 =  new String("Hello");

 

 

Comparing Both Methods

Using string literals is more memory-efficient due to the string pool mechanism. However, using the new keyword gives you a distinct string object separate from the string pool, which can be useful in certain scenarios.

 

Understanding equals( ) Versus ==

The equals( ) method compares the characters inside a String object. The == operator compares two object references to see whether they refer to the same instance.

 

Example-1: Comparing String Objects: equals( ) Versus ==

package ch11.l1;

public class StringExample2 {

	public static void main(String[] args) {
		String s1 = "Hello"; //use string literal
		String s2 = "Hello"; //use string literal
		String s3=new String("Hello");
		
		System.out.println(s1+" equals "+s2+" : "+s1.equals(s2));
		System.out.println(s1+" == "+s3+" : "+(s1==s3));
		System.out.println("s1 hashcode: "+s1.hashCode());
		System.out.println("s2 hashcode: "+s2.hashCode());
		System.out.println("s3 hashcode: "+s3.hashCode());
	}
}

Output

Hello equals Hello : true
Hello == Hello : false
s1 hashcode: 69609650
s2 hashcode: 69609650
s3 hashcode: 69609650

Explanation: All objects have the same hashcode as they contain the same string literal, even though objects are not identical.

 

2. Creating Mutable Strings in Java

  • Java allows for modification of the characters in the sequence and can dynamically change its size.
  • Java also offers classes for mutable strings using StringBuilder and StringBuffer classes.

 

Understanding the StringBuffer and StringBuilder

StringBuffer and StringBuilder In Java, classes are used to create mutable sequences of characters.

The key difference between StringBuffer and StringBuilder lies in their thread safety and performance.

 

2.1. Understanding StringBuffer

  • Thread-Safe: StringBuffer is thread-safe, which means it is synchronized. Multiple threads can safely modify an StringBuffer object without causing any inconsistency in data.
  • Performance: Due to its thread-safety, StringBuffer operations are slower compared to StringBuilderbecause it requires locking the object for each operation.

 

Example: Using StringBuffer

package ch11.l1;

public class StringBufferExample1 {
	public static void main(String[] args) {
		StringBuffer sb = new StringBuffer("Hello");
		System.out.println("Original String: "+sb);
		System.out.println("sb hashcode: "+sb.hashCode());
		//Modifying String
		sb.append(" World");
		System.out.println("Modified String: "+sb); //Prints "Hello World"
		System.out.println("sb hashcode: "+sb.hashCode());
	}
}

Output

Original String: Hello
sb hashcode: 883049899
Modified String: Hello World
sb hashcode: 883049899

 

2.2. Understanding StringBuilder

  • Not Thread-Safe: StringBuilder is not synchronized, which means it is not thread-safe. This class should be used in scenarios where thread safety is not a concern.
  • Performance: StringBuilder is more efficient than StringBuffer in single-threaded environments due to the lack of synchronization overhead.

 

Example: Using StringBuilder

StringBuilder builder = new StringBuilder("Hello");
builder.append(" World");
System.out.println(builder); // Prints "Hello World"

 

Choosing Between StringBuffer and StringBuilder

  • Use StringBuffer When you need to ensure thread safety in a multi-threaded environment.
  • Use StringBuilder When working in a single-threaded environment, or when thread-safety is not a concern, for better performance.

 

Important Note!

Note: Both StringBuffer and StringBuilder allow modification of strings without creating new objects for each change.

 

In the next lesson, you will learn different methods of String class.

 


End of the lesson….enjoy learning

 

 

Student Ratings and Reviews

 

5.0
5.0 out of 5 stars (based on 1 review)
Excellent
Very good
Average
Poor
Terrible

 

 

6 March 2025

This Is a great course as I am studying from sir in person so it’s much better for me but this course is as same as sir teaches in the class.Great Work.Thank You.

 

 

Submit a Review