Skip to content

Instantly share code, notes, and snippets.

@benjholla
Created November 2, 2018 15:59
Show Gist options
  • Save benjholla/e0c5b47bab64f015095740d48366bb5f to your computer and use it in GitHub Desktop.
Save benjholla/e0c5b47bab64f015095740d48366bb5f to your computer and use it in GitHub Desktop.
Java Puzzle 21 (What does this program output?)
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
public class Puzzle21 {
public static void main(String[] args) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
PrintStream oldStream = System.out;
PrintStream newStream = new PrintStream(bytes);
System.setOut(newStream);
System.setErr(newStream);
System.out.print(0 + "_" + 0);
System.out.print(0_0);
System.out.print(0_0_0);
System.out.print(0_0_0L);
System.out.print(0_0_0.0F);
String string = new String(bytes.toByteArray());
System.setOut(oldStream);
try {
System.out.println(Double.parseDouble(string));
} catch (Throwable t) {
t.printStackTrace();
}
System.out.println("Cool.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment