The string encodes to slightly different values with the standard and URL base64 encoders (trailing + vs -) but they both decode to the original string as desired.
In Java, we use the java.util.Base64 class for base64 encoding and decoding. The Base64.getEncoder() and Base64.getDecoder() methods provide standard base64 encoding and decoding, while Base64.getUrlEncoder() and Base64.getUrlDecoder() provide URL-safe encoding and decoding.
The encodeToString() method is used for encoding, which takes a byte array as input. To decode, we use the decode() method, which returns a byte array. We then convert this byte array back to a string for printing.
Note that in Java, decoding can throw an IllegalArgumentException if the input is not properly formatted base64. It’s good practice to catch this exception when decoding.