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

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

2-Reference Types

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

3-Pointer Types

delegates

4-Anonymous Types

var

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

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

 

http://www.codeproject.com/Tips/460614/Difference-between-var-and-dynamic-inCSharp
http://www.harding.edu/fmccown/java_csharp_comparison.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.

About the Author

Hi! I'm Ahmad. I have a degree in Computer Science from Birzeit University and you might call me a bit of computer geek. I am the Founder of Greenbackend Turnkey Solutions a leading Cloud and Hosting Company in the Middle East, Also I am the Founder of Ahmad Naser Turnkey Solutions in Harvey, LA, USA. I'm comfortable with a large range of languages and techniques. After implementing enterprise applications using Salesforce and Siebel CRM for 3 years and with professional experience in building websites, cloud apps, iOS apps, Unity games, Android mobile apps and educational games for 8 years. I decided to share my technical knowledge with people all over the world to benefit from my experience and build their own careers specially because I am a real world example with over 20 successful apps on Google Play Store, Asset Store and Apple Store, So your success is mine and I want to help you reach your goals step by step exactly as I do. I'm passionate about teaching people about technologies, so from time to time I gave courses online and in learning centers and teach beginners and professionals on many technologies and development frameworks. One of my greatest goals in life is growing the next generation of software professionals and to keep teaching programming at every opportunity I get. I can't wait to help you experience the achievement and financial freedom that having a deep understanding of technology brings. So why not start learning to design & develop now by joining our epic Unity 3D professional 2d game development course? See you soon! Ahmad

>