-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|512|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
SoCoder -> Article Home -> Advanced Techniques


 
Scherererer
Created : 15 September 2006
Edited : 07 January 2007

Java vs C#

Compares the C# and Java programming languages

C# vs Java

Overview

Ok, quick overview/reference for you to look at
C#Java
Full OOP SupportYY
Method OverloadingYY
Operator OverloadingYN
GenericsYY
Embeddable AppletsNY
Multi-platformNY
Pointer SupportYN

(list will be added to as I remember things)

Some finer points

Both java and C# run on a virtual machine. They are both quite stable, and I can almost gurantee you that either one will never crash your computer. The most I have seen is the program shutting down, but it shuts down cleanly without damaging the computer's resource pool. C# is much faster than Java; however, there is a reason for this. The .NET framework that C# runs on top of is closely integrated into windows, and therefore it can run quickly. Conversely, Java was designed to be multi-platform; because of this, its bytecodes are very generic and take longer to interpret for the individual machine that it is running on.

Java has the ability to make applets which are little programs that you can run embedded in your browser. Any computer or browser can view and execute an applet as long as they have the JVM installed on their computer. C#, however, is unable to do this. They (microsoft) have created ASP.Net (ASP is similar in nature to PHP, but is microsoft-based) and so therefore .NET has lot of web-integration, so you can still do a lot of cool web-related stuff, but you can't just place a program in the browser.

Both have a good documentation database for all of the built-in methods and classes. For .NET, one can visit MSDN. And for Java, you can get your documentation from Sun Microsystems (Java). This can be very useful for beginners. I have found the MSDN has been more useful for finding out how to do a concept, as in if I was trying to find out how to do a specific task; conversely Sun Microsystems has a system that makes it very easy to find detailed documentation for a class that you already know about.

Java also has a good strong self-documentation system called JavaDoc. Although C# has a similar system built into Visual Studio, JavaDoc generates documentation in a clean HTML-based interface so that it is easily shared with others.

What's nice about C# is that you don't have to just program in C#. Because of the way that .Net is designed, you technically can write in multiple languages (C#, VB, J#, Managed C++) all in the same program and make calls to each code document. So, for instance, VB has better support for databasing while I find it easier to do network code in C#. So I could create VB code for my database classes, and then turn around and make calls to it from my C# code. Additionally, .NET is able to call methods from the JVM. That means that if you find a particular method to be better in the Java Virtual Machine than from the .NET framework (an example of which being the Zip file support in the JVM as opposed to the use of the shell in .NET), you can add the JVM as a reference to a .NET program and make use of the classes in there.

In java, the file that you store the source code must be the same name as each class that you define. So, if you made a class Number it would have to be in Number.java. Because of this, you can't have several classes defined in the same source file. C# takes a different approach, so i can define a class Number and a class Digit all in the same file, and it doesn't even have to be named similar, it could be named Stuff.cs. There are pros and cons to each approach. A pro for the Java approach, is that classes are kept separate and it is easy to transport a single class from one project to another. A con for Java/pro for C# is that if I had a number of small classes or interfaces that would be a waste of space to put in different files, they could all be added into the same file. Technically, Java adheres to a stricter standard in this reguard, and so it is in my opinion a bit better, but that doesn't mean you can't name your C# source files to be the same as the classes they define.

Both of the languages have the ability to import groups of classes. In java, its called a package, while in C# its called a namespace. To retrieve them, in java you use the import statement, and in C# you would use the using keyword. The both of these keywords replace the compiler directive #include that was needed for C and C++.

Accessors

Because it is a standard convention in OOP to keep methods and fields private as possible, methods called accessors were invented to access those fields. In Java, accessors are done by hand, for instance, here is a field and accessor definition for a field called variable:

To access the variable from another class, we would make calls like this (Assuming we've defined an object myInstance that is an object of the class we created the accessor for):


However, in C#, there is built-in accessor support. These accessors are called "properties". Here is an example of a definition of a field and its accessors:

Making the same assumptions we did in the Java code, here is a demonstration of the use of the property:

As you can see, the property is treated as a variable, however it allows for a barrier between the other class and the actual variable.

Style

Since the syntax is just about identical in the two languages, I figured I'd go over some of the differences in the style conventions.
Similarities
-Classes are to be defined with a capitol letter starting the class name.
-Variables are named like nouns
-Classes are named like nouns
-Methods are named like verbs
-Interfaces are generally named with the -able suffix, or more accurately as an "able" word (Collidable, Enumerable, Comparable, Convertable, etc.)
Differences
-Methods in C# start with a capital letter, while they start lower-case in Java
-Interface names in C# have the "I" prefix, while they are named just like normal classes in Java

Games

Ok, a lot of the people here like to make games, and its also an important thing to cover. Currently, a lot of major games are programmed in C++. However, there are options coming out for C# and for Java. Since they're reletively new languages, it will be a while for it all to catch on, but here's what we've got. XNA is Microsoft's new game engine for C#. Its a bit more optimized for games and supports making games for the XBOX 360. On the flip side, there is something called the JMonkey engine for Java. This is not made by Sun, however its a decent engine. There is also Managed DirectX for C#, which is an OOP-version of DirectX.

Other Helpful Resources

C#:
www.msdn.com - MSDN (MicroSoft Developer Network)
www.thecodeproject.com/ - The Code Project
msdn.microsoft.com/coding4fun/ - Coding4Fun
msdn.microsoft.com/directx/XNA/default.aspx - XNA Game Development
Java:
java.sun.com - Sun Microsystems
www.jmonkeyengine.com/ - JMonkey Engine
For a laugh
www.thecodeproject.com/script/survey/detail.asp?survey=577 - No one uses J#

If you have any other links that you'd like me to add to the list, just send me a message/post a comment and i'll add it.

 

Comments


Friday, 15 September 2006, 20:55
magicman
personaly, i prefer java, but i havent really done much with it and i havent done anything with c# or c++. However, i have heard that in the future java will be used for creative programs, like games and the like, and c# will be used for apps and that sort of thing. and i didnt just read that on some blog, but i heard it from a freind who works in the tech world.
Thursday, 04 January 2007, 03:19
JL235
To be honest this doesn't really tell me much about either language, and more importantly their differences. Their are some minor points mentioned like how Java and C# allow you to store classes in files differently, but this is minor. I'd also be suprised if this wasn't a deliberate design decision for Java as it is a very strict object-oriented language.

I disagree with the comparison between Sun Microsystems help and MSDN. I find the Java API fast and easy to use, all the classes which come with Java are there listed in their catorgories, it's very easy to read and the best thing I like is that it is so fast. For me MSDN has always seemed very bloated and navigation confusing and cumbersome whenever I have used it. It's not that is has lots of redundant information, but it could be organised a lot better. You can also easily make a local copy of the Java API, which I've seen encouraged to help with using Java.

Secondly it would help if you explained these points further. For example why do you think MSDN is superior? Even then are you comparing it to Java doc or Sun Microsystems help in general from their main site?
Friday, 05 January 2007, 09:19
Scherererer
New updates! I'll continue to update further when I get another speck of time.
Thursday, 01 March 2007, 02:17
JL235
This is a lot better then it was before, and has quite a lot of stuff I didn't know already. I like the C# style of get and set methods too, it's a bit similar to Ruby.

You might want to add the growing support for multiple languages on the Java Virtual Machine, which is being funded by Sun. This support is going far enough to allow the class of one language to be a super or sub-class of a class of a different language.

I think you should also put more in on the vast Java library, such as the AWT and Swing packages which can be used to draw simple 2d graphics. As well as any C# equivalent.

The Java virtual machine is also one of the most ported interpreters. Everything supports it, even mobile phones, and some devices (such as Sun SPOTs) run it natively (although Sun SPOT actually runs Java ME).
Thursday, 01 March 2007, 06:52
garand
games_section_of_this_article Ok, a lot of the people here like to make games, and its also an important thing to cover. Currently, a lot of major games are programmed in C++. However, there are options coming out for C# and for Java. Since they're reletively new languages, it will be a while for it all to catch on, but here's what we've got. XNA is Microsoft's new game engine for C#. Its a bit more optimized for games and supports making games for the XBOX 360. On the flip side, there is something called the JMonkey engine for Java. This is not made by Sun, however its a decent engine. There is also Managed DirectX for C#, which is an OOP-version of DirectX.

Does this mean that C++ is going to be used less and less by game companies now?
Thursday, 01 March 2007, 07:19
JL235
I've gotten the impression for a long time that no real piece of software is written in C++, just the slow bits. At least that is what I've heard multiple times from many real programmers (such as one guy helped to write and designed and write C#).
Friday, 02 March 2007, 16:44
Yayyak
The greater part of most versions of Windows is C++, with the odd bit of assembly. Actually, most Microsoft code is C++.
Saturday, 03 March 2007, 00:27
JL235
Crash Bandicoot was written in a form of Lisp, except for the graphics. There is currently a big move to write the majority of a game in a higher level language, because it has faster development time with less debugging time. The loss in performance is also usually only minor.