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
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()); }
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
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)); }
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
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")); }
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
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)); }
mei.tanaka shop.example
substring(start, end) includes start but excludes end; with one argument it goes to the end.
Strings are immutable
void main() { String city = "lisbon"; city.toUpperCase(); System.out.println(city); city = city.toUpperCase(); System.out.println(city); }
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 ==
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")); }
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".
"yes".equals(answer) is safe even if answer is null, whereas answer.equals("yes") would throw a NullPointerException.Text blocks
void main() { String poem = """ Roses are red, violets are blue, Java has text blocks and so can you. """; System.out.print(poem); }
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.
File: summary.pdf Extension: PDF