5 Differences Between C# and Java: Objects and Classes

Jun 21, 2012
C# Java
By

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:

// Declare a Name property of type string:
public string Name
{
get
{
return myName;
}
set
{
myName = value;
}
}

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.

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”.

Indexers
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

Conversions
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.

Author: Gerald Hanks
Gerald Hanks has been involved in web development applications since 1996. He has designed applications with JavaScript, ASP.NET and PHP, as well as building databases in MS SQL Server and MySQL. He lives in Houston, Texas.
  • John

    JavaScript is not related to Java in any way, apart from a ill marketing stunt to make them share a similar name.

  • http://wisentechnologies.com/it-courses/.net-training.aspx .Net Training in Chennai

    in C#, within the interface, you can’t define the inner class

  • http://www.hirewebdevelopersindia.com/ Eric Lewis

    In my opinion, you should learn JavaScript (its actually UnityScript but thats not important) first because it is a simpler language. With C#, you have a bit more functionality, but because you are a beginner I doubt you would get into those aspects.

  • http://www.learnquest.com/subcategories.aspx?categoryid=5 Jacob Russell

    There are some differences between these two languages but the overall feel is kinda similar, if you already know C# then you can easily learn Java.

  • Tsayper

    I’d add in Java all methods are virtual and can be overloaded if not sealed