The Artima Developer Community
Sponsored Link

Doing Things with Java that Should Not Be Possible
What is the meaning of life?
by Heinz Kabutz
December 22, 2011
A couple of weeks ago, I sent out a little quiz to my readers of The Java Specialists' Newsletter. No one managed to figure out what the code does without running it. Some managed to explain the result once they had run it. Perfect quiz for weeding out those job applicants you don't like. Especially in the banking industry. Enough hints :-)


A few weeks ago I updated my age to be a factor of 2 and 5. It is the perfect age to reflect what life is all about. Some men don a leather jacket and ride around on a Harley. But as a geek I know exactly where to turn to - my beloved computer.

I needed a long-running method for the new concurrency course I am writing. Something that would take about 15 seconds and that would keep the CPU busy at 100%. Also, since life throws us random events, we would have to include a call to Math.random() in the calculation. In my next article I will explain why Math.random() is dead, long live Java 7, but for now we will call it in order to be super slow.

public class MeaningOfLife {
  public static String findOutWhatLifeIsAllAbout() {
    int meaning = 0;
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 20; j++) {
        for (int k = 0; k < 300; k++) {
          for (int m = 0; m < 7000; m++) {
            meaning += Math.random() + 1;
    return String.valueOf(meaning).replaceAll("0*$", "");

  public static void main(String[] args) {

Think about what the output should be before you run it. Then try it out, preferably with the -server switch. On my machine it takes 15 seconds to find the meaning of life with -server and 25 seconds with -client. Patience is apparently a virtue, though I would not be able to confirm or deny that. Never had patience, which is why I always want the fastest meanest computer that I can get.

The question is: Why is it giving this result? And why does it even compile?

(And yeah, my Artima profile picture seriously needs to be updated. And I now live on the sunny Island of Crete in the Mediterranean Sea. But at least I've blogged again on Artima :-))

Talk Back!

Have an opinion? Readers have already posted 57 comments about this weblog entry. Why not add yours?

RSS Feed

If you'd like to be notified whenever Heinz Kabutz adds a new entry to his weblog, subscribe to his RSS feed.

About the Blogger

Heinz Kabutz enjoys driving Java to the limits, and then a bit beyond. He has been programming in Java since 1997 on several very unimportant projects. During that time, he has picked up some horrifying tips on how you can get the most out of Java. These are published on his bi-monthly "The Java(tm) Specialists' Newsletter" ( It is not for the uninitiated :-) Heinz received a PhD in Computer Science from the University of CapeTown. He loves living in South Africa as it is both beautiful and interesting. Professionally, Heinz survives by writing Java code, insulting, ahem, consulting, and presenting courses on Java and Design Patterns.

This weblog entry is Copyright © 2011 Heinz Kabutz. All rights reserved.

Sponsored Links


Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use