Why Java makes me like C# even more

I recently bought an android phone.

Which means I *MUST* code for it.

It is not a choice.

It is practically a biological imperative.

I can’t help it.

So, the choice is to port HamQuest.

I want to play HamQuest on my phone.

I’ll even slap some admob stuff onto it and put it up in the market for free.

I started porting, beginning with the classes dealing with boards and mazes upon which everything is based.

I use a lot of generics.

After all, a “board” to me is not simply a rectangular 2d grid.

It can also be an isometric grid or a hex grid.

I use the same classes for each, differing only by a class that describes directions.

Generics work in a similar, but not identical way in Java and C#.

For example, the following class declaration in C#:

public class MazeBase<DirectionsType,PortalType,CellInfoType>:Board<MazeCellBase<DirectionsType,PortalType,CellInfoType>>
where DirectionsType:DirectionsBase,new()
where PortalType:MazePortalBase,new()
where CellInfoType:MazeCellInfoBase,new()

Is, near as I can tell, most closely made equivalent with the following Java:

public class MazeBase<S extends IDirection<S>,  T extends IMazePortal<T>,U extends IBoardCellContents<U>> extends Board<MazeCell<S,T,U>>

So, S = DirectionsType, T = PortalType, U = CellInfoType, and the constructs for doing the generics are roughly similar.

However, the availability of the “new()” constraint in C# and the lack of it in Java means I have to solve similar problems with different techniques.

In C#, my MazeBase class can instantiate a DirectionsType directly.

In Java, my MazeBase class cannot instantiate an S class. Not allowed.

So, I have to pass a List<S> into MazeBase’s constructor in Java, so I can have a list of them to use to construct the maze.

Similarly, since I cannot instantiate T or U, I need to pass template instances that get duplicated.

Also, I wish to complain about org.w3c.dom.*

I have been using the .NET namespace System.Xml.Linq for quite a while now.

It is so simple and easy to use that I feel the Xml parsing is barbaric by comparison.

Also, no way to overload the array operator in Java. Boo.

There are other things, more minor, like how I keep a List<T> variable, but since List<T> is not instantiable, I need to create an ArrayList<T>.

I don’t care how the list is implemented.

I just want an object that acts like an array that I can add new items to, delete items from, and change items in.

If I wanted to care about implementation details for containers, I’d still be using C++.


One Response to “Why Java makes me like C# even more”

  1. Ear Bleeding Java Generics « PlayDeez Games’s Weblog Says:

    […] my previous post, I blather on about my adjustment to Java from […]

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: