Comparison of C Sharp to Java
|
- The title of this article is incorrect because of technical limitations. The correct title is Comparison of C# to Java.
This is a comparison of the C# programming language to the Java programming language.
Contents |
Similarities
- Both languages were derived from C++ (at least in their syntax), which was in turn derived from C.
- Both languages were designed to be object oriented from the ground up; unlike C++, they were not designed to be compatible with C.
- Both languages rely on a virtual machine.
- Both languages include synchronization.
- Both languages support generic programming.
- Both include garbage collection.
- Both include boxing and unboxing of value types, allowing numbers to be handled as objects.
- Both include
foreach
, an enhanced iterator-basedfor
loop. - Both include a large class library, which provides APIs for performing many common programming tasks.
- Both the Java VM and the .NET platform optimize code at runtime through just-in-time compilation (JIT).
Advantages of C#
- Tied more closely to the Windows operating system, making for better performance in OS-specific tasks such as user interfaces. C# also provides close integration with COM.
- The .NET framework was designed to support the execution of many different languages using the same bytecode (MSIL) and virtual machine. While this is possible with Java, the design of .NET allows for better cross-language compatibility.
- Better support for arithmetic by including more primitive types and functionality to catch arithmetic exceptions.
- Includes a large number of notational conveniences over Java, many of which, such as operator overloading and user-defined casts, are already familiar to the large community of C++ programmers.
- C# is defined by ECMA and ISO standards, whereas Java is proprietary (although largely controlled through an open community process.)
- Allows the definition of "structs", which are similar to classes but may be allocated on the stack (unlike instances of classes in C# and Java). This can improve performance in some situations.
- C# has properties. This is an improvement on code readability and simplicity as compaired with Java and C++ where separate get and set methods have to be used.
- C# allows switch statements to operate on strings.
- C# has autoboxing of primitives, which is present in Java 1.5/5.0 but not in earlier versions.
- C# has the ability to alias namespaces, which can be used to create more readable code (if not abused).
Disadvantages of C#
- No support for generics (present in Java 1.5/5.0, will be implemented in the upcoming release of C#)
- Not as ubiquitous as Java across disparate operating systems and environments. Currently no real market presence outside of desktop and server environments (ie. embedded or mobile computing.)
- Although not a critisism of the language itself, associated firmly with the proprietary .Net framework, which has patchy (or non-existent) support away from Microsoft operating systems.
Advantages of Java
- Enormous and highly active user base. Java has also become a lingua franca in many modern branches of computer science, particularly areas which involve networking.
- For better or worse, Java now dominates programming courses at high school and college level in the West. At the time of writing, the number of new Java books still continue to outgrow C# books.
- Already implemented on a larger number of platforms, making for better cross-platform compatibility.
- More mature, meaning a lot of problems may have already been worked out, and more code is already widely available.
- Numourous JVM implementations exist, some under open source licensing.
- Java's strictfp keyword can be used to guarantee that the results of floating point operations remain the same across platforms.
- Java's method invocation system is simpler than C#'s because it lacks support for delegates.
- Java Webstart and Java applets provide convenient, lightweight and secure means of distributing an application to the desktop.
- Its maturity and popularity have ensured more third party Java API's and libraries (many of them open source) than C#.
- The efficiency of its Bytecode implementation, coupled with agressive Java-specific compression technologies such as pack200, makes Java a very bandwidth friendly means of distributing applications over a network.
Disadvantages of Java
- Does not include operator overloading -- although this is a done for a reason. Some people consider operator overloading bad, because it leads to code that is harder to understand and debug
- The lack of any call by reference for primitive (or reference) types makes certain procedural programming tasks awkward.
- The Java Language Specification is a proprietary Sun product (however other Java VMs, both free/open source and closed source, exists).
- No support for namespace aliasing.
- Although not a critisism of the language itself, Java's standard API's are somewhat weak when writing applications targeted at desktop users.
Differences between the languages
- There are no unsigned primitive numeric types in Java. While it is universally agreed that mixing signed and unsigned variables in code is bad, Java's lack of support for unsigned numeric types makes it somewhat unsuited for low-level programming.
- C# does not include checked exceptions. Some would argue that checked exceptions are very helpful for good programming practice. Others, including Anders Hejlsberg, chief C# language architect, argue that they were to some extent an experiment in Java and that they haven't been shown to be worthwhile [1] (http://www.artima.com/intv/handcuffs.html) [2] (http://msdn.microsoft.com/vcsharp/team/language/ask/exceptionspecs/default.aspx).
- C#'s namespaces are more similar to those in C++. Unlike Java, the namespace does not specify the location of the source file. (Actually, it's not strictly necessary for a Java source file location to mirror its package directory structure.)
- C# includes delegates, whereas Java does not. Some argue that delegates are extremely useful, but others point out that their inclusion complicates the method invocation model.
- Java requires that a source file name must match the only public class inside it, while C# allows multiple public classes in the same file.
- C# allows the use of pointers, which some language designers consider to be unsafe, but certain language features try to ensure this functionality is not misused accidentally. Pointers also greatly complicate technologies such as Java's RMI (Remote Method Invocation), where program objects resident on one computer can be referenced within a program running on an entirely separate computer. Some have speculated that the lack of memory pointers in Java (substituted by the more abstract notion of object references) was a nod towards the coming of grid computing, where a single application may be distributed across many physical pieces of hardware.
- C# supports the goto keyword. This can occasionally be useful, but the use of a more structured method of control flow is usually recommended.
- C# has true multi-dimensional arrays, as well as the array-of-arrays that is available to Java. Mutli-dimensional arrays are always rectangular (in the 2D case, or analogous for more dimensions), whereas an array-of-arrays may store arrays of various length.
See also
External links
- A Comparison of Microsoft's C# Programming Language to Sun Microsystems' Java Programming Language (http://www.csse.monash.edu.au/courseware/cse1203/Resources/CsharpVJava.htm)
- C#: A language alternative or just J--? (http://www.javaworld.com/javaworld/jw-11-2000/jw-1122-csharp1_p.html)
- Contrasting C# and Java Syntax (http://www.ondotnet.com/pub/a/dotnet/2001/06/14/csharp_4_java.html)
- Java vs. C# - Code for Code Comparison (http://www.javacamp.org/javavscsharp/)
- Nine Language Performance Round-up (http://www.osnews.com/story.php?news_id=5602)
- Java Was Strongly Influenced by Objective-C ... and not C++ (http://virtualschool.edu/objectivec/influenceOnJava.html)
- Java and C-Sharp Compared (http://www.csharphelp.com/archives/archive96.html)