Base64.getEncoder produces an ASCII-safe string from arbitrary bytes, and Base64.getDecoder reverses it exactly, which is how binary data travels safely through text-only channels like JSON or URLs.
byte[] original = "hello, base64".getBytes(java.nio.charset.StandardCharsets.UTF_8);
String encoded = Base64.getEncoder().encodeToString(original);
System.out.println("Encoded: " + encoded);
byte[] decoded = Base64.getDecoder().decode(encoded);
System.out.println("Decoded: " + new String(decoded, java.nio.charset.StandardCharsets.UTF_8));
Encoded: aGVsbG8sIGJhc2U2NA==
Decoded: hello, base64
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-09-27