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.
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.
// Creating strings using literal and new keyword
String s1 = "Hello";
String s2 = new String("World");
// Demonstrating immutability of String
String name = "Java";
name.concat(" Language");
System.out.println(name);
The output will be:
Java
Even after calling concat(), the original string remains unchanged because strings are immutable.
Type a value below and click a method to simulate Java String operations.
new String() for better memory usageStringBuilder or StringBuffer for mutable stringsequals(), not ==equals()length()