Java Strings

Last Updated : 10 Jan 2026

In Java, string is basically an object that represents sequence of char values. An array of characters works as a string in Java. For example:

is same as:

Java String class provides a lot of methods to perform operations on strings such as compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), substring() etc.

The java.lang.String class implements Serializable, Comparable and CharSequence interfaces.

String in Java

CharSequence Interface

The CharSequence interface is used to represent the sequence of characters. String, StringBuffer and StringBuilder classes implement it. It means, we can create strings in Java by using these three classes.

CharSequence in Java

The Java String is immutable which means it cannot be changed. Whenever we change any string, a new instance is created. For mutable strings, you can use StringBuffer and StringBuilder classes.

We will discuss immutable string later. Let's first understand what String in Java is and how to create the String object.

What is String in Java?

Generally, a string is a sequence of characters. However, a string in Java is an object that represents a sequence of characters, and the java.lang.String class is used to create string objects. Java Strings are immutable that means their values cannot be changed once created.

There are two ways to create String object:

  1. By string literal
  2. By new keyword

Here, we are going to discuss these objects one by one.

1. Creating String Using String Literal

Java String literal is created by using double quotes. For Example:

Each time you create a string literal, the JVM checks the "string constant pool" first. If the string already exists in the pool, a reference to the pooled instance is returned. If the string doesn't exist in the pool, a new string instance is created and placed in the pool. For example:


Java String

In the above example, only one object will be created. Firstly, JVM will not find any string object with the value "Welcome" in string constant pool that is why it will create a new object. After that it will find the string with the value "Welcome" in the pool, it will not create a new object but will return the reference to the same instance.

Example

The following example demonstrate how to create string using string literals.

Compile and Run

Output:

Welcome Welcome

Note: String objects are stored in a special memory area known as the "string constant pool".

2. Creating String Using new Keyword

A string can be created using the new keyword, which explicitly creates a new String object in memory rather than using the string literal pool.

For example:

Here,

  • "Welcome" is a string literal, so it is stored in the string pool (one object).
  • new String("Welcome") creates a new String object in the heap (second object).
  • s is the reference variable that points to the object in the heap.

In such case, JVM will create a new string object in normal (non-pool) heap memory, and the literal "Welcome" will be placed in the string constant pool. The variable s will refer to the object in a heap (non-pool).

Compile and Run

Output:

Welcome Welcome

Finding String Length

You can find the number of characters in a string by calling the length() method on the string object.

Syntax

It has the following syntax:

Example

The following example demonstrate how to find string length.

Output:

Length of string: 11

Finding a Character in a String

You can find a character or substring in a string by using the indexOf() method that returns its position.

Syntax

It has the following syntax:

Example

The following example demonstrate how to find a character in a string.

Output:

Index of 'o': 4

Comparing Strings

You can compare two strings using equals() for equality or compareTo() for lexicographical order.

Syntax

It has the following syntax:

Example

The following example demonstrate how to compare strings.

Output:

Equal? false
CompareTo result: -1

Concatenating Strings

You can join two or more strings into one using the concat() method or the + operator.

Syntax

It has the following syntax:

Example

The following example demonstrate how to concatenate strings.

Java String Immutability

In Java, strings are immutable. It means that its value cannot be changed once a String object is created. If any operation appears to modify a String, what happens is the creation of a new String object. The original string remains unchanged. This immutable characteristic of strings in Java has several implications for performance, security, and functionality.

Example

The following example demonstrate the string immutability.

Output:

Original string: Java
After modification, original string: Java
Modified string: Java Programming
After calling toUpperCase on original string: Java
Original string in uppercase: JAVA

Memory Allotment of String

In Java, memory allocation for strings is influenced by immutability and the string pool.

String Literal Storage

When a string is created using a literal, Java first checks the string pool.

  • If the string already exists in the pool, the new reference points to the existing object.
  • If it does not exist, a new string object is created in the pool, and the reference points to it.

This mechanism helps save memory and improves performance, especially when the same strings are used multiple times in a program.

Syntax

It has the following syntax:

new Keyword and String Pool

Strings created with the new operator do not use the Pool by default. They are stored in the heap memory outside the Pool, which means each new operation results in a new object, even if it contains the same string data

Syntax:

It has the following syntax:

Interning

We can manually add a string to the Pool or ensure it uses the Pool by calling the intern() method on a string object. If the Pool already contains an equal string, the string from the Pool is returned. Otherwise, the string is added to the Pool.

Syntax:

It has the following syntax:

Example Demonstrating Memory Allotment of String

Consider the following example, demonstrating the memory allotment of string.

Output:

str1 == str2: true
str3 == str4: false
str1 == str5: true
str1 == str6: true

Construct String from a subset of the char array

You can create a string from a part of a character array using a special String constructor. You need to provide three things: the char array, the starting index, and the number of characters to include.

Syntax

It has the following syntax:

Here,

  • value: The char array.
  • offset: The initial offset into the array.
  • count: The number of characters to use from the array.

Example: Constructing String from Subset of Char Array

The following example demonstrates creating (constructing) a string from the subset of a character array.

Output:

World

Let us see another example of Java String which is created using char[] array.

Example: Creating String Using Char[] Array

The following example demonstrates creating (constructing) a string from a character array (char[] Array).

Output:

java
strings
example

The above code, converts a char array into a String object. And displays the String objects s1, s2, and s3 on console using println() method.

String Class Characteristics and Interfaces

Java String class is a part of the java.lang package which is used to create and manipulate strings. The String class provides many useful methods for operations like comparison, concatenation, and searching.

The String class is one of the most fundamental types in Java that is designed to represent immutable sequences of characters.

Here are the key characteristics and the interfaces it implements:

  • Immutability: Once a String object is created, it cannot be modified. This design ensures thread safety, consistency, and efficiency, especially when used with the string pool.
  • String Pool: Java maintains a pool of string literals to save memory. When a new string literal is created, Java checks the pool for a matching string. If found, the new reference points to the existing string. Otherwise, a new string is added to the pool.
  • Implemented Interfaces: The String class implements several interfaces, including:
    • Serializable: It allows string objects to be converted into byte streams for storage or transmission.
    • Comparable<String>: It enables lexical comparison between strings, supporting natural ordering in collections.
    • CharSequence: It provides a read-only interface for different types of character sequences that allows strings to be accessed and manipulated generically.