Advertisement
Online Test Banks
Score higher
See Online Test Banks
eLearning
Learning anything is easy
Browse Online Courses
Mobile Apps
Learning on the go
Explore Mobile Apps
Dummies Store
Shop for books and more
Start Shopping

Weird Computer Code

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.
\u0050\u0076\u0064\u004c\u0020\u0031\u0020\u0041\u0070\u0072\u0039\u0036
 \u002a\u002f\u0020\u0063\u006c\u0061\u0073\u0073\u0020\u0068\u0020\u007b
  \u0020\u0020\u0070\u0075\u0062\u006c\u0069\u0063\u0020\u0020\u0020\u0020
   \u0073\u0074\u0061\u0074\u0069\u0063\u0020\u0020\u0076\u006f\u0069\u0064
    \u006d\u0061\u0069\u006e\u0028\u0020\u0053\u0074\u0072\u0069\u006e\u0067
     \u005b\u005d\u0061\u0029\u0020\u007b\u0053\u0079\u0073\u0074\u0065\u006d
      \u002e\u006f\u0075\u0074\u002e\u0070\u0072\u0069\u006e\u0074\u006c\u006e
       \u0028\u0022\u0048\u0069\u0021\u0022\u0029\u003b\u007d\u007d\u002f\u002a
 */

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.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win an iPad Mini. Enter to win now!