Monday, 19 April 2010

Our new home

Hi all,

For those of you who weren't aware, as soon as I came back from New Zealand and Australia I was forced to move as the landlord was selling the place.  We've found a great new Garden flat not too far from the old location but the lack of broadband at home is still killing me and is sadly stopping me from contributing to my favourite Open Source projects :(.  The new place is taking shape quite rapidly now and it already has that homely feel that only good company can provide.  Mind you it doesn't hurt that my wife is an extremely skilled Graphic Designer/Artist and just magically knows where things should go for maximum utility and impact (how do they do that?!).  It's a good thing that she keeps me busy manually shifting things about so I don't come up with my somewhat ill thought out ideas on interior decorating.

We also have our new Cat "Lucifer" (courtesy of our lovely flatmate) who is great company around the place, but his habit of scratching at the door and meowing at all hours of night is not quite as well received.  But we're determined to stay strong and not let him come in and purr loudly and be cute at 3am in the morning, I wonder who's going to win :)

We live in a lovely area of North London with plenty of parks and small local places/shops for local people, making it more like living in a pleasant continental European suburb than the cold concrete jungle that London can sometimes come across as.  Still, as "Yummy Mummies" with their prams, countless Dog walkers and over enthusiastic joggers go by it's hard to think what I'm quite doing in this type of neighbourhood.  Having started in the hustle, bustle and grime of East London, I find myself missing the soul and energy of that part of town.

So if you're in the North London neighbourhood then drop me a line, I happen to know of a good pub or two to while the hours away.....

Are there yet?  The big bag of unpacked clothes in our bedroom says no :)

Thursday, 8 April 2010

Java Experiments

Hi all,

So I've been frantically packing and moving over the last week which means once more I have a pithy excuse to have posted as regularly as I promised, my bad!

Again this is a Java posting, so a warning to those adverse to seeing source code!

I ran across another common misunderstanding with Java for those first developing with it (and indeed for some more experienced developers!).  This was also highlighted in a recent TheDailyWTF article.  The mis-understanding is explained in the code below and is often asked about as an interview or certification question.

Thanks to Scott Selikoff, (I co-opted his code example).

package org.martijnverburg.experiments;

/**
 * This class runs experiments to prove various pass by value 
 * and pass by reference rules in the JDK
 *
 * The use of System.out.println is generally frowned 
 * upon, but I'll forgive myself in this case.
 * 
 * This probably should be a nice Unit test, many 
 * apologies!
 *
 * @author Martijn Verburg (aka karianna)
 */
public class PassByValuePassByReferenceExperiments
{

  /**
   * Fire off the experiments
   *
   * @param args - Not used
   */
  public static void main(String[] args)
  {
    PassByValuePassByReferenceExperiments test = new PassByValuePassByReferenceExperiments();
    test.changeValueOfObjectPassedIn();
  }

  /**
   * This method challenges a false assumption whereby the 
   * developer may think that when they pass an object reference 
   * into a method as an argument, then change the value of 
   * that object in the method, that the reference they hold 
   * onto in the calling method retains that changed value.
   *
   * TODO:  The explanation above isn't the clearest English, 
   * try to tidy that up later.
   *
   * If you misunderstand how this works in Java you may expect
   * to see the result as:

   * 
   * 10
   * 10
   *
   * The actual output of this experiment is:
   * 
   * 10
   * 3
   */
   private void changeValueOfObjectPassedIn()
   {
     Integer x = new Integer(3);
     changeX(x);
     System.out.println("Value of x is = [" + x + "]);
   }

  /**
   * The key concept here is the the Integer x declared 
   * in the argument list for the method is a local copy 
   * of the original reference.  So when you alter its value, 
   * you're only altering the local version, which incidentally 
   * falls out of scope when the method ends.
   */
   private void changeX(Integer x)
   {
     x = new Integer(10);
     System.out.println("Value of x is = [" + x + "]);
   }

}

So are we there yet?  Sadly no, all to often developers forget the fundamental rules like this and for new developers this concept can be especially confusing.


But although some developers might look down upon those who don't have the fundamentals memorised, I actually feel that it can be OK to be a bit forgetful or hazy, after all you can't remember it all!  I say that with the proviso that you should actively engage in improving your fundamentals knowledge, writing tiny test cases/experiments like the above code can be a very helpful aid.