Topic 11 of 30 · Intermediate
Functions Reference: Core Utilities, Strings & Math
String, Math, Arrays, Collections and wrapper-class methods you'll use every day.
Concept
Java's standard library already has methods for most everyday tasks. Knowing them saves time in coding rounds and avoids bugs.
String methods return new strings because String is immutable. Arrays and Collections are utility classes with static methods for arrays and lists.
Wrapper classes (Integer, Double, Character) convert between text and numbers and test characters.
Cheat sheet
| Method | What it does | Example → result |
|---|---|---|
| s.length() | Number of characters | "java".length() → 4 |
| s.charAt(i) | Character at index | "java".charAt(0) → 'j' |
| s.substring(a, b) | Part from a to b-1 | "placement".substring(0,5) → "place" |
| s.indexOf(x) / lastIndexOf | Position or -1 | "banana".indexOf('n') → 2 |
| s.contains / startsWith / endsWith | Text tests | "a.pdf".endsWith(".pdf") → true |
| s.equals / equalsIgnoreCase | Compare content | "A".equalsIgnoreCase("a") → true |
| s.compareTo(t) | Dictionary order (<0, 0, >0) | "a".compareTo("b") → -1 |
| s.toUpperCase / toLowerCase | Change case | "sql".toUpperCase() → "SQL" |
| s.trim() / strip() | Remove edge spaces | " hi ".strip() → "hi" |
| s.split(regex) | Split into array | "a,b".split(",") → [a, b] |
| s.replace(a, b) | Replace all | "a-b".replace("-","") → "ab" |
| s.toCharArray() | String → char[] | for sorting/counting letters |
| String.join(sep, items) | Join with separator | String.join("-", list) |
| String.valueOf(x) | Anything → String | String.valueOf(42) → "42" |
| s.isBlank() / repeat(n) | Blank check / repeat | "ab".repeat(2) → "abab" |
| Math.max / min / abs | Max, min, absolute | Math.abs(-3) → 3 |
| Math.pow / sqrt | Power / square root | Math.pow(2,10) → 1024.0 |
| Math.round / ceil / floor | Rounding | Math.ceil(4.1) → 5.0 |
| Math.random() | Random 0.0–1.0 | (int)(Math.random()*6)+1 |
| Integer.parseInt(s) | String → int | Integer.parseInt("42") → 42 |
| Integer.MAX_VALUE / MIN_VALUE | Int limits | 2147483647 |
| Integer.toBinaryString(n) | Binary text | toBinaryString(5) → "101" |
| Character.isDigit / isLetter | Character tests | Character.isDigit('7') → true |
| Arrays.sort(a) | Sort an array | Arrays.sort(nums) |
| Arrays.toString(a) | Printable array | "[1, 2, 3]" |
| Arrays.fill(a, v) | Fill with a value | Arrays.fill(dp, -1) |
| Arrays.binarySearch(a, x) | Search sorted array | index or negative |
| Arrays.asList / List.of | Array → list | List.of(1, 2, 3) |
| Collections.sort(list) | Sort a list | Collections.sort(names) |
| Collections.reverse / shuffle | Reorder a list | Collections.reverse(list) |
| Collections.max / min | Largest / smallest | Collections.max(scores) |
| Collections.frequency(c, x) | Count occurrences | frequency(list, "a") |
| map.getOrDefault(k, d) | Value or default | map.getOrDefault(w, 0) + 1 |
| map.merge(k, 1, Integer::sum) | Count frequencies | word counts |
| map.putIfAbsent / computeIfAbsent | Add only if missing | graph.computeIfAbsent(u, k -> new ArrayList<>()) |
| sb.append(x) | StringBuilder: add to end | new StringBuilder("ab").append('c') → abc |
| sb.insert(i, x) | Insert at index | sb.insert(0, "#") |
| sb.reverse() | Reverse in place | "abc" → "cba" |
| sb.delete(a, b) / deleteCharAt(i) | Remove a range / one char | sb.deleteCharAt(sb.length()-1) |
| Integer.toString(n) / toString(n, radix) | int → String (any base) | Integer.toString(255, 16) → "ff" |
| Integer.bitCount(n) | Number of 1 bits | Integer.bitCount(7) → 3 |
| Character.isLetterOrDigit(c) | Alphanumeric test | isLetterOrDigit('_') → false |
| Character.toLowerCase / toUpperCase | Change one char's case | Character.toLowerCase('A') → 'a' |
Code (Java 21)
import java.util.*;
public class Methods {
public static void main(String[] args) {
String s = " Hello Placement World ".strip();
String[] words = s.toLowerCase().split(" ");
System.out.println(String.join("-", words)); // hello-placement-world
char[] letters = "listen".toCharArray();
Arrays.sort(letters);
System.out.println(new String(letters)); // eilnst
Map<Character, Integer> freq = new HashMap<>();
for (char c : "banana".toCharArray()) freq.merge(c, 1, Integer::sum);
System.out.println(freq); // {a=3, b=1, n=2}
List<Integer> marks = new ArrayList<>(List.of(72, 91, 65));
Collections.sort(marks, Collections.reverseOrder());
System.out.println(marks + " max=" + Collections.max(marks));
System.out.println(Integer.parseInt("42") + Math.abs(-8)); // 50
}
}Remember
- substring(a, b) excludes index b.
- Compare Strings with equals(), never ==.
- Arrays = arrays, Collections = lists.
Interview questions
How do you check if two strings are anagrams?
Convert both to char arrays, sort them with Arrays.sort and compare with Arrays.equals — or count letters in an int[26].
What does split("\\s+") do?
Splits on one or more whitespace characters, so multiple spaces don't create empty items.
Lesson test
Take an 8-question test on “Functions Reference: Core Utilities, Strings & Math” — concepts, output prediction, bug spotting and interview questions.