Strings

Text in Java is represented by the String class. Strings are everywhere — names, messages, file contents, JSON — so it's worth understanding how they work: how to create and join them, why they never change, and the right way to compare them.

Creating strings

JAVA
void main() {
    String greeting = "Hello";
    String name = "Ana";
    String empty = "";
    System.out.println(greeting + ", " + name + "!");
    System.out.println("Length: " + greeting.length());
    System.out.println("Empty? " + empty.isEmpty());
}
Output
Hello, Ana!
Length: 5
Empty? true

String literals use double quotes. String is a class, so strings are objects with methods such as length() — called with a dot.

Concatenation

JAVA
void main() {
    String first = "Jonas";
    String last = "Weber";
    int age = 34;
    String full = first + " " + last;
    System.out.println(full + " is " + age + " years old");
    System.out.println(first.concat(last));
}
Output
Jonas Weber is 34 years old
JonasWeber

+ joins strings and converts other values to text automatically. Remember the left-to-right rule from lesson 3: "Total: " + 2 + 3 gives "Total: 23".

Characters and indexes

JAVA
void main() {
    String word = "Java";
    System.out.println(word.charAt(0));
    System.out.println(word.charAt(word.length() - 1));
    System.out.println(word.indexOf('v'));
    System.out.println(word.indexOf("z"));
}
Output
J
a
2
-1

Characters are numbered from 0, so the last index is length() - 1. indexOf returns -1 when nothing is found. Asking for an index that doesn't exist throws StringIndexOutOfBoundsException.

Substrings

JAVA
void main() {
    String email = "mei.tanaka@shop.example";
    int at = email.indexOf('@');
    System.out.println(email.substring(0, at));
    System.out.println(email.substring(at + 1));
}
Output
mei.tanaka
shop.example

substring(start, end) includes start but excludes end; with one argument it goes to the end.

Strings are immutable

JAVA
void main() {
    String city = "lisbon";
    city.toUpperCase();
    System.out.println(city);
    city = city.toUpperCase();
    System.out.println(city);
}
Output
lisbon
LISBON

No method changes a String; methods like toUpperCase() return a new string. If you don't store the result, it's lost. Immutability makes strings safe to share and use as map keys.

Comparing strings: equals, not ==

JAVA
void main() {
    String a = "hello";
    String b = new String("hello");
    String c = "HELLO";
    System.out.println(a == b);
    System.out.println(a.equals(b));
    System.out.println(a.equalsIgnoreCase(c));
    System.out.println(a.compareTo("help"));
}
Output
false
true
true
-4

== checks whether two variables refer to the same object; equals compares the characters. Two strings with the same text can be different objects, so always compare strings with equals. compareTo orders strings alphabetically: negative means "comes before".

Null safety: "yes".equals(answer) is safe even if answer is null, whereas answer.equals("yes") would throw a NullPointerException.

Text blocks

JAVA
void main() {
    String poem = """
        Roses are red,
          violets are blue,
        Java has text blocks
        and so can you.
        """;
    System.out.print(poem);
}
Output
Roses are red,
  violets are blue,
Java has text blocks
and so can you.

A text block (Java 15+) starts with """ and a line break. Common indentation is removed automatically, and quotes don't need escaping — ideal for JSON, SQL or HTML inside Java code.

Try it yourself

Extract the file name and extension from a path.

Try it yourself · Main.java
Edit the code, copy it into Main.java and run java Main.java (Java 25+). How to set up Java
Output
File: summary.pdf
Extension: PDF