Java Examples

Page 3 of 7

Browse Java examples

javaSnippet

Longest Common Prefix Scan

Scan column by column to keep only the characters every string shares, so grouped keys like deploy-api and deploy-web reduce to their deploy- stem.

javaSnippet

Math Abs Integer Min Overflow

Math.abs(Integer.MIN_VALUE) does not throw: the documented contract returns the same negative value, so widen to long or guard the boundary before negating.

javaSnippet

Multi Catch Exception Types

One multi-catch clause handles NumberFormatException and DateTimeParseException together; the types must be unrelated or the compiler rejects it.

javaSnippet

String Intern Canonical Pool

String literals share one pooled instance, a new copy is equal but not identical, and intern() returns the canonical pooled instance for the same content.

javaSnippet

Default Charset Is UTF-8

Confirm that Charset.defaultCharset is UTF-8 since JDK 18 regardless of platform, so charset-less getBytes and new String behave identically everywhere.

javaSnippet

ShutdownNow Drains Queued Tasks

Call shutdownNow on a busy single-thread executor to interrupt the running task and drain the queue: it returns the runnables that never commenced.

javaSnippet

UTF-8 BOM Survives readString

Java UTF-8 decoding does not strip the BOM, so Files.readString on an Excel-style export starts with U+FEFF and header matches silently fail.

javaSnippet

Zip FS Writes Archive Entries

Mount a ZIP via the zipfs provider with a jar: URI and create=true, create parent directories explicitly, then read and write entries with the Files API.

javaSnippet

Exit Codes For CLI Errors

Follow the CLI exit-code convention: zero for success, one for business failures, two for usage errors, with diagnostics written to stderr.