Predefined methods (course memo + handout)
Java's built-in utility classes/methods for strings, numbers, randomness, and the Object base class.
String methods
length(), toLowerCase(), toUpperCase(), substring(start, end), isEmpty(), equals(String), equalsIgnoreCase(String), trim(), startsWith(String), contains(String), endsWith(String), replaceAll(target, replacement), split(separator).
| Method | Description | Example | Output |
|---|---|---|---|
length() | length of the string | "Sarah".length() | 5 |
isEmpty() | whether the string is empty | "".isEmpty() | true |
equals(s) | equal to s, case-sensitive | "blue".equals("Blue") | false |
equalsIgnoreCase(s) | equal to s, case-insensitive | "blue".equalsIgnoreCase("Blue") | true |
startsWith(s) | starts exactly with s | "hello".startsWith("he") | true |
contains(s) | contains s | "hello".contains("el") | true |
endsWith(s) | ends exactly with s | "hello".endsWith("lo") | true |
toUpperCase() | new copy, all uppercase | "Hello".toUpperCase() | "HELLO" |
toLowerCase() | new copy, all lowercase | "Hello".toLowerCase() | "hello" |
trim() | new copy without leading/trailing spaces | " hi ".trim() | "hi" |
substring(start, end) | new string from start to end - 1 | "Hey there!".substring(1,3) | "ey" |
replaceAll(target, repl) | new copy with all target replaced | "the car is on the street".replaceAll("the","that") | "that car is on that street" |
split(sep) | String[] of pieces cut on sep (wrap in List.of(...) for a List) | List.of("eggs, tomatoes".split(", ")) | ["eggs", "tomatoes"] |
All of these return a new string/value — strings are immutable, none of these mutate the original.
Randomization (java.util.Random)
import java.util.Random;
public class Randomization {
public void example() {
Random random = new Random();
int number = random.nextInt(); // any possible int
int numberBound = random.nextInt(4); // 0 to (4 - 1)
int min = 1, max = 6;
int diceRoll = min + random.nextInt((max - min) + 1); // dice-roll pattern, min..max inclusive
double zeroToOne = random.nextDouble(); // 0.0 to 1.0
boolean trueOrFalse = random.nextBoolean();
}
}Create one Random instance and reuse it for as many calls as needed — no need to recreate it each time.
| Method | Description |
|---|---|
nextInt() | random int across the full int range |
nextInt(limit) | random int in [0, limit - 1] |
nextDouble() | random double in [0.0, 1.0) |
nextBoolean() | random true/false |
Ranged pattern (min to max inclusive): min + random.nextInt((max - min) + 1).
Object methods
Every class in Java — including custom ones — is ultimately an Object, so every class inherits these methods (and can override them):
| Method | Description | Example | Output |
|---|---|---|---|
toString() | string form of the object; default is the class name + memory address unless overridden | Integer.valueOf(5).toString() | "5" (built-ins return sensible values; custom classes need an override, see equals/hashCode/toString) |
equals(other) | equal to other; default compares memory addresses (identity) unless overridden | Integer.valueOf(5).equals(2) | false |
compareTo(other) | 0 if equal, negative if smaller, positive if bigger (numbers compare by value, strings alphabetically, other objects by default via memory address) | Integer.valueOf(5).compareTo(2) | 1 |
Because Java can always ask for an Object and get any class back, this is also the basis for how generic utility code (collections, equals, etc.) can work across arbitrary types.