Strange Hello World in Java

Overview

This is a strange Hello World program because it only needs to be compiled, not run. How does it work? Hint: it only works on MS-DOS/Windows. ;)

The Program

In a file called A.java or any name exception Con.java
class Con {
    String hi = "\n\n\tHello World!\n\n";
}
when compiled produces

How does this work

MS-DOS/Windows treats all files which start with CON (in any case and with any extension) as the console. So when javac writes the Con.class file, it is written to the console instead.

In the MS world; its not a bug, its a feature.

For more details

Use PRINT# to MS-DOS "CON" Device to Send ANSI Escape Codes

Comments

Post a Comment

Popular posts from this blog

Low Latency Microservices, A Retrospective

Unusual Java: StackTrace Extends Throwable

System wide unique nanosecond timestamps