Click here to Skip to main content
12,949,083 members (62,168 online)
Click here to Skip to main content
Add your own
alternative version


34 bookmarked
Posted 11 Jan 2008

Main Differences between C# and Java

, 11 Jan 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
This article discusses the main differences between C# and Java


This article provides a base for fresh C# and Java developers.

Features of C# Absent in Java

  • C# includes more primitive types and the 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.

  • Event handling is a "first class citizen"—it is part of the language itself.

  • 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).

  • C# implements properties as part of the language syntax.

  • C# allows switch statements to operate on strings.

  • C# allows anonymous methods providing closure functionality.

  • C# allows iterator that employs co-routines via a functional-style yield keyword.

  • C# has support for output parameters, aiding in the return of multiple values, a feature shared by C++ and SQL.

  • C# has the ability to alias namespaces.

  • C# has "Explicit Member Implementation" which allows a class to specifically implement methods of an interface, separate from 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.

  • C# provides integration with COM.

  • Following the example of C and C++, C# allows call by reference for primitive and reference types.

Features of Java Absent in C#

  • Java's strictfp keyword guarantees that the result of floating point operations remain the same across platforms.

  • Java supports checked exceptions for better enforcement of error trapping and handling.

Philosophical 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 invocation model, because they are handled through reflection, which is generally slow. On the other hand, they can simplify the code by removing the need to declare new (possibly anonymous) classes to hook to events.

  • 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 (which C# calls jagged arrays). Multi-dimensional arrays are always rectangular (in the 2D case, or analogous for more dimensions), whereas an array-of-arrays may store rows (again in the 2D case) of various lengths. Rectangular arrays may speed access if memory is a bottleneck (there is only one memory reference instead of two; this benefit is very dependent on cache behavior) while jagged arrays save memory if it's not full but cost (at the penalty of one pointer per row) if it is. Rectangular arrays also obviate the need to allocate memory for each row explicitly.

  • Java does not include operator overloading, because abuse of 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. Conversely, .NET's numeric types do not share a common interface or superclass with add/subtract/etc. methods, restricting the flexibility of numerical libraries.

  • Methods in C# are non-virtual by default. In Java however, methods are virtual by default. Virtual methods guarantee that the most overridden method of an object will be called which is determined by the runtime. You always have to keep that in mind when calling or writing any virtual method! If the method is declared as non-virtual, the method to invoke will be determined by the compiler. This is a major difference of philosophy between the designers of the Java and .NET platforms.

  • Java 1.5's generics use type-erasure. Information about the generic types is lost when Java source is compiled to bytecode. .NET 2.0's generics are preserved after compilation due to generics support starting in version 2.0 of the .NET Common Language Runtime, or CLR for short. Java's approach allows Java 1.5 binaries to be run in the 1.4 JRE, at the cost of additional runtime typechecks.

  • C# is defined by ECMA and ISO standards, whereas Java is proprietary, though largely controlled through an open community process.

  • The C# API is completely controlled by Microsoft, whereas the Java API is managed through an open community process.

  • The .NET run-time allows both managed and unmanaged code, enabling certain classes of bugs that do not exist in Java's pure managed code environment but also allows interfacing with existing code.


  • 11th January, 2008: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ghulam Murtaza
Software Developer
Pakistan Pakistan
Want to enhance my capabilities by learning new technologies

You may also be interested in...

Comments and Discussions

SuggestionFurther comparisons Pin
ashok bakthavathsalam17-Jul-15 18:55
memberashok bakthavathsalam17-Jul-15 18:55 
QuestionSome clarification, please Pin
Sam Hobbs9-Dec-13 8:37
memberSam Hobbs9-Dec-13 8:37 
AnswerRe: Some clarification, please Pin
Shlomi Borovitz14-Jan-14 4:31
memberShlomi Borovitz14-Jan-14 4:31 
"I am interested in knowing what primitive types C# has that Java does not."
As notes in the post, C# has unsigned integers, which java lacks. Moreover, C# has the "decimal" type as built-in type.

"Note that stacks are machine-dependent; not all processes have stacks. That might be why stacks are excluded from the Java Virtual Machine that is designed to be portable."
The post is about the languages (C# and Java), and not the runtime (CLR and JVM)... Moreover, Mono proves to be portable to.

"What is the "C# API"? I think it would help to specify it in the terminology that defines it."
I believe he intended "class library"...

"The article says: "C# has support for output parameters" ... "featured shared by C++". I am familiar with C++ but I do not understand what is meant by "output parameters" for C++. I think it would help to clarify what is meant by "output parameters" for C++."
In C++ these are reference parameter:
void Foo(int& i) { ... }

In C# you actually have two types of "output parameters":
void Foo(ref int i) { ... }
Which would be in-out parameter (and must be initialize before calling Foo), and
void Foo(out int i) { ... }
Which would only used as an out parameter (and must be initialized inside Foo).
GeneralUpdate Pin
solomonope28-Feb-12 11:31
membersolomonope28-Feb-12 11:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170524.1 | Last Updated 11 Jan 2008
Article Copyright 2008 by Ghulam Murtaza
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid