University of Waterloo Faculty of Mathematics

A Comparison of Java to C# and Microsoft.NET

Copyright © December 2001 by Graydon Armstrong

NET .NET Documentation Java C# and .NET Other Considerations Java C# and .NET Porting Issues Conclusions Recommendations References 3 4 4 4 5 5 7 7 7 9 9 10 10 11 12 13 14 15 16 Graydon Armstrong A Comparison of Java to C# and Microsoft.Table of Contents Executive Summary Introduction Analysis Language Java C# APIs Java .

until it is determined whether . due mainly to a poor design of the . flexibility and ease of use.NET really catches on in industry.NET. The report recommends considering scaling back . The basis of evaluation is a combination of factors.NET APIs and Microsoft’s relative lack of compatibility with other software.NET development to a core set of features.Executive Summary This report examines and compares Sun Microsystems’ Java programming language and associated APIs with Microsoft’s C# programming language and . This report concludes that Java is preferable to C# and .NET APIs. primarily including power. Cost is not an issue.NET . Graydon Armstrong 3 A Comparison of Java to C# and Microsoft. as all three packages are available freely.

with a set of APIs and a virtual machine. logical design balancing power and simplicity is of great importance. flexibility and simplicity. Developers may abandon a crippled language lacking in power. and may become frustrated with a language that is unnecessarily complex.NET framework combines a new language. portability. C#. and also discusses issues encountered porting code from Java to C#. flexibility and ease of development. but Microsoft may charge for . Analysis Comparative analysis of the two platforms is based primarily on criteria of power. on the surface it sounds very much like Java. Sun Microsystems’ Java platform. which debuted in 1995. Language A strong programming language can be the foundation of a strong development platform.Introduction Microsoft in late 2000 made available a public beta release of their latest development initiative. A clean.NET tools in the future. Cost is not an issue at this time. This report compares the Java platform to the C# language and .NET APIs. has become quite popular in industry due to its type-safe object-oriented design.NET . The . Graydon Armstrong 4 A Comparison of Java to C# and Microsoft.

While the keyword is convenient. does not contain features such as pointers. The result is a very powerful language.NET . This results in cleaner and more readable code. They take the place of Java’s getter and setter methods. but one which is also quite complex (possibly excessively so). structs. C++ developers may bemoan such omissions. if somewhat less efficient. the foreach keyword masks what is really a for loop using an enumeration. Other features introduced in the C# language are very welcome.NET framework in late 2000. but for simplicity. Indexers allow access to elements of a collection in a style similar to array access. C# Microsoft released the first public beta of their new C# language and . Properties in C# have advantages and disadvantages. enums. In many cases. Although very similar to – and clearly designed to compete with – Java.Java Sun Microsystems’ Java language made its debut in 1995 when it was evolved and renamed from Oak. C# supports many of the features of C++ that were dropped from Java and adds new ones as well. The language itself is similar to C++. and is employed with particular frequency by the collections and data access frameworks. C# has almost 20 more keywords than Java. such functionality probably does not belong in the language specification. this functionality would be used to check data validity before 5 Graydon Armstrong A Comparison of Java to C# and Microsoft. but there is elegance in this simplicity. Many of these keywords are used to mask simple functionality. allowing a property value to be stored or retrieved as if it were just a variable. For example. multiple inheritance or templates. code for a Java solution is generally relatively clean. by specifying the index in square brackets. operator overloading.

it is entirely possible to encounter the disturbing situation in which an exception is thrown during access of what appears in code to be a regular variable. Therefore. but the stability issues are much more severe. this ends up cluttering the code. Also. In summary. Apparently this design decision was made because Microsoft felt the trivial try and catch blocks that often appear in Java were clutter. it is impossible to tell whether an exception can be thrown. but like much of the rest of . When calling a method in C#. in order to ensure stability. Graydon Armstrong 6 A Comparison of Java to C# and Microsoft. and the property body code is a regular method. These are valid concerns. to propagate an exception down the stack in Java. C# is a powerful language. However. each method along the way must declare the exception to be thrown. C# does not support checked exceptions. some aspects could have been better thought-out. Also. which may or may not be correct or current.storing the value or to perform simple processing on the value. methods are not required to declare exceptions they throw.NET. In most production systems. the danger of which is only exacerbated by C#’s lack of checked exceptions. This is not good programming form.NET . it is not desirable to have an exception propagate down the stack to a level where it may crash the program. developers may be forced to put bits of code within try blocks and catching any exception. This is unsafe operation. except by referring to the API documentation. This was a very dangerous design decision. since the act of accessing a property is really a method call. catches should be specific.

a few poor (and quite illogical) design decisions seriously cripple what would otherwise be a reasonably good set of APIs.NET API used by C# is. . a well-thought-out group of collection interfaces and a variety of implementations of sets. they are a product of years of evolution. and they fumbled the opportunity badly.APIs APIs are libraries of classes implementing core functionality and concepts required by applications. unjustifiably poor. Although the problem is widespread throughout the .NET The design of the . nowhere is the problem more apparent than in the collections framework. The core Java APIs will grow significantly in size with the upcoming release of Merlin (Java 1. lists. The developers at Microsoft had a clean start. The API design can make or break an otherwise good language. logging and non-blocking IO. and maps. Of particular strength is the Java collections framework. in short. among other things. The future of the Java APIs looks bright.4). Java The core Java APIs are very well designed in most cases. Graydon Armstrong 7 A Comparison of Java to C# and Microsoft.NET APIs (suggesting it was a definite design decision). Of particular disgust are the poor implementations (or lack thereof) of the Equals and ToString methods. Support will be added for.NET . a chance to do everything right.

if two collections contain all the same elements. Fixing but a few small but glaring problems would have resulted in a good API. for example. then C# should have included support for templates. delegates and such that should have been written as nested classes because of their applicability only in the context of the would-be outer class. there is the equality operator. but apparently this was not to Graydon Armstrong 8 A Comparison of Java to C# and Microsoft.NET . one can only label this as another poor design decision by Microsoft. Instead. must write their own comparator method. Overall. classes. displaying a text representation of a collection can be invaluable during the development process. any program wishing to check. many of these classes seem to be crying out for templates. yet so little to be desired. Less severe but a definite annoyance nonetheless is the ToString method. although this is more of a style issue than a real problem. Thus. Finally. Considering the uselessness of the default functionality. Worse.Microsoft decided that the Equals method should check for referential equality. and the trivial amount of code required to override it to provide a useful function. The issue becomes even more complex if collections are nested. the APIs are not wonderfully organized. they clutter the main namespaces. This was a ridiculous design decision on Microsoft’s part. the . In normal operation. There are hundreds of enums.NET API leaves so much. not equivalency of content. in particular while debugging. for the few occasions when a program does need to check referential equality. the default operation of which is to return the type name of the object. If Microsoft considers extending classes in such a way to be an acceptable practice. most programs will not have cause to call ToString on a collection. in many cases the only difference in functionality to the superclass is the return or parameter type of some methods. However.

It is not unreasonable to suggest that Javadoc has been one of the main factors driving widespread adoption of the Java platform.NET C# appears to have a documentation standard. Unfortunately. By defining a standard format for documenting code and providing the Javadoc compiler. Visual Studio sets it up with XML tags in what one can only Graydon Armstrong 9 A Comparison of Java to C# and Microsoft. but based on XML. This facilitates – and thereby encourages – third-party development. a search of the MSDN Library turns up no documentation about the documentation format.NET . Ironically. Documentation Documentation is a key resource in development on any platform. similar to Javadoc. Java The Javadoc documentation system. The only suggestion that such a format exists comes from the auto-format and auto-complete features in Microsoft Visual Studio. when a developer begins a comment in an appropriate context. at the time of writing.NET. C# and . C# provides similar functionality. Java defined a standard format for code documentation and provided tools to compile it to HTML. in-house productivity is improved by virtue of having available quick reference for libraries used by the code under development. Good documentation can be a significant factor in encouraging adoption of a technology. meta-documentation for C# is difficult-to-impossible to find. certainly deserves mention. it is likely too late to remedy these problems. Sun has made it easy to produce excellent HTML documentation of APIs. as doing so at this point would break countless pieces of software already developed for . while not technically part of Java.

The documentation can be compiled into XML with a command-line tool.NET . which is heavyweight and unnecessarily convoluted.NET API documentation in the MSDN Library. but no apparent tool is yet provided to transform the XML to a readable HTML format. One can hope that when such a tool is eventually provided. Graydon Armstrong 10 A Comparison of Java to C# and Microsoft. and not like the . that the generated HTML will be of a simple format similar to Javadoc.presume is the C# documentation format.

the Java virtual machine has been ported to literally scores of operating environments. testing APIs. and as such. it runs Java. Chances are. if an API feature is not adequately documented. including Windows. A variety of compilers are available. one cannot yet expect it to have the wide platform support enjoyed by Java. many for free. BSD.NET . as it promotes greater understanding of what is occurring behind the scenes. Source code for the APIs is included in the Java Development Kit. C# and . It is certainly beneficial to developers to be able to see API source code. Microsoft has so far only released the 11 Graydon Armstrong A Comparison of Java to C# and Microsoft. debuggers. if someone runs it. a developer can peek at the source to determine what the feature does and how it works. Issues such as costs and levels of industry adoption must also be addressed. not to mention high-end server platforms such as IBM OS/390 mainframes and handheld devices such as the Palm platform.Other Considerations Quality is not the only factor in a comparison between two technologies such as Java and .NET . Solaris.NET. subject to licensing terms. and in some cases allows optimization for these processes. and other tools. MacOS and virtually every flavour of Unix. Additionally. Java Java already has a large developer and user base. It is very platform independent. Linux.NET is a relative newcomer to the marketplace. Java development tools are also prolific. as are profilers. Java is open-source.

although a subset of the framework has been approved as an ECMA standard and so a reference implementation of this subset may be provided. though.NET API is closed-source.NET. Graydon Armstrong 12 A Comparison of Java to C# and Microsoft.NET APIs appear to be quite Microsoft-centric. however. and this is a dangerous trap.. the . although a port to FreeBSD is apparently in the works. . documentation of some classes suggests their functions are clearly only applicable to a Windows machine. Currently. and the data access classes are designed to use Microsoft SQL Server. This may change with the final release of .NET framework for their Windows platform. Microsoft seems intent on locking consumers into their technology in this way.NET . it is reasonable to expect Microsoft software to work efficiently together.NET. On the plus side. and should be considered in a decision to develop for .NET development tools such as the C# compiler are available for free download from Microsoft. This may be simply a “token” port to allow . The .NET to claim cross-platform compatibility.

Porting Issues Because both Java and C# have their roots in C++. Methods must be explicitly declared virtual if they are to be able to be overridden. In general. which become apparent when normally-overridden methods such as Equals seem to be misbehaving. the default implementation (referential equality check) is being used.NET . This issue can frequently cause runtime problems. fields and constants to be named using the InitialCaps (e. types. the only difference between the names of Java methods and their .NET counterparts is the capitalization. myString) style is used for 13 Graydon Armstrong A Comparison of Java to C# and Microsoft. Often. this is trivial to convert. The .g. this is not the case. all but minor aspects of the grammar are identical. properties.NET naming standard is for all namespaces. methods are virtual by default. C# uses a namespace block and using statements instead of Java’s package declaration and import statements. the superclass is accessed with the base keyword (super in Java). subclasses may override their implementations to provide isomorphism. A final language issue that often escapes preliminary detection is C#’s virtual and override methods. The initialCaps (e. really. there are no major language issues that arise during porting. In C#. In Java. Many of the .g. startIndex.NET classes are isomorphs of their Java counterparts. A C# type declaration is in the C++ style. and its constructor is called in the C++ style. methods. In C#. As such. ArrayList. The overriding method must also declare with the override keyword that it overrides a method. with the class name followed by a colon and a comma-delimited list of the type it extends and/or the interfaces it implements (as opposed to Java’s extends and implements keywords). it is a fairly trivial task in many cases to port Java code to C#. CompareTo) style.

the main method is called Main. The tool would automate the conversion of Java code to C#. there exists Visual J#. Since the main issue in porting is in dealing with the API differences. if it is. it will likely be in no small part due to Microsoft’s marketing clout and their lock-in strategy.NET will be a real contender.NET had a fresh start. In the meantime. there is no reason not to port it all the way to C#.NET” (Java User Migration Path to . but they may be rendered irrelevant anyway if Microsoft releases the “JUMP to . A full discussion of porting issues is beyond the scope of this report. and Java already has a large developer and user base. unconstrained by requirements of backward compatibility.NET to be done in the Java language.NET APIs. Java will likely remain a powerful force in the industry for years to come.NET . Considering that . Its APIs are much better designed than the . Note that in an executable class in C#. Conclusions Java is a much simpler language than C#. its offering could have been much better.local variables only. which allows coding for .NET) tool it announced long ago. Graydon Armstrong 14 A Comparison of Java to C# and Microsoft. yet it lacks little of the power of C#. though. It remains to be seen whether .

but viable. If porting becomes a significant time sink without much payoff. Clients should be encouraged to use Java solutions wherever possible. This issue may need to be reopened at a later date.NET API is poorly designed. difficulties in porting may increase in number and severity. customer requirements may demand support for . and it remains to be seen whether it will be truly accepted by the industry. The recommended course of action is to focus primarily on development for Java. Conversely. Abandoning a minimal set of ported code is not a big deal. the proposed solution allows for quick development around a minimal existing ported code base should the need arise while at the same time insulating the company from danger should the . However. Currently. since time is of great importance in software development. and neither is adding functionality to that code.NET. Either course of action can be undertaken with minimal costs. as the code increases in complexity. it may be worthwhile to abandon . However. It may not seem wise to wait “sitting on the fence” about this issue. it may make sense to provide full support for . The . if the API is improved or gains wide acceptance in industry. the task of porting Java code to C# is awkward. However.NET. Graydon Armstrong 15 A Comparison of Java to C# and Microsoft. development in only Java would be sufficient.NET initiative be a flop.Recommendations Ideally.NET . Porting to C# should be done only in cases where the work required to port is trivial or where key functionality is implemented by the code in question.NET.

3/techdocs/api/ The Java Language Specification (Second Edition) http://java.3/docs/api/ Java 2 Platform Enterprise Edition Java 2 Platform Standard Edition v1.NET Framework Class Library C# From A Java Developer’s Perspective Java Resources Overview API Specification http://java.html Graydon Armstrong 16 A Comparison of Java to C# and Microsoft.html C# Language Specification http://msdn.3 API Specification http://java.geo/java.NET .

Sign up to vote on this title
UsefulNot useful