Notes / Java / Strings

Strings in Java — Revision Notes

A quick-reference cheat sheet: definition, syntax, example, common mistakes and an interview tip.

beginner 📖 2 min revision

Definition

📌 Strings Java's String class represents text and comes with a rich set of built-in methods.

Syntax

syntax
String name = "text";
name.length();
name.toUpperCase();
name.equals(other);

Example

java
public class Main {
    public static void main(String[] args) {
        String name = "CodeyHub";
        System.out.println("Name: " + name);
        System.out.println("Length: " + name.length());
        System.out.println("Upper: " + name.toUpperCase());
    }
}

Output

outputName: CodeyHub Length: 8 Upper: CODEYHUB

Important Points

Common Mistakes

Interview Tip

🎯 Before your interview "Why are Java strings immutable?" is a classic question — it enables safe string sharing/caching and makes strings safe to use as hash keys.
Back to notes Read full lesson →