Recent Entries »

How I bought a car to complete my first marathon

2008 PEC Marathon Medal

The following is a somewhat long-winded first hand account of my first marathon, which I completed earlier this year, but took some time to write about.

It was a usual Friday afternoon, the time of the week that I usually can’t wait for. However, this Friday I was feeling a little bloated and slow having come back from a lunch buffet where I’d overindulged.

To work off this lethargic feeling, I decided to visit the gym after work – something that I only do infrequently on Fridays, being keen on getting home as early as possible in anticipation of the weekend. I’m fortunate enough to have a workplace that has a gym onsite – but this also means any excuses I’d have for not going would only be made weaker.

Dragging myself to the gym, I started into my routine. In between reps and trying to catch my breath, I starting making some small talk with Brian, one of my coworkers, who was on the bench beside me.

“I don’t normally see you in here this time of day,” I said.

“I usually come in at lunch,” he responded. “But a lunch meeting ran overtime today.”

“Are you working out to prepare for hockey?” I asked, referring to the recreational league that was starting in September.

“No,” he replied, slowly. “I’m in here crossing-training for the marathon.”

That last word, marathon, set off a trigger in my mind.

Continued

Driving Gloves

Ever since getting my new car back in September, things have been great. That is, until things started getting colder. Besides having to brush off snow/ice after every snowfall, the cold weather also makes driving more uncomfortable.

More specifically, holding a cold steering wheel after you’ve just spent the past few minutes outside chipping ice off of your car sucks. Not having the benefits of a heated steering wheel (do such things exist?) I was left with the option of keeping my gloves on while driving, at least until the car warmed up sufficiently. The only problem was that my gloves didn’t provide enough grip and thus driving wasn’t as fun.

Continued

Handling mutable fields in Java

In Java, a mutable object is one whose state can be altered after it has been instantiated. An immutable object is one whose state is fixed after instantiation; that is, the data represented by the object cannot be changed in that object. Perhaps the most well-known immutable type is the built in String class; while there are methods on the String class that seemingly alter its state (such as toUpperCase() and trim()), in actuality these methods return a new String object if changes had to be made. In this article I’ll discuss how mutability will affect how you expose private fields in objects.

Pop Quiz

Consider the following code fragment. We create a MapContainer object, and then get the contained map, which is guaranteed to have a certain value associated with the key “today”. We then alter the value associated with this key, using our local reference to returned map. We then query the MapContainer object and get the contained map again. What is the value associated with the key “today” in this map?

final MapContainer mapContainer = new MapContainer();
final Map<String, String> map = mapContainer.getKeyValuePairs();

final String today = map.get("today");
assert null != today;
System.out.println(today);  // Returns the current date-time.

// Change the value using our local reference.
map.put("today", "tomorrow");

final Map<String, String> mapAgain = mapContainer.getKeyValuePairs();
System.out.println(mapAgain.get("today")); // What is output?

Don’t waste too much time on this problem, as it’s a trick question. The answer actually depends on the implementation of MapContainer. Depending on how it’s implemented, the second output could be unchanged from the first or be changed to the new value of “tomorrow”.

Continued