close
The Wayback Machine - https://web.archive.org/web/20261124011039/https://www.geeksforgeeks.org/java/strings-in-java/

Java Strings

Last Updated : 20 Nov, 2026

A String in Java is an object that represents a sequence of characters. The String class belongs to the java.lang package and provides built-in methods for handling text.

  • Strings are immutable, meaning their contents cannot be changed after creation.
  • Strings represent text as a sequence of UTF-16 code units.
  • Java supports string operations such as comparison, concatenation and searching.
Java
public class Geeks{
    public static void main(String[] args){

        // Creating strings
        String name = "Geeks";
        System.out.println(name);
    }
}

Output
Geeks
frame_1000002518
String Representation in Java

Note: The diagram represents the sequence of characters, not the actual JVM memory layout. Since Java 9, the standard String implementation uses Compact Strings, with a byte[] and an encoding indicator.

Ways of Creating Strings

There are two commonly used ways to create strings in Java.

1. Using String Literals

A string literal is created using a sequence of characters enclosed in double quotes.

String s1 = "Geeks";
String s2 = "Geeks";

Java reuses the same String object from the String Pool when identical literals are used.

2. Using the new Keyword

The new keyword creates a separate String object, even if an identical string exists in the String Pool.

String s3 = new String("Geeks");
String s4 = new String("Geeks");

Here, s3 and s4 refer to two distinct String objects outside the pool, although their contents are identical.

java_string
String Literals and String Objects in Java Memory

The String Pool helps reduce memory usage by reusing String objects with identical literal values. To learn more, refer to String Constant Pool in Java.

Java
public class Geeks{
    public static void main(String[] args){
        String s1 = "Geeks";
        String s2 = "Geeks";
        String s3 = new String("Geeks");
        String s4 = new String("Geeks");

        // Compare object references
        System.out.println(s1 == s2);
        System.out.println(s3 == s4);

        // Compare string contents
        System.out.println(s1.equals(s3));
    }
}

Output
true
false
true

Explanation:

  • s1 == s2 returns true because both refer to the same pooled object.
  • s3 == s4 returns false because they refer to separate objects.
  • s1.equals(s3) returns true because both contain the same text.

Immutable Strings

Strings are immutable, meaning their contents cannot be modified after creation. Operations such as concatenation return a String result without changing the original object.

Java
public class Geeks{
    public static void main(String[] args){
        String str = "Hello";

        // Concatenate without reassignment
        str.concat(" World");
        System.out.println(str);

        // Concatenate with reassignment
        str = str.concat(" World");
        System.out.println(str);
    }
}

Output
Hello
Hello World

Explanation:

  • The first concat() call leaves the original reference unchanged.
  • The second concat() call assigns the resulting String reference to str.
  • The original String object remains unchanged in both cases.

Java provides additional classes and interfaces for handling character sequences.

  • CharSequence: Represents a readable sequence of characters.
  • StringBuffer: Provides mutable, synchronized character sequences.
  • StringBuilder: Provides mutable character sequences without synchronization.
Comment