Java ist auch eine Insel

Donnerstag, September 06, 2007

Shortest Java Quine with 106 chars

Because of a contest in a German Java forum I tried to formulate the shortest Java Quine:

enum _{_;{String _="enum _{_;{String _=%c%s%1$c;System.out.printf(_,34,_);}}";System.out.printf(_,34,_);}}

To compile and run under Windows (with Unix you need to redirect Exceptions to /dev/null and use diff):

>javac _.java 
>java _ 2> NUL > Quine.out
> fc Quine.out _.java
Vergleichen der Dateien Quine.out und _.JAVA
FC: Keine Unterschiede gefunden
>jrunscript -e "print(new java.io.File('_.java').length())"
106

I haven’t found a shorter solution on the net, so I suppose this is the shortest for now.

AddThis Social Bookmark Button

10 Comments:

  • Could you please explain a little bit what it does? First of all, who can you start an enum that doesn't have a main method???

    By Anonymous Anonym, at September 06, 2007 1:25 PM  

  • Take a look at the description of a Quine from wikipedia: "In computing, a quine is a program, a form of metaprogram, that produces its complete source code as its only output." It does not need a main because it has a static-Block like this:

    class A
    {
    static {
    S.o.p("class A…");
    }
    }

    The static block is executed on runtime and printing "class A…". Of course there is an exception, but this is OK in this special case, because I can redirect errors to /dev/null and the program can print itself.

    By Blogger Christian Ullenboom, at September 06, 2007 1:30 PM  

  • Your quine-example misses a semicolon before the second S.o.p

    By Anonymous Anonym, at September 06, 2007 1:44 PM  

  • Oh yes. Copy and paste error. I corrected it.

    By Blogger Christian Ullenboom, at September 06, 2007 1:53 PM  

  • I don't get you regarding the "main is not necessary" explanation. If i'm trying to copy&paste and compile your code, i'm getting an "_.java:1: 'class' or 'interface' expected"-error as expected by myself :-)

    By Anonymous Matthias, at September 06, 2007 2:13 PM  

  • You are really using Java >= 5 to use enumeration?

    By Blogger Christian Ullenboom, at September 06, 2007 2:30 PM  

  • By throwing an exception, this program does not run properly. There is a runtime error, and you essentially sweep it under the rug.

    Your solution only works under certain conditions, and is therefore not valid.

    By Blogger David, at November 12, 2007 8:42 AM  

  • Sure I’m using special conditions. It depends on the definition of a quine. If you consider only Java programs without runtime errors to be “real quines” my solution is of course wrong. But if only the output of a Java program matters–and this is the assumption with the call “java _ 2> NUL > Quine.out” here–I think that this is valid. Besides, the Wikipedia definition does not explicitly forbid this, “In computing, a quine is a program, a form of metaprogram, that produces its complete source code as its only output. […] Note that programs that take input are not considered quines. This would allow the source code to be fed to the program via keyboard input, opening the source file of the program, and similar mechanisms. Also, a quine that contains no code is ruled out as trivial; in many programming languages executing such a program will output the code (i.e. nothing). Such an empty program once won the "worst abuse of the rules" prize in the Obfuscated C contest.”

    By Blogger Christian Ullenboom, at November 12, 2007 9:14 AM  

  • Runtime ERROR.

    ERROR.

    That means something. Any, ANY, program that fails at runtime obviously does not work. Any programmer can tell you this.

    Also, regarding your redirection of output - since you seem to love Wikipedia, here's a quote you've used: "a quine is a program [...] that produces its complete source code as its only output."

    Your program has in its output a runtime error. You just happen to output it somewhere else.

    By Blogger David, at November 13, 2007 6:49 AM  

  • I disagree with David above. As long as your only output is the source, it's a Quine!

    By Blogger Frank, at April 17, 2008 3:00 PM  

Kommentar veröffentlichen

<< Home