I'd like to compare the java language to .net as I'm experiencing it. First of all, I'd like to point out that the differences are less than the similarities. Purely seen from a syntax point there really are no differences. Except of course if you're used to .net 3.5. This is irritating at first, but after a while you're just resigned to it. Take the simple properties we're so used to in .net
Compare these two identical pieces of code:
java:
private int counter;
public int getCounter(){
return counter;
}
public void setCounter(int value){
counter = value;
}
.net:
public Counter{get; set;}
I know we didn't have them before .net 3.5, and I realize the dangers of overexposing members, but I love the conciseness of automatic properties. For 7 properties I have 7 lines of code in .net and 70 lines of code in java.
Other things you don't get in java are anonymous methods, so that's no lambdas or LINQ either. No extension methods, so bye bye to simple readability like
1.February(2005). Access modifiers are slightly different but that's just something you accept and move in. All in all it's like having to go back to .net 2.0, which a lot of us have to do from time to time.
However, the thing that the .net platform really nailed, compared to java, is generics. I'm going to illustrate this with an example.
.net:
public T Get<T>(int id)
{
return session.Get<T>(id);
}
This is just not possible in java. For example, you cannot ask the type of T at runtime like this
typeof(T)). Generics are just a little syntactical sugar at design time and as a compiler check.
In java, your code will look like this:
<T> public T get(Class<T> clazz, int id){
return (T) session.get(clazz, id);
}
Which makes every caller look like this:
repository.get(Person.class, id);
The cast to T might even fail at runtime if the type returned by
session is not of T.
Primitive types are also something to get used to. In .net, you can easily do
1.ToString(). No such luck in java.
new Integer(1).toString() is the way to go, since
1 is not an object. To me, java seems a more technical language where it is harder to express
intent without giving in too much into technical implementation.
I might sound a little whiny in this post, but all things taken into account, I enjoy this journey into a new, albeit a similar language. I can't wait to get an offer to try Ruby on Rails ;-)