Java String Introduction
In this page:
String variable = "text";
String variable = new String("text");
Creating Strings
A String in Java is an object representing an immutable sequence of characters, and you can create one either with a literal ("hello") or explicitly with new String("hello"), though the literal form is almost always preferred.
Both approaches produce a fully usable String object with the same methods available.
Example: Creating Strings
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare a and set it to "hello"
String a = "hello";
// Create a new String object and store it in b
String b = new String("hello");
// Print a line to the console
System.out.println(a);
// Print a line to the console
System.out.println(b);
}
}
Login to try C/C++/Java/PHP code in the editor
Understanding the String Pool
To save memory, Java stores string literals in a special region called the string pool, so two literals with identical text actually point to the exact same object in memory rather than two separate copies.
You can see this with == comparing true for two identical literals, which is precisely why == is the wrong tool for comparing string content.
Example: Understanding the String Pool
public class Main {
public static void main(String[] args) {
String a = "hello";
String b = "hello";
System.out.println(a == b); // true: same pooled object
}
}
Login to try C/C++/Java/PHP code in the editor
String Immutability
Strings are immutable: once created, a String object's characters can never be changed in place.
Every method that appears to "modify" a string — toUpperCase(), concat(), replace() — actually returns a brand-new String object, leaving the original untouched.
Example: String Immutability
public class Main {
public static void main(String[] args) {
String original = "hello";
String upper = original.toUpperCase();
System.out.println(original); // unchanged
System.out.println(upper); // new string
}
}
Login to try C/C++/Java/PHP code in the editor
Checking String Length
The length() method returns the total character count of a String, including spaces, punctuation, and digits, not just letters. Since it's a method (not a field like an array's length), remember the parentheses: str.length().
Example: Checking String Length
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare text and set it to "Hello World"
String text = "Hello World";
// Print a line to the console
System.out.println(text.length());
}
}
Login to try C/C++/Java/PHP code in the editor
Accessing Single Characters
charAt(index) retrieves a single character at a given zero-based position, so charAt(0) gets the first character. Calling it with an index equal to or beyond the string's length throws a StringIndexOutOfBoundsException.
Example: Accessing Single Characters
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare text and set it to "Hello"
String text = "Hello";
// Print a line to the console
System.out.println(text.charAt(0));
}
}
Login to try C/C++/Java/PHP code in the editor
- Comparing strings with
==instead ofequals(). - Expecting
s.toUpperCase()to changes, when strings are immutable and it returns a new string. - Accessing
s.charAt(s.length()), which throwsStringIndexOutOfBoundsException.
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: