Posts tagged "java"

My Adventure Writing My First Quine in Java

Today, I tried to write my very first quine. What is a quine, you might ask? Well, for those that don’t know, a quine is simply a program that takes no input but whose only output is the program code itself. Easy, right? That’s what I thought. This post is going to document my adventure writing my first quine in Java.

 

Trial and error

So, I need to write a program that outputs a program…itself to be exact. I didn’t think much of it, so I dove right in. Let’s take a look at my first pass…

public class Quine {
	public static void main(String[] args) {
		System.out.println("public class Quine {\n\tpublic static void main(String[] args) {\n\t\tSystem.out.println(\"...fuck");
	}
}
 
/* OUTPUT:
public class Quine {
	public static void main(String[] args) {
		System.out.println("...fuck
 
*/

Fuck. This won’t work. As you can see, the problem quickly reveals itself. Essentially, there comes a point where you must print the code that you are using to print! (Even that sentence is a little confusing) This can easily degrade into an infinite loop. So, how do you overcome this?

You must write some code that you use to both store the characters
to be printed, but can ALSO be used to print itself!

I believe this is the main problem to solve when writing quines. Once you can get over this hump, the rest is simple! If you can figure this out, you’ve done 90% of the work. Really! So I started to tackle this problem, head on.

 

A string that prints itself

First, I did away with the System.out.println() method. Instead, I opted for the printf() method. With functionality very similar to the ANSII C printf() method, it has some powerful string manipulation features that will make this quine much easier to write (and read). Second, I tried to think of a way to write some code that printed itself. More specifically, I decided that I needed to figure out a way to solve the following code segment…

String s = "?";
System.out.printf(?);

What could I replace the question marks with such that it would output the string declaration itself, exactly, and nothing else? I thought about this for a while before I came up with a solution. Since I think that this step is the most important and requires the most thinking, rather than ruin it for you, I’ll let you think about it for yourself.

 

*SPOILER ALERT – ANSWER BELOW*


 

 

 

 
Willard Van Orman Quine - A Harvard Professor of philosophy for which the self-reproducing code concept was named after

Willard Van Orman Quine - A Harvard professor of philosophy for which the self-reproducing code concept was named after



 

 

 

 

After some head-scratching, I came up with…

public class Quine {
	public static void main(String[] args) {
		String s = "String s = %c%s%c;";
		System.out.printf(s, '"', s, '"');
	}
}
 
/* OUTPUT:
String s = "String s = %c%s%c;";
*/

There are many other solutions for this, but this is what I came up with. Now that I’ve got this figured out, all that’s left is filling in the stuff before and after.

 

All that’s left is everything

The self-reproducing code that I wrote above is essentially the middle of the program, and I’ve figured out a way to print it. Now, on to printing the stuff that goes before. This is quite a bit simpler than the first step, but not quite trivial. Again, here is another failed attempt…

public class Quine {
	public static void main(String[] args) {
		String s = "public class Quine {\n\tpublic static void main(String[] args) {\n\t\tString s = %c%s%c;";
		System.out.printf(s, '"', s, '"');
	}
}
 
/* OUTPUT:
public class Quine {
	public static void main(String[] args) {
		String s = "public class Quine {
	public static void main(String[] args) {
		String s = %c%s%c;";
*/

You’ll notice that hardcoding the line-feeds and horizontal tabs into the string doesn’t give us the right result. They get evaluated by the printf() twice. The solution to this is to make use of the variable arguments in printf() and reference those characters instead.

public class Quine {
	public static void main(String[] args) {
		String s = "public class Quine {%c%cpublic static void main(String[] args) {%c%c%cString s = %c%s%c;";
		System.out.printf(s, '\n', '\t', '\n', '\t', '\t', '"', s, '"');
	}
}
 
/* OUTPUT:
public class Quine {
	public static void main(String[] args) {
		String s = "public class Quine {%c%cpublic static void main(String[] args) {%c%c%cString s = %c%s%c;";
*/

Nice! Things are coming along, but the code is getting uglier.

 

Spring cleaning!

As you can see, we’re starting to get a lot of redundancy in the printf() call. My next step was to clean this up. Quine’s are all about reuse! The less code there is, the less code I have to worry about re-printing. So, I changed all of the arguments to printf() to be index-based instead, allowing me to reuse characters.

public class Quine {
	public static void main(String[] args) {
		String s = "public class Quine {%3$c%4$cpublic static void main (String[] args) {%3$c%4$c%4$cString s = %2$c%1$s%2$c;";
		System.out.printf(s, s, '"', '\n', '\t');
	}
}
 
/* OUTPUT:
public class Quine {
	public static void main (String[] args) {
		String s = "public class Quine {%3$c%4$cpublic static void main (String[] args) {%3$c%4$c%4$cString s = %2$c%1$s%2$c;";
*/

This is looking a lot cleaner now. Finally, let’s round this quine off by filling in the rest of the code.

 

Print the rest...easy! I think.

Again, this is quite a bit simpler than the previous steps, but not trivial. Another failed attempt…

public class Quine {
	public static void main(String[] args) {
		String s = "public class Quine {%3$c%4$cpublic static void main (String[] args) {%3$c%4$c%4$cString s = %2$c%1$s%2$c;%3$c%4$c%4$cSystem.out.printf(s, s, ...fuck";
		System.out.printf(s, s, '"', '\n', '\t');
	}
}
 
/* OUTPUT:
public class Quine {
	public static void main (String[] args) {
		String s = "public class Quine {%3$c%4$cpublic static void main (String[] args) {%3$c%4$c%4$cString s = %2$c%1$s%2$c;%3$c%4$c%4$cSystem.out.printf(s, s, ...fuck";
		System.out.printf(s, s, ...fuck
*/

We have hit the familiar problem of printing code that is used to print itself! This is slightly different, however. The difference is that we are trying to output characters that must be escaped in one output, but not in the other. There is a simple solution for this! Use the ASCII character codes instead! Replacing ‘”‘, ‘\n’, and ‘\t’ with their ASCII decimal equivalent codes, we are easily able to insert them into the string literal without them being interpreted by the printf() method, and without the need for escaping them. In the end, we get….

public class Quine {
	public static void main(String[] args) {
		String s = "public class Quine {%3$c%4$cpublic static void main (String[] args) {%3$c%4$c%4$cString s = %2$c%1$s%2$c;%3$c%4$c%4$cSystem.out.printf(s, s, 34, 10, 9);%3$c%4$c}%3$c}";
		System.out.printf(s, s, 34, 10, 9);
	}
}
 
/* OUTPUT:
public class Quine {
	public static void main (String[] args) {
		String s = "public class Quine {%3$c%4$cpublic static void main (String[] args) {%3$c%4$c%4$cString s = %2$c%1$s%2$c;%3$c%4$c%4$cSystem.out.printf(s, s, 34, 10, 9);%3$c%4$c}%3$c}";
		System.out.printf(s, s, 34, 10, 9);
	}
}
*/

Eureka! My very first quine! Try writing one for yourself! Feel free to share them with me in the comments, or through e-mail at charlesb[at]adobe[dot]com! I’d love to post them :)

Enjoy!

 
Charles