Oracle Java Foundations study materials
Oracle Java Foundations study resources

Oracle Java Foundations: Working with Selected Classes from the Java API

Oracle Java Foundations · Work through your exam, one topic at a time.

All guide sections

Working with Selected Classes from the Java API

String

String is immutable - every "modifying" method returns a new object. == compares references, so use equals/equalsIgnoreCase for content, and compareTo for ordering. Key methods: length(), charAt(int), indexOf, substring(begin, end) (end exclusive), toUpperCase, trim/strip, replace, contains, split (returns String[]), and concat. A bad index throws StringIndexOutOfBoundsException; substring(2, 2) is legal and returns "".

StringBuilder

Mutable and efficient for concatenation: append, insert, delete, reverse, length, toString. StringBuilder does not override equals, so compare sb.toString() or content.

Wrappers and parsing

Integer.parseInt("42") returns a primitive; Integer.valueOf("42") returns an Integer. Invalid text throws NumberFormatException. Integer.MAX_VALUE and MIN_VALUE are handy constants; Integer.toString(int) produces text.

Dates and Math

LocalDate, LocalTime, and LocalDateTime are immutable, so date.plusDays(1) returns a new object that must be assigned. Create them with now(), of(...), or parse(...); format with DateTimeFormatter.ofPattern("dd-MM-yyyy"). Math offers max, min, abs, random, and round - Math.round(double) returns a long, and Math.round(float) returns an int.

Collections preview

ArrayList methods: add, get, set, size, remove(int index) versus remove(Object) - the overload choice matters because one returns the removed element and the other a boolean.

Worked example

String s = "Java"; s.toUpperCase(); leaves s as "Java". Only s = s.toUpperCase(); changes it - the classic immutability trap.