I know lots of people who were passionate about computers since they were just little kids. I wasn’t. I wanted to be a chief officer (seaman), a lawyer, a scientist or a graphical designer at different parts of my childhood. The first piece of code I wrote in 2002 was something like this (this is a CASIO fx graphical calculator with a BASIC dialect).

13 years later I work as a programmer - but it is not the passion for computers that drives me forward. It is helping people to solve their problems, and of course getting a chance to put some simple mathematics into it :)

A few weeks ago I was at the annual JavaZone where they had many great talks and quite many cool stands. At one of the stands they had a spinner, a wheel with numbers 1 to 20, and if you were lucky you could win a book. The girl at the stand said “You can choose one number and spin it twice or you can choose two numbers but only spin once”. “Is there a difference?” - I asked, “No” - the girl replied and shook her head. But really? Lets look at this…

Assume we had just one number and one spin, the probability to win is then 1/n where n is 20. Lets call it just p. Now, with two numbers the probabilty is 2/n or 2p.

With two spins it must be 1 minus the probability to loose on both attempts, that is 1-(1-p)^2. Or, put it another way, the probability to win on the first attempt plus the probability to win on the second attempt given that the first attemp has failed, that is p+(1-p)p. Either approach gives 2p-p^2.

The difference of p^2 is actually very interesting. Why is it or what does it represent? Well, both situations can be expressed as an “A or B” outcome, which can also be expressed as “A + B - A and B”. In both situations A and B have the same probability, p, but “A and B” is however different. In the first case we cannot win with both numbers at the same time, while in the second case it is possible, we can win on both spins.

This means that something we percieve as increasing our chance to win, in reality reduces it. It is probably why the most people at the stand chose to spin the wheel twice. As a true scientist, I sneaked to the stand whole 7 times, chose two numbers each time and didn’t win once. Well, probability and luck have nothing to do with each other I say.

This week a few colleagues and I have been to the JavaZone, which is an annual Java/JVM conference arranged here in Oslo. To me, a conference like this is a great opportunity to learn something new, meet new people and old friends and just have fun. Long story short, here are seven of the best talks, ranged by how good, interesting, funny or relevant they were to me. All seven are in English and are suitable for most (Java) developers:

  1. Practical Considerations For Microservice Architectures – a nice talk with some great slides about architecture, design, collaboration and tools.
  2. Making Steaks from Sacred Cows – a hilarious talk about cargo cult programming, how little do we know and the Matrix.
  3. Understanding Java Byte Code - an interesting and brief introduction to the Java byte code format.
  4. JVM tools - a good introduction to a number of tools such as jstat, jmap and mission control.
  5. Building a Big Data Machine Learning Platform - Cliff Click’s presentation of the H20 platform. Quite interesting!
  6. Java 8 Lambdas and Streams for Dummies - a nice but very basic introduction to Java 8. Also mentions using JMH for micro-benchmarking.
  7. 33 Things You Want to Do Better - an interesting talk; towards the end presents also some useful frameworks, such as Guava and LomBok.

As a bonus, for those who likes mathematics and understands Norwegian – Kontroversiell matematikk. Otherwise, more of this year’s presentations and lightning talks can be found here.

This example illustrates how to extract the sign (the leftmost bit), exponent (the 8 following bits) and mantissa (the 23 rightmost bits) from a float in Java.

int bits = Float.floatToIntBits(-0.005f);
int sign = bits >>> 31;
int exp = (bits >>> 23 & ((1 << 8) - 1)) - ((1 << 7) - 1);
int mantissa = bits & ((1 << 23) - 1);
System.out.println(sign + " " + exp + " " + mantissa + " " +
  Float.intBitsToFloat((sign << 31) | (exp + ((1 << 7) - 1)) << 23 | mantissa));

The same approach can be used for double’s (11 bit exponent and 52 bit mantissa).

long bits = Double.doubleToLongBits(-0.005);
long sign = bits >>> 63;
long exp = (bits >>> 52 & ((1 << 11) - 1)) - ((1 << 10) - 1);
long mantissa = bits & ((1L << 52) - 1);
System.out.println(sign + " " + exp + " " + mantissa + " " +
  Double.longBitsToDouble((sign << 63) | (exp + ((1 << 10) - 1)) << 52 | mantissa));

For more information look here.

I’ve been into a couple of interviews during the last two weeks. Today I got an interesting question: “reverse a single-linked list, in place”. The solution I suggested was good, but not great - “a better solution would use non-static recursion and be only two lines long”. After some thinking on the way home, I think the answer could look like this (updated version):

public class LinkedList {

  public String s;
  public LinkedList tail;

  public LinkedList(String s) {
    this.s = s;
    this.tail = null;
  }

  public LinkedList add(String s) {
    if (this.tail == null) {
      this.tail = new LinkedList(s);
    } else {
      this.tail.add(s);
    }
    return this;
  }

  public LinkedList reverse() {
    if (this.tail == null) {
      return new LinkedList(this.s);
    } else {
      return this.tail.reverse().add(this.s);
    }
  }

  @Override
  public String toString() {
    if (this.tail != null) {
      return s + " -> " + tail;
    } else {
      return s;
    }
  }

  public static void main(String[] args) {
    System.err.println(new LinkedList("s").add("i").add("m").add("o").add("n").reverse());
  }
}