javaweb-submit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Javaweb-submit] shorter self-printing Java program


From: Eric Blake
Subject: [Javaweb-submit] shorter self-printing Java program
Date: Tue, 26 Jun 2001 14:43:20 +0100

As long as you are willing to bend the rules, here is the shortest
self-printing Java program I could come up with (162 bytes):

class s{static{new s(){{System.out.print((s+=(char)34)+s+';'+'}');}};}String
s="class s{static{new
s(){{System.out.print((s+=(char)34)+s+';'+'}');}};}String s=";}

Notice that `java s | diff s.java -` completes successfully, indicating that
output to stdout matches the original program exactly.  This behavior is due
to a bug in the JDK which executes the static initializer before throwing
the NoMethodFoundError for the non-existant main method.

If the output to stderr bothers you, use the 192 byte version below, which
is still shorter than the programs listed at
http://www.gnu.org/software/java/selfprint-java.html:

class _{static{new
_(){{System.out.print((_+=(char)34)+_+';'+'}');}};System.exit(0);}String
_="class _{static{new
_(){{System.out.print((_+=(char)34)+_+';'+'}');}};System.exit(0);}String
_=";}

--
Eric Blake




reply via email to

[Prev in Thread] Current Thread [Next in Thread]