Similarities and Differences between Java and C#

by Michael

You’re a Java developer about to embark on a C#.NET project or a C#.NET developer about to start work on some Java code or you’re just curious about the differences between these two platforms. Whatever the case may be, fear not the new, because in this case, you will see that these languages have a lot in common.

In a sense, C# feels like the bastard child of Java and C++ and somehow even Python managed to get in on the action (yield and dynamic variables). As mentioned on the wiki page, C# began its life as an extension of Java designed by Microsoft. But Sun disagreed with Microsoft’s design suggestion as Sun felt that they went against Java’s philosophy of keeping things simple. Thus C# was born and this is the reason why there is so much of an overlap between Java and C#. Being a new language, it had the advantage being able to adapt to the wants and needs of developers to include many new programming aspects such as lambda expressions and dynamic variables.

Namespace and Package

In Java, one is forced to map the package structure to the file structure. This isn’t the case in C# where namespace acts pretty much like namespace in C++ and you can have your C# files wherever you want as long as they are in the project path. In Java, one writes import to include functionality while in C# one would write using.
The Java Way

The C# Way

Naming Conventions

Anything that is public in C# is written in Pascal case, private member variables have an “_” (underscore) prefix followed by camel case, variables declared inside functions use camel case. In Java, you use Pascal case for class names and camel case for class members. This can be seen in the images above where I wrote some example code.

Constants in most languages are represented using SCREAMING_CAPS, all uppercase with underscore separators. This is discouraged in C# but fine in Java. C# also suggests that developers use Hungarian notation for helping identify the type of the variable.

Since C# doesn’t distinguish between implementing an interface and extending a class, which is the case in Java, interface declarations have a “I” (capital i) prefix, such as IDisposable.

At this stage you will have also noticed that C# uses C style block declarations where open curly brace is on a new line, while Java has the open curly brace on the same line as the expression or declaration, the exception in both cases being inline blocks.

Implementing Interfaces and Extending Classes

While Java uses the keywords implements and extends, C# uses the colon (:) to show that a class implements an interface or extends a class. C# syntax doesn’t differentiate between the two whereas Java is strict about which keyword should be used.
The Java Way

The C# Way

Getters and Setters

You will have already noticed that Java uses a naming convention for declaring getters and setters while C# uses the keywords get and set. In C#, variables that are accessed via accessors are properties and are accessed using conventional dot notation, much like a public variable in Java. In Java, many frameworks make use of the naming convention to simplify accessing variables via getters and setters, such as Javas expression language used in JSP’s.


C# implements string as a primitive whereas Java implements String as an object. Generally, Java has a class definition of all primitives such as int and Integer, this is not the case for C# where a primitive is the class. Thus, in Java you would write

int i = Integer.parseInt("2");

and in C#

int i = int.Parse("2");

This also means that you can use primitives in generics freely, which is not the case for Java generics where you are required to use an object instead.

C# specialties

C# is a gigantic language when compared to Java which is a much more compact language but used for similar tasks as C#. C# has a plethora of keywords that have a slightly different name than in Java or non-existent in Java, such as ref and out. For a detailed keyword comparison, follow this link.

For Java Developers

If you’re new to C#, make absolutely sure that you familiarize yourself with linq and lambda expressions. These are two very powerful C# features, one could say a killer-app for C# that makes for very elegant solutions. Also, the use of implicitly typed variables using the keyword var.

For a very detailed comparison, check out the Wikipedia link: comparison of Java and C# as well as very thorough analysis of the differences between the two languages