Quine

Quine belongs to the group of problems that are very easy to formulate and understand but hard to solve.
The idea is to write a program that prints itself, this is, a program that outputs its own source code without being able to read it, of course.
It sounds fascinating but at a first glance seems not possible to be done with an ordinary language. The good news is that it can be done without any complex language feature.

Let's start by looking at an approach:
This pseudocode shows the basic structure of a Quine and gives us a important idea, take a look:

public static void main(String[] args) {
    
    String sourceLines = "the beginning of source code...  + 
                         String sourceLines = +
                         ; + 
                         the end of the source code.";
          
    // step 1: print up to: "String sourceLines =" inclusive;
	
    // step 2: print all the content of sourceLines string but within double quotes;
    
    // step 3: print the ";" and the end of sourceLines;
	
}

The key is to realize that in the sourceLines definition we can include the "recursive" definition of sourceLines: String sourceLines =
But this is not enough! Since we need to print this string into pieces, it will be better if we have an array of strings instead and there is one more problem the solve: we need to print these strings within double quotes but if we include double quotes in our strings when we will need to escape and print them, it is easier if we use the ascii code of the quotes (34) assigned the a char variable.

What follow is the implementation of these ideas in actual code:

package ar.com.kriche.demos;

/** A program that prints itself. */
public class Quine {

	public static void main(String[] args) {

		char doubleQuote = 34;

		String[] sourceLines = {
				"package ar.com.kriche.demos;",
				"",
				"/** A program that prints itself. */",
				"public class Quine {",
				"",
				"	public static void main(String[] args) {",
				"",
				"		char doubleQuote = 34;",
				"",
				"		String[] sourceLines = {",
				",",
				"				",
				"		};",
				"",
				"		// print up to the String[] sourceLines = { line:",
				"		for (int i = 0; i < 10; i++) {",
				"			System.out.println(sourceLines[i]);",
				"		}",
				"",
				"		// print the content of sourceLines array:",
				"		for (int i = 0; i < sourceLines.length; i++) {",
				"			System.out.println(sourceLines[11] + doubleQuote + sourceLines[i] + doubleQuote + sourceLines[10]);",
				"		}",
				"",
				"		// close sourceLines array and print the for statements:",
				"		for (int i = 12; i < sourceLines.length; i++) {",
				"			System.out.println(sourceLines[i]);",
				"		}",
				"",
				"	}",
				"",
				"}",
		};

		// print up to the String[] sourceLines = { line:
		for (int i = 0; i < 10; i++) {
			System.out.println(sourceLines[i]);
		}

		// print the content of sourceLines array:
		for (int i = 0; i < sourceLines.length; i++) {
			System.out.println(sourceLines[11] + doubleQuote + sourceLines[i] + doubleQuote + sourceLines[10]);
		}

		// close sourceLines array and print the for statements:
		for (int i = 12; i < sourceLines.length; i++) {
			System.out.println(sourceLines[i]);
		}

	}

}

And here the output of this code, which of course must be the same:

package ar.com.kriche.demos;

/** A program that prints itself. */
public class Quine {

	public static void main(String[] args) {

		char doubleQuote = 34;

		String[] sourceLines = {
				"package ar.com.kriche.demos;",
				"",
				"/** A program that prints itself. */",
				"public class Quine {",
				"",
				"	public static void main(String[] args) {",
				"",
				"		char doubleQuote = 34;",
				"",
				"		String[] sourceLines = {",
				",",
				"				",
				"		};",
				"",
				"		// print up to the String[] sourceLines = { line:",
				"		for (int i = 0; i < 10; i++) {",
				"			System.out.println(sourceLines[i]);",
				"		}",
				"",
				"		// print the content of sourceLines array:",
				"		for (int i = 0; i < sourceLines.length; i++) {",
				"			System.out.println(sourceLines[11] + doubleQuote + sourceLines[i] + doubleQuote + sourceLines[10]);",
				"		}",
				"",
				"		// close sourceLines array and print the for statements:",
				"		for (int i = 12; i < sourceLines.length; i++) {",
				"			System.out.println(sourceLines[i]);",
				"		}",
				"",
				"	}",
				"",
				"}",
		};

		// print up to the String[] sourceLines = { line:
		for (int i = 0; i < 10; i++) {
			System.out.println(sourceLines[i]);
		}

		// print the content of sourceLines array:
		for (int i = 0; i < sourceLines.length; i++) {
			System.out.println(sourceLines[11] + doubleQuote + sourceLines[i] + doubleQuote + sourceLines[10]);
		}

		// close sourceLines array and print the for statements:
		for (int i = 12; i < sourceLines.length; i++) {
			System.out.println(sourceLines[i]);
		}

	}

}

And now a C version of a short quine by Vlad Taeerov and Rashit Fakhreyev:

main(a){printf(a,34,a=main(a){printf(a,34,a=%c%s%c,34);}",34);}



Amazing but this was just a casual introduction! There is more: source code in a language that outputs source code in another language that in terns outputs the original source code!!
There are awesome examples out there, so just look them up!!