A Comparison between Java & C#

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. You can go through the java tutorial topics on this web site if you are having any doubt.

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 6 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 (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 on another .NET Language. With .NET you can perform server side Programming via ASP. NET, like Servlets in Java.

Java Applets Vs .NET

In the last Session, we have dealt with Java Applets. Now you may be having questions like, is the alternative for Applets in .NET in the mind. My answer is yes, there is an alternative to Applets in .NET. It is called WinForms Applications. With WinForms you can design and develop windows based applications. This can be achieved through Code with C# or by using Development Environment tools like Visual C#.

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

HelloWorld in Java

  1: class Hello {
  2: public static void main(String args[]) {
  3: System.out.println("HelloWorld");
  4: }
  5: }

HelloWorld in C#

  1: class Hello {
  2: public static void Main() {
  3: Console.WriteLine("Hello world");
  4: }
  5: }

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:

  1: using System;public interface First {
  2: void A();
  3: void B();
  4: }
  5: public interface Second {
  6: void C();
  7: }
  8: public interface Combined: First, Second { }
  9: class Combineimple: Combined {
 10: // statements goes here
 11: // main method goes here
 12: } 
 13: }

           
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.

2 Comments

Leave a Comment