default(T) in C# and why Java does not have something similar

When you use generics ,your classes or functions behave like templates. We don’t call them templates in C#. In C++ we call them templates but in C# and Java we simply call them Generics but let’s face it, they are templates… in the true meaning of templates. I say that because they serve as templates for the compiler. The compiler uses the code for generics to actually compile the strong typed version of the class.

The whole argument of whether they are templates or not is probably something up for academic debate and that’s besides the point in regards to intent of this post.

Recently I was trying to create a data structure that used a doubly linked list. I created the Node internally. The structure required a Node that had an attribute named data of type T. The structure had a removeFront method. The method was similar to a pop or dequeue. It required the method to find the first item and remove it from the list and return its value.

However, since it removes a node from the list what should the return value be when the list is empty. Let’s acknowledge that most data structures such as queues and stacks normally throw an exception when removing from an empty list (e.g. such as in pop or dequeue). However, let’s say that we, instead of throwing an exception, want to return value of type T.

C# happens to have a reserved key word for this; default(T). In that case if the value is a primitive such as an integer it will return 0. If the value is a reference class, it will return null. But… my code was in Java and Java does not have default(T).

public T removeFront(){

   if(size > 0){
      T temp = head.data;
      //do something to remove the node and advance the head
      return temp;
   }
   
   //what do we return here for T
   throw new Exception("Removing from empty list is not allowed");
   //Can't do the following because that's not valid
   //T result;
   //return result;
   //or
   //return T
}

Well, to be honest, I had to look up the answer to this problem. In the process of finding the answer, it made perfect sense. I don’t know why I didn’t see it before. All you have to do is return null. null is and should be the default value to all generics in Java.

Wait… what if it’s a primitive like an int or a double? Well… the reality is that in Java, generics cannot be created against primitives… only reference types. All reference types can be set to null. But what about generics agains int and double. Java has the class version of each of those such as Integer and Double. That means that even in cases where you want to use primitives you have to use the class version of those and then Java uses boxing and unboxing to handle the conversion between the reference type and the primitive type.

public T removeFront(){

   if(size > 0){
      T temp = head.data;
      //do something to remove the node and advance the head
      return temp;
   }
   
   return null;
}

I can’t believe I was stuck on this problem for as long as I was. I was stuck on the idea that the return could be 0, or Null depending on the data type of T. The return will always be valid as null. The solution is so simple but I was so focused on .Net’s solution that I forgot to consider that Java is not .Net and Java does not have the capability of making generics with primitives.

2 thoughts on “default(T) in C# and why Java does not have something similar

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: