Java Strings

Level : Intermediate
Mentor: Shailendra Chauhan
Duration : 00:03:00

What are Strings in Java?

A string in Java is a sequence of characters that represent text-based information and are created using the String class from java.lang package. Strings are immutable, which means their contents cannot be changed after the string object is generated.

Ways to Create a String Object in Java

String objects can be generated in two different ways:

  1. Using String Literal
  2. Using the new keyword

String Pool

The Java String Pool is a heap memory collection that stores strings. When a new object is created, it checks to see if it already exists in the pool and returns its reference if it does; otherwise, it creates a new object in the pool and returns its reference.

Types of Strings

There are 2 types of strings in Java:

  1. Immutable String
  2. Mutable String

Interfaces and Classes in Strings in Java

The java.lang.String class provides support for the Serializable, Comparable, and CharSequence interfaces.

CharSequence Interface

The CharSequence interface abstracts character sequences and provides methods for their manipulation and access. Implemented by String, StringBuilder, and StringBuffer, it allows for the interchangeable usage of numerous character sequence types, making code development more versatile.


A string is an immutable class, meaning it cannot be modified. We need to generate a new object and functions like to upper, lower, and so on to return new objects.


StringBuffer is a Java class that represents a changeable string of characters. It indicates that you can modify the contents of the buffer without creating a new object. It offers an alternative to the immutable String class, allowing you to change the contents of a string without having to create a new object each time.


StringBuilder in Java enables mutable character sequences, allowing for faster operations than StringBuffer due to its lack of thread safety. While it is an alternative to the String class, the key difference is its lack of synchronization, which makes it unsuitable for concurrent operations.

String Tokenizer

The StringTokenizer class in Java breaks strings into tokens by preserving a location within the string and returning substrings as tokens.

String Methods

String methods in Java are built-in procedures that allow you to modify strings by extracting substrings, concatenating them, and searching for certain characters or substrings inside a string. 

Java's Immutable String

In Java, string objects are immutable. Immutability simply means that something cannot be changed or modified. Once a string object is formed, its data or state cannot be modified; instead, a new string object is produced.

Memory Allocation of String

The String constant pool stores string objects formed as literals, allowing the JVM to allocate and optimize memory efficiently. String literals are preferred for memory optimization in Java programs.

Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
Accept cookies & close this