Ever heard of self-replicating or self-copying programs? Well there are viruses and worms that can self replicate to one system to the other. Without any involvement of external factors these destroy systems by propagating to one device to the other. Why we are reading this? Yes, there is a similarity. Quines are self-replicating codes or programs. We shall look in to more details.
The word Quine, was coined by Douglas R. Hofstadter, named after American mathematician and logician Willard van Orman Quine. It is a program that is written in any programming language and outputs its own code. The idea of producing this type of code is originated from the misconception that making such is impossible. So, to make it possible there are two building blocks: code and data.
The data represents the textual form of the code and is derived from in an algorithmic way by sometimes placing quotation marks(“) and sometimes in a more complicated way. The code uses the data to print the code and also uses it to print the data. It will get more clear once you go through the code.
The basic idea to produce such program is to initialize a string variable with a placeholder for interpolation. Finally printing the string interpolating the string into itself. This differs from one programming language to the other. It depends on how the variables are declared, method of interpolation, the quotes that are required, need for semi colons, newlines, etc. The possible examples are given in the Reference section.
Sample quine program in Java:
public class Quine
{
public static void main(String[] args)
{
char q = 34; // Quotation mark character
String[] l = { // Array of source code
"public class Quine",
"{",
" public static void main(String[] args)",
" {",
" char q = 34; // Quotation mark character",
" String[] l = { // Array of source code",
" ",
" };",
" for(int i = 0; i < 6; i++) // Print opening code",
" System.out.println(l[i]);",
" for(int i = 0; i < l.length; i++) // Print string array",
" System.out.println(l[6] + q + l[i] + q + ',');",
" for(int i = 7; i < l.length; i++) // Print this code",
" System.out.println(l[i]);",
" }",
"}",
};
for(int i = 0; i < 6; i++) // Print opening code
System.out.println(l[i]);
for(int i = 0; i < l.length; i++) // Print string array
System.out.println(l[6] + q + l[i] + q + ',');
for(int i = 7; i < l.length; i++) // Print this code
System.out.println(l[i]);
}
}
//Source: https://en.wikipedia.org/wiki/Quine_(computing)
This is the basic understanding of what quine is. More details can be referred in Reference.
Well! If you are interested to know about in any other language, we have the translation option available on the side bar (on desktop) or the translation button at the below (mobile view). We have got you something even more interactive now!! We have gone through the code in Java here. We have come up with an explanation for a Python quine code along with the definition. Here is our Video on Quine. Check it out!
Related Terms:
Interpolation: It is a process of substituting the placeholders in a string with the values of variables. For more understanding, will take an example.
We generally use templates to create posters or pictures and post on any social media. What we do in templates? We replace the value given there with the one we require. The template just keeps the placeholder for the value to be replaced with. Hope you got the term!!