2.1 Potent combo of Java and C


2.1 Potent combo of Java and C++

When I first started learning C#, I was mildly surprised at how much the C# team ' copied ' from the Java programming language. Microsoft defined C# as "a modern object-orientated language that enables programmers to quickly build a wide range of applications for the new Microsoft .NET platform". It was added that C# was "derived from C and C++". It has never been mentioned in any official Microsoft literature (not even the C# Language Specification) that certain features in C# have been developed after considerable study of the Java language, though this fact is immediately obvious to anyone who knows a bit of Java and C++.

Many of us will agree that Java is an improved version of C++. Java got rid of several unpleasant C++ features such as multiple inheritance, the goto keyword, and added a host of nice features such as a common class ancestry for all Java classes and Java interfaces (in place of multiple inheritance).

C# can also be viewed as an improved version of Java. Like Java, C# supports only single inheritance, uses interfaces, and all objects have a common ancestral class (C#'s System.Object ).

Despite the similarities between C# and Java, to say that C# has been totally modeled on Java alone is not fair [5] . There are also several C++ 'legacies': such as the destructor, preprocessor directives, enum keyword, structures, and operator overloading to name just a few. These 'legacies' will be total strangers to Java developers with no C/C++ background.

[5] Java is, after all, a language that has its roots in C++ (and SmallTalk). Hence, some would argue that it is not wrong to state that C# is a direct descendent of C/C++ alone, although I believe some credit has to be accorded to the founders of the Java language. Java has proved the viability of some OO language features not found in C++ (such as single class inheritance and much stricter typing).

In addition to C/C++ 'legacies' and Java 'add-ons', C# spots several new useful features not found in both languages “ examples include C# indexes, properties, attributes, delegates and events. A summary is shown in Figure 2.1.

Figure 2.1. C# has not only extracted the cream features from both C++ and Java, it has come up with several new extras to aid the powerful developer. These extras come at a price “ a steeper learning curve and the possibility of 'abuse' by weaker developers who use them for the wrong purposes.

graphics/02fig01.gif

Microsoft calls C# "a combination of the ease of use of VB and the power of C++". I would rather view it as having the elegance and simplicity of Java coupled with the power and convenience of C++ (see Figure 2.2). The C# team obviously had the advantage of benefit from the experience of both Java and C++ when they came up with a whole new programming language (and I think they have done a good job having seen the product.)

Figure 2.2. The new .NET programming languages and their predecessors in VS .NET. C# is a brand new language targeted primarily at the .NET platform. C# inherited elegance and simplicity from Java, and power and convenience from C++.

graphics/02fig02.gif

Because of C#'s similarity to Java, learning C# will be relatively easy if you have a good Java foundation (compared to learning C# with a VB background, for example). Despite the initial "Hey-that's-a-feature-of-Java!", as you go along you will realize that C# is, after all, not so much like Java in certain aspects. There is a learning curve to climb even if you are a Java expert (and especially if you have never been exposed to C or C++). Nevertheless, it is the objective of this book to bridge this gap.

2.1.1 Is C# superior to Java?

It is difficult to say if Java or C# is the superior language because their purposes are vastly different in the first place. [6] Java emphasizes portability, platform independence, and simplicity. C# is commonly viewed as the most powerful .NET language used for writing web applications, Windows applications, and web services on the .NET platform. For C#, portability is of low value “ rather, the emphasis is on giving the programmer as much power and convenience as needed, even if it takes more effort to master the language.

[6] Always keep this point in mind when you read articles comparing the two languages (especially marketing material). The basis of comparison must be made very clear so that the comparison is fair. For example, you can say that Java's inability to make use of native operating system features is a disadvantage, but that is actually an engineering trade-off for portability. Likewise, the inability to use pointer operations in Java to access memory directly can be viewed as both an advantage and disadvantage .

One thing is for sure, learning C# can involve a steeper climb than learning Java if you start from scratch. In addition to the 'convenience features' added into C#, C# has also preserved several features from C++ which the Java designers left out deliberately (such as operator overloading and the enum keyword), thus making learning C# more difficult compared to Java.

Table 2.1 compares the features of C# and Java. Where feature richness and power is concerned , I would say that C# is the clear winner. But Java wins in ease of use and learning. In my personal opinion, C# will succeed if, and only if, .NET succeeds. This language has been created from the ground up just for the .NET platform “ if vendors do not use .NET for their implementations , there is little reason for software engineers around the world to learn a new language.

One important advantage of C# is the ability to write unsafe codes. Java codes must run within the JVM, and that is a severe limitation when it comes to real time programming. It is also impossible to write to memory-mapped devices using Java. JNI “ which comes with performance overheads “ is needed for Java codes to 'talk to' native components . It all boils down to the same engineering problem “ where is the appropriate balance between portability and efficiency.

Table 2.1. Comparing some aspects of Java and C#

Feature

Java

C#

Platform portability

Generally portable in the spirit of Java's "write once, run anywhere " vision

Portable to other .NET platforms “ currently, .NET runs on Windows only, though this might change in the future

Unsafe codes

Not able to write unsafe codes “ Java codes cannot perform pointer operations

Can be used to write unsafe codes in code blocks marked with the unsafe keyword

Simplicity

Very simple and easy to learn. The feature set has been kept to a minimum so that features that help productivity are not present. You can still write powerful OO Java codes without these convenience features

More difficult to learn because the language has many features “ several productivity (convenience) features, such as operator overloading, are retained

Standardization

Regulated and evolved by the JCP

Standardized by ECMA

Post compilation codes

Java bytecodes in class files which runs on a JVM

IL in .NET assemblies which runs on a .NET CLR

The next chapter will discuss Microsoft's JUMP to .NET strategy specially tailored for Java developers.



From Java to C#. A Developers Guide
From Java to C#: A Developers Guide
ISBN: 0321136225
EAN: 2147483647
Year: 2003
Pages: 221
Authors: Heng Ngee Mok

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net