Thursday, August 20, 2015

Difference Between Java and C#

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.


Other Differences between Java and C#


  • C# and java both were derived from C++, and therefore they have similar roots, both are widely used for web programming. We discuss the difference between C# and java these are as follows: 
  • C# has more primitive datatypes 
  • Java uses static final to declare a class constant while C# uses const. 
  • Java does not provide for operator overloading. 
  • C# supports the struct type and java does not. 
  • Unlike java, all C# datatypes are object. 
  • C# provides static constructors for initialization. 
  • In java, parameters are always passed by value, c# allows parameters to be passed by reference by Ref keyword. 
  • C# includes native support for properties, java does not. 
  • Java does not directly support enumerations. 
  • In java, the switch statement can have only integer expression, in C# supports integer and string both.

0 Comments:

Post a Comment