← Java roadmap

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

MethodWhat it doesExample → 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) / lastIndexOfPosition or -1"banana".indexOf('n') → 2
s.contains / startsWith / endsWithText tests"a.pdf".endsWith(".pdf") → true
s.equals / equalsIgnoreCaseCompare content"A".equalsIgnoreCase("a") → true
s.compareTo(t)Dictionary order (<0, 0, >0)"a".compareTo("b") → -1
s.toUpperCase / toLowerCaseChange 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 separatorString.join("-", list)
String.valueOf(x)Anything → StringString.valueOf(42) → "42"
s.isBlank() / repeat(n)Blank check / repeat"ab".repeat(2) → "abab"
Math.max / min / absMax, min, absoluteMath.abs(-3) → 3
Math.pow / sqrtPower / square rootMath.pow(2,10) → 1024.0
Math.round / ceil / floorRoundingMath.ceil(4.1) → 5.0
Math.random()Random 0.0–1.0(int)(Math.random()*6)+1
Integer.parseInt(s)String → intInteger.parseInt("42") → 42
Integer.MAX_VALUE / MIN_VALUEInt limits2147483647
Integer.toBinaryString(n)Binary texttoBinaryString(5) → "101"
Character.isDigit / isLetterCharacter testsCharacter.isDigit('7') → true
Arrays.sort(a)Sort an arrayArrays.sort(nums)
Arrays.toString(a)Printable array"[1, 2, 3]"
Arrays.fill(a, v)Fill with a valueArrays.fill(dp, -1)
Arrays.binarySearch(a, x)Search sorted arrayindex or negative
Arrays.asList / List.ofArray → listList.of(1, 2, 3)
Collections.sort(list)Sort a listCollections.sort(names)
Collections.reverse / shuffleReorder a listCollections.reverse(list)
Collections.max / minLargest / smallestCollections.max(scores)
Collections.frequency(c, x)Count occurrencesfrequency(list, "a")
map.getOrDefault(k, d)Value or defaultmap.getOrDefault(w, 0) + 1
map.merge(k, 1, Integer::sum)Count frequenciesword counts
map.putIfAbsent / computeIfAbsentAdd only if missinggraph.computeIfAbsent(u, k -> new ArrayList<>())
sb.append(x)StringBuilder: add to endnew StringBuilder("ab").append('c') → abc
sb.insert(i, x)Insert at indexsb.insert(0, "#")
sb.reverse()Reverse in place"abc" → "cba"
sb.delete(a, b) / deleteCharAt(i)Remove a range / one charsb.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 bitsInteger.bitCount(7) → 3
Character.isLetterOrDigit(c)Alphanumeric testisLetterOrDigit('_') → false
Character.toLowerCase / toUpperCaseChange one char's caseCharacter.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.

Practise Java with the AI mentor

Lesson test

Take an 8-question test on “Functions Reference: Core Utilities, Strings & Math” — concepts, output prediction, bug spotting and interview questions.

Can't find a Java topic you need for exams or placements? Request it and we'll add it.