Geeks With Blogs
.net alternatives by Michel Grootjans
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 ;-)



Posted on Sunday, August 9, 2009 5:43 PM .net , java | Back to top


Comments on this post: java vs .net: the language

# re: java vs .net: the language
Requesting Gravatar...
Yes, you sound wry, and rightfully so. Java sucks and C# these days is simply much better. I personally don't care for MS or .Net at all, but as far as the language goes there's unfortunately no contest anymore. I think most of 'us' on the Java side of things will agree to that. Luckily, there are other languages on the JVM, but MS has a great advantage in a good 'default' language, while the rest of us I guess are waiting for Scala (etc.) to pick up.
Left by Erik Post on Sep 02, 2009 3:01 PM

# re: java vs .net: the language
Requesting Gravatar...
You should try Groovy instead of Java.

As you'll see, it's almost like a Java++

Left by Henrique Lobo Weissmann on Sep 02, 2009 4:39 PM

# re: java vs .net: the language
Requesting Gravatar...
thanks for the post. good one. I am fresher which will be the best for me. java or .net
Left by SUNILKUMAR on Nov 06, 2009 10:54 AM

Your comment:
 (will show your gravatar)


Copyright © Michel Grootjans | Powered by: GeeksWithBlogs.net