GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — String Class in Java - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

String Class in Java

Last Updated : 29 Sep, 2026

The String class in Java is used to represent and work with sequences of characters. It is part of the java.lang package and provides various methods for creating, comparing, searching, and manipulating strings. String objects are immutable, meaning their values cannot be changed after creation.

  • String literals can be stored and reused through the String Pool.
  • Immutability makes String objects safe to share and use across multiple parts of a program.
  • Strings can be created using string literals or the new keyword.

Key Features of the String Class

1. Immutable

Immutable means that once a String object is created, its value cannot be changed.

Example:

Java
public class Main {
    public static void main(String[] args) {
        String text = "hello";
        text.charAt(0) = 'H';  // compile-time error
    }
}

Explanation: The line text.charAt(0) = 'H'; causes a compile-time error because charAt(0) returns a read-only char, not a variable. String is immutable in Java, you cannot modify its characters directly.

2. Thread-Safe

String objects in Java are considered thread-safe because they are immutable. Once a String object is created, its value cannot be modified, so multiple threads can safely read and share the same String object without changing its contents.

  • Multiple threads can access the same String object safely for read operations.
  • No synchronization is required to protect the String object's value from modification.
  • Operations that appear to modify a String actually create a new String object instead of changing the existing one.

3. Supports Various Utility Methods

String is a predefined final class in Java present in java.lang package. It provides various methods to create, manipulate, and compare strings, like length(), charAt(), concat(), equals(), etc.

Java
import java.io.*;

class GFG {
    public static void main (String[] args) {
        String str = "hello geeks";
        System.out.println("Length of String-> "+str.length()); 
        System.out.println("Changed String ->"+str.toUpperCase());
    }
}

Output
Length of String-> 11
Changed String ->HELLO GEEKS

4. Implements Interfaces

The String class implements several interfaces that provide features such as character-sequence handling, comparison, serialization, and constant description.

  • CharSequence: Provides methods for accessing a sequence of characters, such as charAt() and length().
  • Comparable<String>: Enables lexicographical comparison of strings using compareTo().
  • Serializable: Allows String objects to be serialized.
  • Modern Java versions also support additional interfaces related to constant descriptions.

String Constructors in Java

In Java, String constructors are used to create new String objects from different sources like character arrays, byte arrays, or another string. Although strings in Java are usually created using string literals, the String class also provides constructors for more control.

Let us check these constructors using a example demonstrating the use of them.

Java
public class Geeks {
    public static void main(String[] args) {
        
        // Constructor 1: Creating string using new keyword
        String str1 = new String("Hello Java");
        System.out.println("String using new keyword: " + str1);

        // Constructor 2: Creating string from character array
        char[] charArray = { 'J', 'A', 'V', 'A' };
        String str2 = new String(charArray);
        System.out.println("String from char array: " + str2);

        // Constructor 3: Creating string from byte array
        byte[] byteArray = { 72, 101, 108, 108, 111 };
        String str3 = new String(byteArray);
        System.out.println("String from byte array: " + str3);
    }
}

Output
String using new keyword: Hello Java
String from char array: JAVA
String from byte array: Hello

String Constructors Table

String Constructors

Description

String(byte[] byte_arr)

Construct a new String by decoding the byte array. It uses the platform's default character set for decoding.

String(byte[] byte_arr, Charset char_set)

Construct a new String by decoding the byte array. It uses the char_set for decoding.

String(byte[] byte_arr, int start_index, int length)

Construct a new string from the bytes array depending on the start_index(Starting location) and length(number of characters from starting location).

String(byte[] byte_arr, int start_index, int length, Charset char_set)

Construct a new string from the bytes array depending on the start_index(Starting location) and length(number of characters from starting location).Uses char_set for decoding.

String(char[] char_arr)

Allocates a new String from the given Character array.

String(char[] char_array, int start_index, int count)

Allocates a String from a given character array but choose count characters from the start_index.

String(int[] uni_code_points, int offset, int count)

Allocates a String from a uni_code_array but choose count characters from the start_index.

String(StringBuffer s_buffer)

Allocates a new string from the string in s_buffer.

String(StringBuilder s_builder)

Allocates a new string from the string in s_builder.

Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.