<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>adams.co.tt blog - tagged with java</title>
  <id>http://adams.co.tt/</id>
  <updated>2009-07-03</updated>
  <author>
    <name></name>
  </author>
  <entry>
    <title>Don't use Java primitives</title>
    <link rel="alternate" href="http://adams.co.tt/blog/2009/07/03/dont-use-java-primitives/"/>
    <id>http://adams.co.tt/blog/2009/07/03/dont-use-java-primitives/</id>
    <published>2009-07-03</published>
    <updated>2009-07-03</updated>
    <author>
      <name></name>
    </author>
    <content type="html">&lt;p&gt;Java syntax evolved from C&amp;rsquo;s and as a result has the primitives:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;byte short int long float double boolean char&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;These primitives can be considered to be the building blocks for all other value objects within Java and allow users to hold data as fields and variables. However, I would argue that they are not useful and a good rule of thumb is to not use them at all. Instead Java provides the classes:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;Byte Short Integer Long Float Double Boolean Character&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;These are classes for fully fledged, immutable, value objects which can be used in much the same way as the basic primitives, but offer the following advantages:&lt;br /&gt;&lt;br /&gt;1. They can be used in generics. This is possible:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;new ArrayList&amp;lt;Integer&amp;gt;();&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;But this is not:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;new ArrayList&amp;lt;int&amp;gt;();&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;2. They can still be passed into library functions that demand primitives. Auto boxing means that a method signature with &lt;code&gt;int&lt;/code&gt; can still be called with an &lt;code&gt;Integer&lt;/code&gt; objects transparently. Likewise, a function returning an &lt;code&gt;int&lt;/code&gt; can be assigned to an &lt;code&gt;Integer&lt;/code&gt;.&lt;br /&gt;&lt;br /&gt;3. They offer all the expected methods for use in debugging and collection handling, such as &lt;code&gt;toString&lt;/code&gt;, &lt;code&gt;equals&lt;/code&gt; and &lt;code&gt;hashcode&lt;/code&gt;.&lt;br /&gt;&lt;br /&gt;4. They can be null. While there is a lot of discussion as to whether applications should be passing null around, it is still sensible to ask the question what should be returned from a method such as &lt;code&gt;getIdIfPresent&lt;/code&gt; if there is no ID. With primitives, you&amp;rsquo;d be forced to return a value that represented a nonexistent result, such as &lt;code&gt;-1&lt;/code&gt;. WIth real objects, you have the option of returning &lt;code&gt;null&lt;/code&gt;.&lt;br /&gt;&lt;br /&gt;5. Primitives in java are passed by value, whereas objects in java are passed by reference. This may not have a huge impact on how you would handle &lt;code&gt;Integer&lt;/code&gt; vs &lt;code&gt;int&lt;/code&gt;, however. This is because the objects that represent the primitive types are immutable, and therefore you cannot change the value of any of the parameters passed. Despite this, I would recommend using objects over primitives, simply because it means you don&amp;rsquo;t have two kinds of behaviour represented in your code.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Consider overriding Equals and Hashcode as a general rule</title>
    <link rel="alternate" href="http://adams.co.tt/blog/2009/06/15/consider-overriding-equals-and-hashcode-as-a-general-rule/"/>
    <id>http://adams.co.tt/blog/2009/06/15/consider-overriding-equals-and-hashcode-as-a-general-rule/</id>
    <published>2009-06-15</published>
    <updated>2009-06-15</updated>
    <author>
      <name></name>
    </author>
    <content type="html">&lt;p&gt;In both C# and Java, objects have methods for checking equality and producing hashcodes. For the purposes of this post I&amp;rsquo;ll mostly refer to the java methods &lt;code&gt;equals&lt;/code&gt; and &lt;code&gt;hashcode&lt;/code&gt;, but C# has the equivalent methods &lt;code&gt;Equals&lt;/code&gt; and &lt;code&gt;GetHashCode&lt;/code&gt; respectively.&lt;br /&gt;&lt;br /&gt;If you have two objects, and you wish to test their equality, the &lt;code&gt;equals&lt;/code&gt; method is the obvious choice. The default implementation in java is simply to check if the references are the same, i.e. the default implementation of this:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;objA.equals(objB)&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;is the same as this:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;objA == objB&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;However, value objects such as &lt;code&gt;String&lt;/code&gt;, have a different implementation of &lt;code&gt;equals&lt;/code&gt;:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;pre&gt;public boolean equals(Object anObject) {&lt;br /&gt;    if (this == anObject) {&lt;br /&gt;        return true;&lt;br /&gt;    }&lt;br /&gt;    if (anObject instanceof String) {&lt;br /&gt;        String anotherString = (String)anObject;&lt;br /&gt;        int n = count;&lt;br /&gt;        if (n == anotherString.count) {&lt;br /&gt;            char v1[] = value;&lt;br /&gt;            char v2[] = anotherString.value;&lt;br /&gt;            int i = offset;&lt;br /&gt;            int j = anotherString.offset;&lt;br /&gt;            while (n&amp;mdash; != 0) {&lt;br /&gt;                if (v1[i++] != v2[j++])&lt;br /&gt;                    return false;&lt;br /&gt;            }&lt;br /&gt;            return true;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    return false;&lt;br /&gt;}&lt;/pre&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;This implementation actually checks the value of another string that is passed to it, and will return true if the type and the value match.&lt;br /&gt;&lt;br /&gt;Seeing this implementation, I postulate the following: &lt;strong&gt;All objects that represent a value should override &lt;code&gt;equals&lt;/code&gt; and by extension &lt;code&gt;hashcode&lt;/code&gt;&lt;/strong&gt;.&lt;br /&gt;&lt;br /&gt;You might argue that this is pointless if you aren&amp;rsquo;t performing comparisons between these objects of this type. What is the point of writing and maintain code that never gets executed at any time within your application? Well, there are a few reasons.&lt;br /&gt;&lt;br /&gt;Firstly, utilities such as java&amp;rsquo;s &lt;code&gt;Collections&lt;/code&gt; classes use &lt;code&gt;equals&lt;/code&gt; and &lt;code&gt;hashcode&lt;/code&gt; to manage their contents and to spot duplicates. Since the default behaviour is assume that different instances are not equal, it is possible, for example, to have multiple instances of the same value object in a hashmap key set. Bugs like this are subtle and can prove difficult to spot. Often they might be missed during unit testing, because it is possible that the scenario where multiple instances with the same value may not have been considered.&lt;br /&gt;&lt;br /&gt;Another reason to properly override &lt;code&gt;equals&lt;/code&gt; regularly is that it aids testability. If you are using matchers for testing the following line will be a common one in your tests:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;assertThat(objA, equalTo(objB));&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;If you are using the default implementation of &lt;code&gt;equals&lt;/code&gt;, then what that line does is check that &lt;code&gt;objA&lt;/code&gt; and &lt;code&gt;objB&lt;/code&gt; are the same instance. That would mean that the above assertion would be equivalent to the following if you have not overridden &lt;code&gt;equals&lt;/code&gt;.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;assertThat(objA, same(objB));&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;This is clearly not the case. The intent expressed by &lt;code&gt;equalTo&lt;/code&gt; and &lt;code&gt;same&lt;/code&gt; is quite definitely different.&lt;br /&gt;&lt;br /&gt;A third reason to implement &lt;code&gt;equals&lt;/code&gt; and &lt;code&gt;hashcode&lt;/code&gt; on your classes is that if you are producing code to be used by another group of individuals or any sort of API, you have no idea how those individuals intend to use your objects. Even if you aren&amp;rsquo;t handling them in collections, they quite easily might.&lt;br /&gt;&lt;br /&gt;However, as I alluded to earlier, there are reasons not to implement &lt;code&gt;equals&lt;/code&gt; and &lt;code&gt;hashcode&lt;/code&gt; on classes. The first that this is a lot of code to implement, test and maintain on many classes. Having said this, I would argue implementing them is still the right thing to do. There are a few cases where implementing these methods just really doesn&amp;rsquo;t make sense. That would be on things like static utility classes which never have objects instantiated. Another example might be on objects where the instance itself can be considered the value, for example a node in a representation of a tree.&lt;/p&gt;
</content>
  </entry>
</feed>
