Java And C# Compared(转载自c#helpe)

Ever since the advent of the internet, Java has been the target for many developers.
Many C++ developers migrated to Java within a short period of time. Many high quality multithreaded animations, games, distributed applications are being developed and implemented successfully. Moreover the Java language is completely Platform Independent. 

Java Programs called Applets are verified for security in user's machine before downloading, thus ruling out the possibility for any viruses creeping into the user's machine. Many new advancements like swing package, Java2D API, Networking packages (java. net) have been built into the language after the initial release of Java Development Kit. Since Java is from Sun Microsystems many of its rival companies have started creating an alternative to this hot programming language, some two years back. 

Now a company which needs no further introduction, Microsoft have come up with a technology called Microsoft. NET. One of the prime languages in the .NET family is C# or C-sharp. This language was derived from Java/C++. Technically from my experience with C# for over six months, I am of the opinion that C# is more or less similar to Java. But many of the features of C++ like operator overloading (which was removed from Java) are there in C#.

Java programs will run on any platform having Java Virtual Machine or JVM installed on it. But .Net programs targets platforms having Common Language Runtime or CLR. This is the runtime which is being used by all the .Net languages for the execution. Thus one .Net language can call the modules and functions written in another .Net Language. Further if you learn a language like C#, then it is very easy to learn other .Net languages because all .Net languages follow what Microsoft calls .Net Framework. This Framework includes all the necessary class libraries to build and deploy robust .Net applications. With .Net you can perform server side programming via ASP.Net, much like servlets in Java. 

Like Applets in Java, C# introduces WinForms and WebForms. With WinForms you can design and develop windows based applications and with WebForms you can develop applications for the Web. This can be achieved either through code with C# or by using development environment tools like Visual Studio.Net. It includes Visual C# with which you can build C# applications very easily and with minimum effort. 

WinForms is almost similar to win32 applications which we develop using Visual C++.
It is possible to develop these applications using C++. But VC++ simplifies our development effort by providing us with wizards. Now Visual C# is also doing the same work. So if you are already a VC++ developer, it will be better for you to try out VC#. 

Comparing a Java program with C# program.
It is worth to compare a program written in Java with that of C#. We will then discuss the major differences involved in both programs. 

Hello Java Program
class Hello { // class declaration
public static void main(String args[]) { // main method, entry point
System.out.println("Hello Java"); // prints Hello Java to the console
}
}
Hello C# Program
using System; // System namespace called
class Hello   { // class declaration
public static void Main () { // main method, entry point
console.writeLine("Hello C#"); // prints Hello C# to the console
}
}
In Java, the lang package is automatically imported. It is not necessary to import this package in simple programs. But in C# we have to call the namespace System in all programs. Please note that console is a class under the System namespace. Try out the above program and observe the output.
Other Notable Differences
Main Method
Java:
It is possible to compile a Java program without a main method, provided that class is used as a super class. However the program will execute only when there is a valid main method in the class. 
C#: It is not possible to compile a C# source file without a valid main method. If it is absent, then the compiler will show an error message like program 'filename.exe' does not have an entry point defined.

Inheritance
Both Java and C# doesn't supports multiple inheritance and provides interfaces as an alternative to it. However C# covers much more advanced issues in interfaces. Please check out the topic titled New Additions given at the end of this article. 
 
Operators and control flow statements:
All operators like Arithmetic, logical, increment and decrement etc available in Java are supported by C#. Moreover control flow statements like if, else, if-else, for, do-while, while etc included with Java  are also available in C#. However C# reintroduces the popular GoTo statement found in C++. 

Exception Handling:
You can handle runtime errors also called as Exceptions in C# by making use of try, catch, finally, throws clauses. Exception handling in C# is almost same as in Java except some minor differences. System.Exception namespace is the base class of all the exception classes in C#. 

Multithreading:
The application of Multithreading in C# is much more simplified, than in Java. We can synchronize  threads by making use of the Monitor and Mutex classes. 

New Additions
The concept of Indexers, Attributes and Delegates are new to C#. Indexers are almost similar to Vectors in Java. A complete discussion related to these new concepts are beyond the scope of our discussion. However I will try to explain these at an another occasion through this site. C# introduces the concept of operator overloading, which was not present in Java.
However both Java and C# supports Method overloading and Method overriding.

Coming back to interfaces, we can verify whether the methods included within an interface are implemented by using is and as operators. There are ways to avoid name ambiguity while implementing the interface methods. You can even combine two interfaces into one and implement that combined interface in a class like the following piece of code

using System;
public interface First {
void A();
void B();
}
public interface Second {
void C();
}
public interface Combined: First, Second { }

class Combineimple: Combined {
// statements goes here
// main method goes here

} }

Further C# compiler shows the compile time error messages along with their error numbers. You can search the online documentation that comes with the SDK for the error number to study the cause of the error in detail.
posted @ 2010-06-28 11:03  mozer  阅读(224)  评论(0编辑  收藏  举报