Curriculum / Java / Lesson 10

Strings in Java

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

10 of 11
beginner 📖 5 min read 🎯 1 question

Strings

Java's String class represents text and comes with a rich set of built-in methods. A key concept: Java strings are immutable — once created, a String's content can never change; operations like .toUpperCase() return a brand new string.

💡 Key insight String immutability is a defining Java concept — every 'modification' actually creates a new String object in memory.

Syntax

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

Example

.length() returns the character count; .toUpperCase() returns a new, uppercase copy of the string.

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());
    }
}
outputName: CodeyHub Length: 8 Upper: CODEYHUB

Try it yourself

Copy this example into the Java compiler and run it — try changing a value and see what happens.

Quick Quiz

1. What does calling .toUpperCase() on a Java String do to the original string?

Back to curriculum