SwadhyAI Notes

All Java notes

Java Reference

›Short versionOpen if you only have a minute.
  • These pages are contracts: what goes in, what comes back, and whether the original object changes.
  • A call that looks like an edit often returns a new value, or a view of the same data.
  • Search methods often return -1 or false when nothing matches. They do not throw.
  • Index ranges are usually half-open: the start is included, the end is excluded.
  • Anything that opens a file, reader, writer, or scanner belongs in try-with-resources.

Use these pages when you already know the broad idea and need the exact behavior: what goes in, what comes back, what mutates, and which edge cases usually surprise beginners.

Methods

APIParametersReturnsBehaviorWatch for
String methodsIndexes, substrings, regex patterns, replacement values.Usually a new String, boolean, char, int, or array.Strings are immutable, so transformation methods return another value instead of editing the original.Index ranges are zero-based and end indexes are usually exclusive.
Collections and arraysIndexes, elements, collections, comparators, predicates.Elements, booleans, sizes, arrays, views, or void for mutating utilities.Collection methods may mutate in place, while some utilities return read-only views or backed subviews.Removing while iterating needs Iterator.remove() or removeIf().
Input and outputStreams, readers, writers, format strings, paths, buffers.Bytes, characters, lines, counts, booleans, or void.I/O APIs often hold external resources and should be closed with try-with-resources.Checked IOException is part of many file workflows.
System and exceptionsStatus codes, property keys, arrays, Throwable objects.Time values, properties, streams, or nothing.System touches JVM-level state; exception APIs describe failures and recovery paths.System.exit() ends the JVM; catch specific exceptions before general ones.

Which call fits

OptionBest forTradeoff
Array vs ArrayListArrays suit fixed-size primitive-heavy data; ArrayList suits resizable object lists.Arrays are compact, but ArrayList has richer methods and automatic resizing.
Scanner vs BufferedReaderScanner is convenient for token parsing; BufferedReader is better for fast line reading.Scanner is easier but slower and has newline traps after numeric reads.
File vs stream classesFile describes paths and metadata; streams/readers/writers move bytes or text.File cannot read file contents by itself.

Examples

Pick the API by the job

java
String line = "id=42,name=Ada";
String[] parts = line.split(",");

ArrayList<String> names = new ArrayList<>();
names.add(parts[1].substring(5));

System.out.println(names.get(0)); // Ada

Resource-safe file workflow

java
try (BufferedReader reader = new BufferedReader(new FileReader("users.txt"))) {
    String firstLine = reader.readLine();
    System.out.println(firstLine);
} catch (IOException e) {
    System.err.println("Could not read file: " + e.getMessage());
}

You call name.trim() and then print name. The spaces are still there. What did the call actually do?

It built a new string with the spaces removed and threw that result away, because nothing stored it. The variable name still points at the original string. Assign the result, name = name.trim(), if that is the value you want to keep.