Jump to content

Comparison of C Sharp and Java

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Oldadamml (talk | contribs) at 06:28, 17 August 2005 (speed comparison). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

This is a comparison of the C# programming language to the Java programming language. As two modern garbage-collected runtime-compiled languages derived from C and C++, Java and C# are very similar. This page documents the strong general similarities of the languages and then points out those instances where the languages diverge. Although the page is organized into advantages and disadvantages, both languages were designed carefully, and if one language has a feature another lacks it is the result of a conscientious design decision. Thus, the reader is advised to avoid the temptation to 'keep score,' and instead think about why the designers made each decision.

Similarities

  • The syntax of both languages is similar to C++, 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-based for 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# implements properties as part of the language syntax. This is an improvement on code readability and simplicity as compared with Java and C++ where separate get and set methods have to be used.
  • C# allows switch statements to operate on strings.
  • C# has support for output parameters, aiding in the return of multiple values. This is familiar to C/C++ developers, as well as SQL database developers.
  • C# has the ability to alias namespaces, which can be used to create more readable code (if not abused).
  • C# allows a class to specifically implement methods of an interface, separate to its own class methods. This allows it also to implement two different interfaces which happen to have a method of the same name. The methods of an interface do not need to be "public"; they can be made to be accessible only via that interface.

Disadvantages of C#

  • No support for generics (will be implemented in the upcoming release of C# 2.0)
  • 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, C# is associated firmly with the proprietary .NET framework, which has patchy (or non-existent) support away from Microsoft operating systems.
  • The .NET run-time allows both managed and unmanaged code, enabling certain classes of bugs that do not exist in a pure managed code environment.

Advantages of Java

  • Large and highly active user base. Java has also become a lingua franca in many modern branches of computer science, particularly areas which involve networking.
  • Java dominates programming courses at high school and college level in the West. There are currently many more Java than 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 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 representation, 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

  • The lack of any call by reference for primitive and 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, exist).
  • 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] [2].
  • 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 complicate the method invokation model, because they are handled through reflection, which is generally slow.
  • 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. Multi-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.
  • Java does not include operator overloading, because operator overloading can lead to code that is harder to understand and debug. C# allows operator overloading, which, when used carefully, can make code terser and more readable. Java's lack of overloading makes it somewhat unsuited for certain mathematical programs.

Speed comparison in Java SE 5.0 and .NET 2003

C# is a bit faster then Java and significant faster in nested loops (like matrix multiplication). Also, GUI might be much faster in C# then in Java. Performance comparison C++, C# and Java LZMA implementation speed comparison

See also