Friday, 13 April 2012

introduction to c and java

Till now it is the well known fact  that from last 5 years the only language which ruled the complete world is java, a programming language that is launched by the Sun micro systems.  Java has all the features to provide varied services to its customers.  Especially with the web services it become most popular.

But with the release of new language  called C# from Micro Soft, has shacked the roots of its Royalty. Now I think, everyone will agree the fact that release of C# effected the market of Java.

So today I would like to discuss about the running cold war between C# and Java.

Areas which I am going to cover are :

Brief introduction to c#.
Factors that lead to the evolution of C#.
.Net platform that is supported by c#.
Similarities and dis-similarities between c# and Java.
Impact of C# on Java.
C# improvements over Java.
Finally with an example, I conclude topic by presenting my view.

Microsoft describes c# as a simple, modern, object-oriented and type safe programming language derived from c and c++.  As some experts says c# as the “technology of the year”.

C# is a suitable automatic garbage collection, automatic variable initialization and type-safing the variables. C# will give the basic freedom of coding at the same time bestows you with all the advantages of Java.  So we can develop applications with the power of c, ease of VB and the simplicity of Java.

Using simple c# language constructs, the components can be converted into web servers. MS will be offering c# with Visual Studio 7 and though c# does not have class library of its own, it has access to libraries used by VB and VC++.  C# can be called as the superset of all the previous languages, because it incorporates more features than the older languages.


Why does Micro Soft think we need another language?  When Micro Soft introduced VJ++ in October 1996, it throws lots of resources into the project. Their efforts produced the fastest JVM on the market and the WFC, a set of Java classes wrapped in WIN32 API.

Micro Soft decided to make changes to Java to integrate it more closely with windows.  Some of the changes – interfacing seamlessly with COM, refusing to support RMI and JNI, and adding delegates – caused it to break compliance with the Java standard.

Consequently, SMS sued MS in October 1997 for violating Java license agreement. This doomed MS’s feature development of Java and VJ++.

However, MS decided to take its advances in the java language, java compiler and JVM and morph them into an even more ambitious project .Net.


If I talk about c# without telling .Net platform it is just going to be an incomplete speech. So, I briefly discuss about .Net too.

The term MS.NET is similar to ‘Windows DNA’ in that it refers to many things : a business strategy, development model, market pitch, development platform, and language runtime.

Programs written in c# are compiled into an intermediate language called
MSIL, the equivalent of Java byte code.  Any language that can be compiled to MSIL can take advantages of the CLR features such as garbage collection, reflection, meta-data, versioning, events and security etc.

It is interesting that micro soft is pushing cross-language development while Sun/Java pushes cross-platform development.


Resemblance of java with c# goes beyond syntax, keywords and delimiters.
It includes many features in common are as follows:
Automatic garbage collection.
Reflection for type information discovery. 
Source code is compiled to an intermediate byte code.
JIT compilation compiles byte code into native code.
Every thing must be in a class – no global functions or data.
No multiple inheritance, although you can implement multiple interfaces.
All classes derive from object.
Security for restricting access to resources.
Exceptions for error handling.
Code comments as documentation.
Array are bounds checked.
GUI, networking and threading support.
No un-initialized variables.
No pointers.
No header files.

Most of keywords in java have their c# counterpart.  Some key words are identical. For example : new, bool, this ,break, static class ,throw and null.
(as they are derived from c++).

C# specifies access modifiers inline as part of the member definition just like java.


Some keywords that are in java has different name in c# like
Super - base,  import – using, package – namespace ,synchronized – lock, final – sealed respectively.

The following table shows differences in java and c#.

Name                                       Java                               C#

Inheritance            class Foo extends Bar              class Foo:Bar,FooBar{}
                                  implements FooBar{} 

Referring to the      Super.hashcode();                    base.GetHashCode();
base class

Importing types        import java.util.*;               using sytem.Net;
Namespaces            package mystuff;                 namespace mystuff;
                                 class Myclass{}                  class Myclass{}

prevent inheritance  final class foo{}               sealed class foo{}

constants                  final static int max=5;      const int max=5;

code comments          /* comment */                  /// comment

Synchronization      synchornized(this){}         lock(this){}

Deprecated classes       @deprecated don’t use!     [obsolete(“don’t use”);]
    or  methods

No throws class in c#, all exceptions are  unchecked.

Package access is default for Java while private is the default for c#.

Another good example of cosmetic differences is the system.  Object class in c#, which has the exact same methods as the java.lang.object class in java.


You believe the fact that c# influenced the java’s market. Nearly 40% has been reduced after release of c#.


C# has a simplified syntax for things such as iterations, events and treating primitive types like objects which reduces the amount of code you need to write.

Version problems can be solved.
Assertion are useful for sanity checking and enforcing the precondition, post conditions and invariants of design by contract.  Value types in c# (int, long, char) can be treated just like reference types without requiring special wrapper classes in java.

Provide implementations for runtime type identification , serialization etc.
Reflection provides the ability to obtain type information dynamically and makes it possible for the runtime system to automatically inform.


Example : Hello World in Java
    class helloworld
     {   public static void main(String[] args)
           {  System.out.println(“Hello World”);

 Hello World in C#

    class  helloworld
     { public static void Main(string []s)
         {  System.Console.WriteLine(“Hello World”);


C# or Java?  One big advantage with MS’s products is, when they come up a new product, they bundle it with nice development and debugging tools.  Which lacks in Sun Micro Systems products where developing and debugging is difficult.

Now c# along with  .Net has one advantage – the power of c, which is probably missing in java. C# will give you that freedom at the same time bestows you with all the advantages of java.  All programming languages are compiled to an intermediate languages in the MS.NET architecture it is possible to inherit a c++ class in small talk class and instantiate that in c# class which is never possible in java.

One big issue is java has been already with us for the past  5 years and has a long base of programmers but Ms now has a tough time in training .Net or c# developers.

One problem with MS software is the security many  bugs and memory allocations errors are common in MS’s software and active X components is a serious security violation, which are clouded over the internet. Javas sandbox is still a winner and strongly controls behavior of the codes. But still people feel that java programmers are slow. Let us wait and see whether the c# make such a faster impact like what java has made………….


Post a Comment