Java Specialists' Java Training Europehome of the java specialists' newsletter

The Java Specialists' Newsletter
Issue 1972011-12-06 Category: Language Java version: Java 5+

GitHub Subscribe Free RSS Feed

What is the Meaning of Life?

by Dr. Heinz M. Kabutz
Abstract:
In this newsletter we try to calculate the meaning of life, with surprising results.

Welcome to the 197th issue of The Java(tm) Specialists' Newsletter sent to you from Chania in Greece. This past weekend I hit the "big four". I had asked Helene if she could perhaps organize a "surprise party" for me. I like celebrations, but do not enjoy being part of the steering committee. Well, it certainly was a surprise, since she drove off without telling me where the party was being held! She had told my sister, who had flown down from London to be with us, but Bettina had forgotten the name. Fortunately at this time of year most restaurant are closed, so we managed to find the "surprise party" on first attempt. Where else, but Irene's Taverna?

Join us on Crete (or via webinar) for advanced Core Java Courses:Concurrency Specialists Course 1-4 April 2014 and Java Specialists Master Course 20-23 May 2014.

What is the Meaning of Life?

This morning, whilst waiting for my Suzuki Jimny to be serviced, I wrote some demo code for my new concurrency course. I needed a long running method that would eventually return with some value. Since I had just experienced a change in age that typically precipitates introspection, I thought it would be fun to finally calculate the meaning of life. Since life appears to be filled with random "black swan" events, we should probably include Math.random() in our calculation.

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) {
    System.out.println(findOutWhatLifeIsAllAbout());
  }
}
  

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.

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

Kind regards

Heinz

Language Articles Related Java Course

Java Master
Java Concurrency
Design Patterns
In-House Courses



© 2010-2014 Heinz Kabutz - All Rights Reserved Sitemap
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners. JavaSpecialists.eu is not connected to Oracle, Inc. and is not sponsored by Oracle, Inc.
@CORE_THE_BAND #RBBJGR