In Java, a String is text: an object of java.lang.String. It stores a sequence of characters—names, messages, file paths, JSON, and almost every piece of text a program prints or reads.
You create strings with double-quoted literals ("Hello"), join them with +, and print them with System.out.println. Strings are immutable: once created, their characters do not change in place.
== compares object references. Two strings with the same characters can still be different objects—always prefer equals() for content.
Immutable returns.
concat(), toUpperCase(), and similar methods return a new String. Assign the result if you need the updated text.
Syntax
Java Syntax
Common ways to create and inspect strings:
Java
String literal = "Hello, Java!"; // double-quoted literal
String built = first + " " + last; // + concatenation
int len = text.length(); // number of char units
char ch = text.charAt(0); // character at index
boolean same = text.equals("Hi"); // compare content
Reference
Core Ideas
Ideas every Java string tutorial builds on:
Idea
Example
Remember
Literal
"Hello"
Most common way to create a String
Concatenation
a + " " + b
Creates a new String
Length
text.length()
Method (with parentheses)
Character
text.charAt(0)
Zero-based index
Content equality
a.equals(b)
Not == for text
Immutability
text.toUpperCase()
Returns a new String
Code
Java Examples
Five short programs that define how strings work in a Java tutorial.
Example 1 — Create and print
Assign text and display it with System.out.println.
Java
public class StringsIntro {
public static void main(String[] args) {
String language = "Java";
int version = 21;
System.out.println("Language: " + language);
System.out.println("Version: " + version);
System.out.println("Ready to learn strings!");
}
}
Output
Language: Java
Version: 21
Ready to learn strings!
How It Works
String language stores a literal.
+ joins a string and a number (the number becomes text).
println prints each line with a newline.
Example 2 — Escapes
Quotes, backslashes, and newlines inside a string.
She said, "Hello!"
C:\Users\Dev\project
Line 1
Line 2
How It Works
\" inserts a double quote.
\\ inserts a backslash.
\n starts a new line.
Example 3 — Join with + and concat()
Build a message from smaller pieces.
Java
public class StringConcat {
public static void main(String[] args) {
String first = "Hello";
String last = "Java";
String greeting = first + ", " + last + "!";
System.out.println(greeting);
String status = "Status: ";
status = status.concat("Active");
System.out.println(status);
}
}
A String is an object of java.lang.String that stores a sequence of characters—names, messages, paths, and more. You usually create one with a double-quoted literal like "Hello".
No. Strings are immutable. Methods like concat() and toUpperCase() return a new String. For many edits in a loop, use StringBuilder.
Use equals() to compare text content. == checks whether two variables point to the same object in memory—identical text can still be different objects.
Use +: String full = first + " " + last;. You can also call concat(). Prefer StringBuilder when joining many pieces in a loop.
Practice the examples on this page, then open the String Methods index for charAt(), equals(), contains(), format(), and every other method guide in this Java tutorial series.