Language differences in unexpected places

At the moment, I am mainly using two programming languages: C# during work and Java for my hobby project (http://strategicrps.com/..Yes, I am still working on it 😉 ).

Because of the many similarities between these two languages (at least in syntax), it is quite easy to switch from one to the other. However, it is also because of these similarities that it is sometimes easy to forget that they actually are different languages using different frameworks.

Recently, I came across one of these instances, and I thought it would be nice to share.

As an introduction, I want to ask you the following question: In the two code snippets below. What will be the values of “exists1” and “exsits2”?

C#

List<int> intList = new List<int>();
Dictionary<object, int> dictionary = new Dictionary<object, int>();

dictionary.Add(intList, 7);

bool exists1 = dictionary.ContainsKey(intList);

intList.Add(3);

bool exists2 = dictionary.ContainsKey(intList);

Java

List<Integer> intList = new ArrayList<Integer>();
HashMap<Object, Integer> hashMap = new HashMap<Object, Integer>();

hashMap.put(intList, 7);

boolean exists1 = hashMap.containsKey(intList);

intList.add(3);

boolean exists2 = hashMap.containsKey(intList);

Even though the snippets are very similar (ArrayList and HashMap are the Java equivalents to List and Dictionary in C#.NET), the answers actually differ. In the C# code, both booleans will be “true”, while in the Java code, the second value will be “false”.

Both HashMap and Dictionary first use the object hash method (hashCode() in Java and GetHashCode() in C#) for mapping, followed by the equals method (equals(Object) in Java and Equals(object) in C#) in case of collisions. This is where the difference occurs: whereas the ArrayList in java implements its own hash method based on its content, the List object in C# does not. As we change the contents of the ArrayList in the example above, the original hash value used as the key for the HashMap, is changed, and the list cannot be found anymore. In case of the List in C#, the hash implementation of the object class is being used, which is related to the memory location of the object.

As I have the most experience with C#, I did not expect the hash code of the ArrayList to change when changing the contents of the list. That being said, if I actually think about it, the Java implementation actually makes more sense to me; The hash code should change with the state of a class. My experience with the way C# works simply conditioned me not to give this a second thought.

Ironically, if you have a look at the source code of the object class in C#, it has the following comment:

// GetHashCode is intended to serve as a hash function for this object.
// Based on the contents of the object, the hash function will return a suitable
// value with a relatively random distribution over the various inputs.
//
// The default implementation returns the sync block index for this instance.
// Calling it on the same object multiple times will return the same value, so
// it will technically meet the needs of a hash function, but it's less than ideal.
// Objects (& especially value classes) should override this method.

If I were to interpret this comment, I would actually expect that a class like List should have overwritten the GetHashCode method, just like Java does.

Before wrapping up, I feel like I have to add that I do realize you should never use mutable objects as keys, nor in C# or in Java. The case I came across was very different from this one, and I merely created this case as an example.

Even though this is probably not something you will ever encounter (if you do, there is a big chance that something else is wrong with your implementation), I still thought it was an interesting subject to write about. It is not only about the underlying technicals. It is also a reminder of the things we can encounter when we start to think we understand it all.

Whatever your personal take is on this subject, I hope it was at least an interesting read.

Moved to a new home

It has certainly been a while since I last updated my blog (luckily I can secretly add some backdated posts to get back on track), but it has been quite a hectic period recently.

Among other things, I have been moving to a different apartment, which is taxing enough. Coincidentally, the apartment came available during a period I had already planned a vacation to Morocco. In the end, I was able to combine the two, but it certainly was a stressful period.

Of course moving always requires a lot of work to be done on the new home, which was the reason I had less time for writing and coding in my free time.

Now I am happy to report that I am all settled and ready to get back to work on my hobbies. I am already hard at work on writing a new article for Mind Dough and am also working on a new version for strategic RPS. Stay tuned!

Training: Successful Presentation

The past two days I have been attending a presentation training.

Presenting is not a weakness of mine, and especially when I am into the subject and had sufficient time to prepare, I like to think I am already quite good at it!

The reason for registering for a training like this is two-fold: Firstly, I believe it is always a good idea to test your theories, especially if they are about yourself. But secondly, and maybe more importantly, I registered for this training because I had become inspired by what I saw during my PIT-Stop training.

The trainers over there were able to tell a story so naturally and so inspiring. Something that made me realize there is still a ton to learn!

The training itself was quite nice. The setting was relatively intimate (understandably, as many people also take this training in order to get over a fear of presenting) which made that there was plenty of time for feedback and practice.

Even though I did not have a concrete goal of what to improve during my training, I did leave with some quite useful tips I will continue to practice on.

Publication: The Consequences of a Conscious Deterministic Simulation

With the recent progress in computing, and more specifically artificial intelligence (mainly neural networks), it is not strange that we see more and more sources say that it is only a question of time before such a computer program will eventually surpass the capabilities and intelligence of the human brain. A scary thought for one, an interesting thought for another. But if this is indeed a possibility, what will this mean for (human) consciousness and our understanding of it?

This article will explore the consequences for the existence of consciousness in a very specific type of computer simulation: a deterministic simulation.

Enjoy!

(Also published on LinkedIn: https://www.linkedin.com/pulse/consequences-conscious-deterministic-simulation-jasper-lammers)

Cultural Awareness

Recently I have been asked whether or not I would be prepared to visit other countries for work. As this sounds very interesting to me, I said yes, and I am looking forward to the opportunity to do so.

As part of preparation, I also signed up for the training about cultural awareness that Vanderlande provides.

At first, I was going there to learn more about countries like China and India. Countries with an obvious and very large culture difference with the Netherlands. I was surprised to learn however, that the culture differences between countries like the Netherlands, Germany, Belgium and Great Britain is also very large!

This was one of those trainings where you think you are going to learn one thing, but you learn another. It was very interesting, and it really helped me understand on how to work together with people from other countries. Not only will I be using this for working in other continents, but also when working with my colleagues from Germany.