By Barry Burd

Everybody needs break once in a while. If you’re a hard-working student of the Java programming language, you should take time off to look at some programming language quirks — peculiar things you find in Java and other programming languages. Most of these quirks are amusing, and some of them are downright mind-boggling!

Obfuscated code

If you visit https://weblogs.java.net/blog/forax/archive/2006/10/obfuscated_java.html, you find one of the strangest Java programs ever written:

/*   Just Java
     Peter van der Linden
     April 1, 1996.
u0050u0076u0064u004cu0020u0031u0020u0041u0070u0072u0039u0036
 u002au002fu0020u0063u006cu0061u0073u0073u0020u0068u0020u007b
  u0020u0020u0070u0075u0062u006cu0069u0063u0020u0020u0020u0020
   u0073u0074u0061u0074u0069u0063u0020u0020u0076u006fu0069u0064
    u006du0061u0069u006eu0028u0020u0053u0074u0072u0069u006eu0067
     u005bu005du0061u0029u0020u007bu0053u0079u0073u0074u0065u006d
      u002eu006fu0075u0074u002eu0070u0072u0069u006eu0074u006cu006e
       u0028u0022u0048u0069u0021u0022u0029u003bu007du007du002fu002a
 */

To the naked eye, the program looks like one big comment. And if you paste the code into an Eclipse editor, most of the code is green (meaning that it’s part of a comment). When you run the code, nothing should happen. But looks are deceiving. When you run this code, the output is

Hi!

This code is an example of an obfuscated program. An obfuscated program is one that’s intentionally difficult for people to read and understand. There are two reasons for creating obfuscated code: (1) for fun, and (2) to keep other people from understanding your coding tricks and stealing your ideas.

Many commercial products can turn regular code into obfuscated code, but the most amusing examples come from people who write obfuscated code from scratch. For other examples of obfuscated Java code, visit Semantic Designs and the blog Note to self.

Surprising language behavior

Some languages have features that defy intuitions. The following example (and many others) are from stackoverflow.com.

<html>
<head>
<script>
function writeStuff()
  {
  document.writeln('5' + 3)
  document.writeln('<br>')
  document.writeln('5' - 3)
  }
</script>
</head>
<body onload="writeStuff()">
</body>
</html>

In this example, the lines between the <script> and </script> tags are JavaScript code. JavaScript has little to do with Java. (The names Java and JavaScript are similar only for historical reasons.) People use JavaScript primarily as a language for embedding code inside web pages.

In JavaScript, the expressions 5 + 3 and 5 – 3 are very similar, so you’d guess that the two expressions behave the same way. But it’s just not so! If you visit this section’s code in a web browser, you see

53 
2

The plus sign concatenates the character 5 and the character 3; the minus sign subtracts the number 3 from the number 5. You’d never guess it!

Self-reproducing programs

A Quine is a program that outputs a copy of itself, despite the fact that it receives no input. The site The Quine Page is a repository for Quine examples. The site contains Quines in about 50 different programming languages. The following program (created by Bertram Felgenhauer) is almost a Quine — “almost,” because to make it a Quine, you have to type the entire program on one line with no line breaks. (For the purposes of this article, the line breaks are present so you can see all the code at once.)

class S{public static void main(String[]a){
String s="class S{public static void main(String[]a){
String s=;char c=34;System.out.println
(s.substring(0,52)+c+s+c+s.substring(52));}}";
char c=34;System.out.println
(s.substring(0,52)+c+s+c+s.substring(52));}}

The world of programming languages

You may hear some fresh faced programmers say that they want to learn to code in all the programming languages. Good Luck! There are thousands of computer programming languages. (You can see the influences that popular languages have on each other by visiting IBM.com.)

The page esolangs.org lists about 800 of the more obscure languages. Roughly one in ten of these languages were created as jokes. (The joke languages have their own special page at http://esolangs.org/wiki/Joke_language_list.)

One favorite language isn’t in the Esolang list. It’s the Unbounded Register Machine language which has only three statement types. You can add 1 to a variable, subtract 1 from a variable, or loop while a variable’s value isn’t zero.

myVar++
myVar--
while (myVar != 0) {
}

That’s all you can do in the Unbounded Register Machine language. But here’s the punch line: By combining statements, one after another, in this language, you can implement any strategy that you can implement in Java (or in any other general-purpose, industrial-strength programming language). For more information, visit Portland State University’s page.