← Back to Chapters

Java String Class

? Java String Class

? Quick Overview

In Java, the String class represents a sequence of characters. Strings are immutable, meaning once created, their values cannot be changed. The String class is part of java.lang and is automatically available in every Java program.

? Key Concepts

  • Strings are objects, not primitive data types
  • Strings are immutable
  • Stored in String Constant Pool (SCP)
  • Supports many built-in methods for manipulation

? Syntax / Theory

Strings can be created using string literals or by using the new keyword. String literals are stored in the String Constant Pool for memory efficiency.

? View Code Example
// Creating strings using literal and new keyword
String s1 = "Hello";
String s2 = new String("World");

? Code Example(s)

? View Code Example
// Demonstrating immutability of String
String name = "Java";
name.concat(" Language");
System.out.println(name);

? Live Output / Explanation

Output

The output will be:
Java

Even after calling concat(), the original string remains unchanged because strings are immutable.

? Interactive Playground

Type a value below and click a method to simulate Java String operations.

// Output Console Waiting for input...

? Tips & Best Practices

  • Use string literals instead of new String() for better memory usage
  • Use StringBuilder or StringBuffer for mutable strings
  • Always compare strings using equals(), not ==

? Try It Yourself

  • Create a string and convert it to uppercase
  • Check whether two strings are equal using equals()
  • Find the length of a string using length()