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.

10 Antwort(en) auf ›Shortest Java Quine with 106 chars‹

  1. # Anonymous Anonym

    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???  

  2. # Blogger Christian Ullenboom

    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.  

  3. # Anonymous Anonym

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

  4. # Blogger Christian Ullenboom

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

  5. # Anonymous Matthias

    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 :-)  

  6. # Blogger Christian Ullenboom

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

  7. # Blogger David

    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.  

  8. # Blogger Christian Ullenboom

    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.”  

  9. # Blogger David

    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.  

  10. # Blogger Frank

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

Kommentar veröffentlichen