What C# does better than Java

I spend 90% of the development time at my job using either Java or C# .NET, with a roughly equal split between the two. So I know a fair amount about both languages — enough to feel qualified to comment on the differences between them, anyway. Now being a Free Software guy, my obvious preference is for Java, which is actually Free as in freedom (finally, anyway) and runs on a large variety of platforms. Given the choice of which to use on my personal projects, Java is a no-brainer. The best IDE for Java, Eclipse, is absolutely Free. The best IDE for C#, Visual Studio is … well, it’s several hundred dollars and proprietary to boot. And it has the limitation of not running on or compiling for GNU/Linux; since I use Ubuntu as my home desktop operating system, that’s a deal breaker.

But just on a pure comparison between the languages, I have to say that C# is the better of the two. It’s not a fair comparison because C# is many years younger and was able to learn from all of Java’s mistake, but then again, that old canard about life not being fair still holds true. C# is the better language. It has lots of features that simply make it more pleasant to code in. One feature I would’ve killed for in Java while writing a recent project at work is properties. Here’s a sample of the code I wrote in Java:

writeOut(data.getAccount().getContract().getAddress().getAddress1());
writeOut(data.getAccount().getContract().getAddress().getAddress2());
writeOut(data.getAccount().getContract().getAddress().getCity());
writeOut(data.getAccount().getContract().getAddress().getZipCode());
writeOut(data.getAccount().getClient().getCoSigner().getFullName());

And it went on and on for dozens of lines; you get the drift. This is getter and parentheses overload. There’s no real reason the code has to be this messy. And with C#, it isn’t. Here’s how the same code would look in C#:

writeOut(data.Account.Contract.Address.Address1);
writeOut(data.Account.Contract.Address.Address2);
writeOut(data.Account.Contract.Address.City);
writeOut(data.Account.Contract.Address.ZipCode);
writeOut(data.Account.Client.CoSigner.FullName);

And yes, you could accomplish the latter in Java by making all member variables public, but that’s a bad idea. In C# you don’t have to make all of the member variables public to do this — you simply define them as properties, which allows for fine-grained control on who can get and set each property, and without all of the messiness of having to corral dozens of different getter and setter functions for each member variable.

So if nothing else mattered, I would recommend and use C# to the exclusion of Java. But since other issues matter a lot more than programming conveniences, like software freedom, I do still recommend and use Java to the exclusion of C#. But Microsoft did put up a good effort.

21 Responses to “What C# does better than Java”

  1. Knacker Says:

    Why not:

    Address ad =data.getAccount().getContract().getAddress();
    writeOut(ad.getAddress1());
    writeOut(ad.getAddress2());
    writeOut(ad.getCity());

    etc?

    I’m sorry I don’t work writing code.

  2. Cyde Weys Says:

    Knacker: That is the way I wrote it in the actual code. This was just an example. But keep in mind there are dozens more fields of interest that I didn’t use for the example, which are scattered all throughout the account tree. Even when you try to make it prettier, there’s still an overload of parentheses and “get”s.

  3. drinian Says:

    C# is a better language than Java, although Java 5 and 6 have fixed a lot of things. However, they both seem to fall into the “pretty good” realm of syntax these days: not perfect, but not bad for a common business-oriented language. Better than that last COBOL, anyway.

    But to me the huge thing that Java has going for it is a huge number of FOSS libraries; even looking at Apache’s contributions alone it’s amazing. I find myself writing a lot more code on C# projects that is tangential to the goal of my application (parsers, string checkers, file handlers, etc.) because things like the Apache Commons libraries aren’t there.

    And I think that’s why Java is still a better choice for development where you don’t need to be tightly coupled to Windows as a platform.

  4. Knacker Says:

    Oh ok. I thought that was production code.

    I’m not a defender of Java or anything, my language of choice actually is C#, though I’m learning and liking Objective-C.

  5. apotheon Says:

    Cyde Weys:

    My first thought upon seeing all that code was “Holy mother of God, my eyes!”

    My second was “This looks like a job for Recursion Man!”

    My third was “I sure am glad tail call optimization will be available with Ruby 2.0, unless they rip it out of 1.9 before then for some inexplicable reason.”

    By the way, on the subject of Java being “Free” — it’s:

    1. not free enough for me by licensing, but yeah, I’d rather use it than C# for the same reasons you would

    2. not free enough for me by availability of components, unless something has changed since the last time I looked (since, last I checked, you couldn’t build a complete Java development environment without agreeing to some damned non-open-source license or other somewhere along the way)

    Oh, yeah, and I also agree that C# appears to be a better designed language than Java. Then again, my experience with both is surely less extensive than your own since I’ve never really used either in “the real world”.

    drinian:

    I find myself writing a lot more code on C# projects that is tangential to the goal of my application (parsers, string checkers, file handlers, etc.) because things like the Apache Commons libraries aren’t there.

    Of course they aren’t. You’re meant to use IIS with C#, instead.

    Knacker:

    I’m learning and liking Objective-C.

    It’s a better designed language than C++, Java, and C# put together. I’m not surprised you like it.

  6. Cyde Weys Says:

    Knacker: Nope, you’re not going to see any production code. That might get me in a bit of trouble. The very least that I do is change some names and mangle things around a bit, so as to not give anything away from the business. Oh, and the choice of code samples. I think it’s pretty safe to say that I’m not giving away anything proprietary by revealing that this company is storing the addresses of their customers.

  7. knacker Says:

    Well, it’s not exactly like anyone could do anything with that if it WAS production code, even done optimally.

  8. Cyde Weys Says:

    Well, you might give away some insight on a data structure or something. It’s definitely possible to give up the ghost of some sort of trade secret in only a few lines of code.

  9. drinian Says:

    Of course they aren’t. You’re meant to use IIS with C#, instead.

    Apache Commons libraries have absolutely nothing to do with the web server.

    If you’re a Java developer and you’re not using them, you really should be.

  10. T2A` Says:

    I heard .NET’s garbage collector is much better than Java’s. But I wouldn’t really know because I don’t use either. However, I do not like Eclipse and its slowness. Visual Studio (I got 2005 Pro from school) is a great piece of software.

  11. Cyde Weys Says:

    T2A`: I actually have the opposite opinion than you. In my experience, Eclipse is faster. It definitely takes less time to start up than Visual Studio 2008 does, and its IntelliSense (or whatever the non-Microsoft variety is called) is a lot snappier than Visual Studio’s. I also really like that it continuously runs background builds. With Visual Studio, there are whole classes of errors that you won’t find out about until you run a Rebuild All, and since doing so can take a long time on a big project, it doesn’t get done a lot more often. If I’m comparing purely IDE against IDE, my preference is definitely for Eclipse over Visual Studio.

  12. drinian Says:

    I can only speak to VS2k5, not 2k8 which apparently has some improvements, but 2k5 did nothing to encourage good programming practices. The code auto-formatter was stashed away under “Edit -> Advanced…” for instance. There were no default compiler nags for sloppy coding like unused imports, either, not to mention that 2k5 couldn’t automatically import namespaces like Eclipse can when using class name autocompletion.

    Eclipse has gotten faster over the last few iterations, and more importantly, seems to make much better choices about sane defaults and developer customization. Not to mention what appears to be a much healthier “ecosystem” of open-source extensions.

    The coupling between specific versions of Java and Eclipse also seems much looser than specific versions of the .NET framework and VS-$year

  13. Cyde Weys Says:

    Yup, I agree with everything Drinian says. The one thing I do appreciate about Visual Studio is its tight integration with writing Windows GUI apps. Creating dialogue boxes is as simple as dragging-and-dropping components (of course, this can lead to all sorts of problems if you don’t know what you’re doing). As far as I’m aware, Eclipse doesn’t do the same kind of thing with Java Swing applications. Not that this matters in our line of work, mind you; we almost never write thick-client apps. In terms of the web apps that we do write, Visual Studio and Eclipse both support WYSIWYG web page editing.

  14. Ed Says:

    I read you guys talk about “style and control” but where is the “damage and aggression”? I mean, which language produces code that runs faster, occupies the least space and integrates better with other already made stuff?

  15. drinian Says:

    If you’re seriously concerned about performance and executable size, you shouldn’t be using anything other than C and maybe C++, but that would be silly.

    Also, WYSIWYG is no way to do Web development for a whole host of reasons. Not that I have a problem with decent Web-focused text editors, but… seriously. Aside from creating unreadable code, the WYG while you’re designing will never be the same as what your viewers get, given the wide range of ways to view the Web.

  16. Jhon Says:

    I was a software systems developer doing loads of legacy – COBOL :-(. I am currentley doing a QA role and would like to get back into a developer role.

    I understand the OO methodology and programming concepts. The problem I am having is which path I should go Java or C#? I was thinkng at first Java – but this space seems like a mess

  17. Jhon Says:

    there are too many vendors using there own API implementations. For example you have hibernate.jar and jboss-hibernate.jar.

    Just want to get back into development with a simple path so do you guys think C# is the way to go?

  18. Cyde Weys Says:

    Jhon: Both are widely used, so it’s hard to recommend one over the other on that basis. In regards to particular applications, however, I kind of feel like Java is used slightly more for web applications, while the .NET languages are used slightly more for thick-client applications. So if you have a preference for one over the other, you may want to focus on that.

    But really, all you need to do is just learn a modern language. The differences between Java and .NET are minuscule compared to the differences between either one of those and COBOL. If you know one of them well, you should be able to pretty easily do work in the other one.

  19. Graphite Says:

    This is pathetic. Next time, try to write something that actually compares real differences. C#, as an exclusive language, is for losers who don’t know unix and want to stay away from it no matter what the cost. Every component I’ve used that has been ported to C# from Java has been sub-par. C# has its place, but it just don’t hold its weight to Java. I’m tired of going back in time every time I want to accomplish a task that has already been done in Java, but just isn’t there in C#. C# is the better language??? How is that when I can’t even do anonymous inner classes? That alone is enough to drive me to the mad house.

  20. Skilly Says:

    You can’t argue “Anonymous Inner Classes”, because then someone else can argue “Closures”. If you want to argue language schematics, and features, Java has a few nifty features, but in count C# has more. Not all C# developers “don’t know unix” I permanently run a few OpenBSD and ArchLinux boxes, all running C# on mono. I wanted to use Java for unix programming, but it’s so far behind modern languages, which if you’re used to, makes java abit too primitive. So I’dd stick to C#/D =)

  21. Akhil Gupta Says:

    In this article, we are just proposing that why developers should opt for .Net Technology instead of other major technologies.
    First of all, what is .Net Technology? – .Net is an environment for building and running software and web applications. It enables the software to become platform and device independent.
    The .net Framework allows you to:
    1)Apply common skills across a variety of devices, application types and programming tasks.
    Computer Systems normally require during execution a lot of interaction between the older and newer applications. Through .Net Framework we can access the functionalities that is implemented in programs that execute outside the .Net environment. Eg: Through .Net we can even access the Microsoft Office Products, Adobe Products etc.It can even use the Windows predefined DLLs. We can even access other Microsoft Components like Paint, Calculator etc.
    Thus, we can say that we have a pretty easy access to the COM Components. COM components usually have an extension – .exe/dll/.ocx. But the extension is mostly irrelevant. In actual, it may refer to any object or application that contains reusable code and provides ways through which its associated data and methods could be accessed.
    2) Has got Language Independence
    Microsoft provides support for a lot of languages like
    a) Microsoft Visual Basic
    b) C#
    c) J#
    d) Microsoft Visual C++
    e) Javascript
    f) HTML
    A programmer who is coding in C# can use the code written in Visual Basic. Similarly, a programmer who is coding in J# can use the code written in C#.
    3) Simplified Deployment:
    .Net framework has a very simplified deployment wizard so that the installation of the computer software could be managed easily.
    4) Security:
    .Net applications are highly secure ones. The framework has been designed in such a way so as to address small vulnerabilities such as buffer overflows.
    5) Faster development:
    Through .Net we can make compelling applications much faster.
    6) Base Class Library:
    This is a library of functionalities available to the .Net Framework. This provides classes which consists of a number of common functions like – file reading/writing, database interaction, XML data manipulation etc.
    7) Portability:
    The .Net framework is portable. Its platform independent. Though till now, Microsoft has not implemented the full framework on any other system except its own. Yet Microsoft has already submitted specifications for Common Language Infrastructure (which includes all class libraries), C# language to both ECMA and ISO making them open standard. This makes it easily possible for the third parties to create compatible implementations of framework and its language on other platforms.
    The latest version of the .Net Framework is 4.0. A lot of the professionals are still working on .Net Framework 3.5 and 3.0.
    Last but not the least, we come to the much heated discussion. What’s better ? .Net / JAVA.
    There are a number of reasons why .Net is much better than JAVA.
    1) .Net, first and foremost is a very fast development tool. Applications that would take 1 month in .Net framework would take around 3 months in JAVA.
    2) .Net, being a very fast development tool is much more cheaper than JAVA.
    3) JAVA in itself is a language. Here, you can do only coding in JAVA. But .Net is a development platform where you can use the coding languages according to your convenience. You have the options of using Visual C++, C#, J# or Visual Basic, whichever you feel most comfortable with.
    4) JAVA GUI based programs look alien on the host operating systems. .Net’s GUI is much better compared to JAVA’s.
    But still there are some disadvantages of the .Net Framework which as a developer we must keep in mind:
    1) .Net Framework runs application in a managed environment which usually exhausts a lot of resources. This in comparison to other applications that utilize machine resources directly is much more.
    2) Newer versions of .Net framework are only available on Windows 7/ Vista and above. if you need to make them work on OS like Windows XP, the you need to make some extra efforts. In the future more frameworks would evolve. With their evolution the support for the lower versions of the OS would not be practically possible. So, the user with the older OS would have to face lot of problems.
    3) As we had already discussed, .Net Framework’s implementations has been made open standard but yet its has fully been implemented only on Windows. So, .Net Framework remains platform independent just on paper. Practically, its just possible to run .net Application on Windows OS as no other OS has yet implemented .Net framework for themselves.
    We have discussed a lot on .Net Framework, its advantages and disadvantages, its features. Now, we must move on to the real programming stuff. So, let’s get started with .Net Programming. Let us just write a simple program for adding two numbers and then showing the result in a message box.
    For further discussions and queries, you may visit:
    http://www.visiontechno.net/studymats/firstprog.html
    In this article, we are just proposing that why developers should opt for .Net Technology instead of other major technologies.

    First of all, what is .Net Technology? – .Net is an environment for building and running software and web applications. It enables the software to become platform and device independent.

    The .net Framework allows you to:
    1)Apply common skills across a variety of devices, application types and programming tasks.

    Computer Systems normally require during execution a lot of interaction between the older and newer applications. Through .Net Framework we can access the functionalities that is implemented in programs that execute outside the .Net environment. Eg: Through .Net we can even access the Microsoft Office Products, Adobe Products etc.It can even use the Windows predefined DLLs. We can even access other Microsoft Components like Paint, Calculator etc.

    Thus, we can say that we have a pretty easy access to the COM Components. COM components usually have an extension – .exe/dll/.ocx. But the extension is mostly irrelevant. In actual, it may refer to any object or application that contains reusable code and provides ways through which its associated data and methods could be accessed.

    2) Has got Language Independence

    Microsoft provides support for a lot of languages like

    a) Microsoft Visual Basic
    b) C#
    c) J#
    d) Microsoft Visual C++
    e) Javascript
    f) HTML

    A programmer who is coding in C# can use the code written in Visual Basic. Similarly, a programmer who is coding in J# can use the code written in C#.

    3) Simplified Deployment:

    .Net framework has a very simplified deployment wizard so that the installation of the computer software could be managed easily.

    4) Security:

    .Net applications are highly secure ones. The framework has been designed in such a way so as to address small vulnerabilities such as buffer overflows.

    5) Faster development:

    Through .Net we can make compelling applications much faster.

    6) Base Class Library:

    This is a library of functionalities available to the .Net Framework. This provides classes which consists of a number of common functions like – file reading/writing, database interaction, XML data manipulation etc.

    7) Portability:

    The .Net framework is portable. Its platform independent. Though till now, Microsoft has not implemented the full framework on any other system except its own. Yet Microsoft has already submitted specifications for Common Language Infrastructure (which includes all class libraries), C# language to both ECMA and ISO making them open standard. This makes it easily possible for the third parties to create compatible implementations of framework and its language on other platforms.

    The latest version of the .Net Framework is 4.0. A lot of the professionals are still working on .Net Framework 3.5 and 3.0.

    Last but not the least, we come to the much heated discussion. What’s better ? .Net / JAVA.

    There are a number of reasons why .Net is much better than JAVA.

    1) .Net, first and foremost is a very fast development tool. Applications that would take 1 month in .Net framework would take around 3 months in JAVA.

    2) .Net, being a very fast development tool is much more cheaper than JAVA.

    3) JAVA in itself is a language. Here, you can do only coding in JAVA. But .Net is a development platform where you can use the coding languages according to your convenience. You have the options of using Visual C++, C#, J# or Visual Basic, whichever you feel most comfortable with.

    4) JAVA GUI based programs look alien on the host operating systems. .Net’s GUI is much better compared to JAVA’s.

    But still there are some disadvantages of the .Net Framework which as a developer we must keep in mind:

    1) .Net Framework runs application in a managed environment which usually exhausts a lot of resources. This in comparison to other applications that utilise machine resources directly is much more.

    2) Newer versions of .Net framework are only available on Windows 7/ Vista and above. if you need to make them work on OS like Windows XP, the you need to make some extra efforts. In the future more frameworks would evolve. With their evolution the support for the lower versions of the OS would not be practically possible. So, the user with the older OS would have to face lot of problems.

    3) As we had already discussed, .Net Framework’s implementations has been made open standard but yet it has fully been implemented only on Windows. So, .Net Framework remains platform independent just on paper. Practically, its just possible to run .net Application on Windows OS as no other OS has yet implemented .Net framework for themselves.

    We have discussed a lot on .Net Framework, its advantages and disadvantages, its features. Now, we must move on to the real programming stuff. So, let’s get started with .Net Programming. Let us just write a simple program for adding two numbers and then showing the result in a message box.

    For further discussions and queries, you may visit:

    http://www.visiontechno.net/studymats/firstprog.html