I offer a solution to one beautiful task — writing code that outputs its text is valid for interpreters and compilers of different languages and is correctly executed when reversing its sources. Not so long ago I learned about code that can be both interpreted in PHP and compiled to Java: PhpJava.java. As it turned out, this idea is not new: code which is valid for several compilers or interpreters is called a polyglot. It is possible to write such code because of the peculiarities of processing strings and comments in different interpreters or compilers. For example, in Java, you can describe characters in the usual way, such as the character '/', or encoded as a Unicode character: 'u002F'. In the C# compiler, these entries will not be valid. However, if they are "hidden" inside a comment, then on the one hand they will not interfere with the compilation of C# code, on the other hand, they will be valid in Java code. For example, if you want code A to be compiled only in C# but not in Java, and code B to be compiled only in Java but not in C#, you should use the following fragment: C#-compiler will interpret this code in the usual way, comments will remain comments: But it is more interesting with Java, as Unicode characters will transform and the following will remain: By combining instructions that are valid for both languages and dividing the differences, you can write any program. Tricks with Unicode characters are not the only way. For example, in C-like languages, there are preprocessor directives that can determine which code should be compiled and which should not. You can put code of completely different languages into uncompiled code. For example, below you can find a polyglot code in C++ and Python (taken from here), where #if false section contains Python code. And the sequences ''' start and finish comments in Python. In HTML, for example, comments begin with the sequence <!--, which can also be used. There are also more complex polyglots that work simultaneously in six or even sixteen languages. The first polyglot displays "hitforum" message for all languages and the second one works more interestingly: it displays the name of the language in which it is compiled or interpreted. In order to write a polyglot quine in C# and Java, it is necessary to combine the principles of developing a quine and a polyglot. As it turned out, this topic is not new: on Codegolf site, users compete, whose polyglot quine, or polyquine, is shorter: Write a Polyquine. However, in this question, there were no variants with more verbose languages like C# and Java, and I wanted to fix that. For this polyglot quine, you need to escape characters that are forbidden in the lines of both languages and occur in the lines of the program itself. These characters are quotes ", line break \n and the backslash \. And to reduce the size of the code, duplicate character sequences such as \u000A, \u002F, and \u002A were also replaced with single characters in the encoding line. Here is an example of the resulting polyglot quine, which is valid for C# and Java compilers: Let's complicate the task even more: try to write a palindrome polyglot quine. I remind you that a palindrome is a number or a text which reads the same backward as forward. I described the principle of developing palindrome quines in the article several years ago (it was also the Day of the Programmer). The principle of the palindrome code is that the mirror part of the program is placed in a single-line or multi-line comment, which does not interfere with compilation. The simplest palindrome code in C# looks like this: As you can see, the multi-line comment /* starts from the middle and continues to the end, where it is closed by the sequence */. A blank comment at the beginning is necessary for the line to become completely symmetric. So, combining the principles of developing a palindrome, a polyglot, and a quine, I wrote the following code: Unfortunately, this monster turned out to be too big (1747 symbols), but this is explained by long Unicode strings and verbosity of C# and Java languages. I'm sure that in other languages it will be possible to write a palindrome polyglot quine of a much smaller size. Now let's check how this program is executed. First, get rid of all comments and format the code correctly: From the listing, it becomes clear that the variable s contains the code of the program, where characters prohibited in the lines are escaped, and duplicate sequences are compressed. Below is a table of symbols which is used to form the output string t. Next, the string t is concatenated with the inversion of itself so that the output is a palindrome. It is worth noting that the number 872 (the length of the string t) must be calculated after the quine has already been written. To do this, it is necessary to run the written code, which can also present particular difficulties. The main criteria when writing such code was to achieve the smallest text size, not the purity of the code. Therefore, of course, it looks somewhat strange. So, you can see that there is nothing difficult in writing such quines. For those who don't believe that this works, there are several tests in the repository Freaky-Sources (to run them, you need Java to be installed). Share in the comments the code of your quines-palindromes-polyglots in other languages or other interesting code-freaky things, I'll read them with pleasure. Software Developer at JetBrains (Kotlin Compiler)