6 Differences Between C# and Java: Objects ,Classes and DTs


One of the most important aspects of C-derived languages is object orientation. Objects and classes allow programs to specify methods and variables in one portion of code and use them again wherever necessary. While the basic structures of class construction remain consistent between C# and Java, some subtle differences my cause problems for developers unaccustomed to the idiosyncrasies between the two languages.

#1: Instance-level inner classes

C#: Work-around support Instance-level inner classes
Java: Support for Instance-level inner classes
An inner class (also called a “nested class”) is declared entirely inside another class or interface. Although both languages support inner classes at the Class level, only Java supports these inner classes at the instance level without the need to pass around the outer object instance. Java handles the instance-level inner class with an “outer this pointer”.

#2: Partial Classes

C#: Supports partial classes
Java: No support for partial classes
A “partial class” is a class whose methods and variables are parceled out into multiple files. When the files are compiled, the class reassembles itself into the full class definition. While the C# 2.0 compiler (and other OOP compilers) allows for class files to merge at compile time, the Java compiler does not. In Java, each class must be in its own specific source code file.

#3: Anonymous Classes

C#: Supports statement-level anonymous classes
Java: Supports implicit anonymous classes
An anonymous class is just that: a class without a name. Developers often define anonymous classes within a method to build simple delegate callback objects, such as those used in listener methods. Java treats anonymous classes as implicit, but C# code must defined the anonymous class at the statement level.

#4: Properties

C#: Supports properties
Java: Does not support properties
A property uses the tools of a method while holding a value like a variable:

Although other Java-related languages and toolsets (e.g. JavaBeans and JavaScript) support similar ways of defining a property, Java does not.

#5: Events

C#: Supports events
Java: Work-around support for events
An event is a way that a class can notify its clients that an action has occurred that affects some method or variable within the object.  Although Java does not support the “event” keyword for this specific purpose, Java developers can create a class that has much of the same behavior as an event.

#6: Data Types

1-Value Types

byte, sbyte
short, ushort, int, uint, long, ulong
float, double, decimal
structures, enumerations

2-Reference Types

object (superclass of all other classes)
arrays, classes, interfaces

3-Pointer Types


4-Anonymous Types


var: The compiler and runtime know the type. you can not change type at run-time!(for local variable declaration only)

dynamic: Most similar to object, but with dynamic dispatch. you can change type at run-time!(for local variables, return type, and parameter type)


Honorable Mentions

Operator Overloading
C#: Supports
Java: Does not support
According to the Java FAQ, Java does not support operator overloading “because C++ has proven by example that operator overloading makes code almost impossible to maintain”.

C#: Supports
Java: Does not support
Indexers allow class instances to be indexed and counted in ways similar to arrays for variables. Class instances in Java can still be indexed, but the “get” and “set” methods must be specified as variables.

Example: http://www.javacamp.org/javavscsharp/indexer.html




C#: Supports
Java: Does not support
C# allows both implicit and explicit conversions from one data type to another. Java requires that the user specifically state the conversion method.

Leave a Reply