Running on Java 17-ea+15-1230 (Preview)
Home of The JavaSpecialists' Newsletter

197What is the Meaning of Life?

Author: Dr. Heinz M. KabutzDate: 2011-12-06Java Version: 5Category: Language
 

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?

We have three upcoming LIVE virtual classes in April and May 2021:

  1. Refactoring to Streams and Lambdas for US$ 497 on April 6-7 2021 @ 9am-1pm Frankfurt Time.
  2. Extreme Java - Advanced Topics Java 17 Edition for EUR 1299 on April 19-20 2021 @ 9am-5pm Frankfurt Time. (almost sold out)
  3. Design Patterns Deep Dive for US$ 497 on May 11-12 2021 @ 7-11am Los Angeles Time.

My favourite course at the moment is the Refactoring to Streams and Lambdas course. We spend 8 hours ripping apart a 330k LOC application and replacing bits with more modern code. Too much fun! We still have a few places available for next week. The Advanced Topics Course is also very interesting. It is almost sold out though, so please grab the seats if you would like to join.

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" [ISBN 1400063515] 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

 

Comments

We are always happy to receive comments from our readers. Feel free to send me a comment via email or discuss the newsletter in our JavaSpecialists Slack Channel (Get an invite here)

When you load these comments, you'll be connected to Disqus. Privacy Statement.

Related Articles

Browse the Newsletter Archive

About the Author

Heinz Kabutz Java Conference Speaker

Java Champion, author of the Javaspecialists Newsletter, conference speaking regular... About Heinz

Superpack 21

Superpack 21 Our entire Java Specialists Training in one huge bundle more...

Free Java Book

Dynamic Proxies in Java Book
Java Training

We deliver relevant courses, by top Java developers to produce more resourceful and efficient programmers within their organisations.

Java Consulting

We can help make your Java application run faster and trouble-shoot concurrency and performance bugs...

Java Emergency?

If your system is down, we will review it for 15 minutes and give you our findings for just 1 € without any obligation.