14.07.2013 Views

Contents - Cultural View

Contents - Cultural View

Contents - Cultural View

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Comparison of Java and C Sharp 75<br />

Numeric applications<br />

To adequately support applications in the field of mathematic and financial computation, several language features<br />

exist. [13] In this category, Java provides the strictfp keyword, which enables strict floating-point calculations for a<br />

region of code. This will ensure that calculations return the exact same result on all platforms. C# provides no<br />

equivalent, but does provide the built-in decimal type, for accurate decimal floating-point calculations. This forgoes<br />

the problems that exist with binary floating-point representations (float, double). Such binary representations are not<br />

suited to accurately represent decimal numbers and hence introduce rounding errors. For financial applications, an<br />

accurate decimal type is essential.<br />

The BigDecimal class also provides such characteristics for Java. BigDecimal and BigInteger are types provided<br />

with Java that allow arbitrary-precision representation of numbers. As of 2010 the current stable release of the .NET<br />

framework (4.0) includes classes for manipulating arbitrary-precision integers and complex numbers (with operators<br />

overloaded for easy use so that BigInteger objects can be used just like any other primitive data type) but still .NET<br />

Framework lacks classes to deal with arbitrary-precision floating point numbers (see Arbitrary-precision arithmetic).<br />

In Java there is no way to provide the same level of integration for library-defined types such as BigDecimal or<br />

complex numbers as there is for the primitive types. For this purpose, C# provides the following:<br />

• Operator overloading and indexers providing convenient syntax (see below).<br />

• Implicit and explicit conversions; allow conversions such as exist for the built-in int type that can implicitly<br />

convert to long.<br />

• Valuetypes and generics based on valuetypes; in Java every custom type must be allocated on the heap, which is<br />

detrimental for performance of both custom types and collections.<br />

In addition to this, C# can help mathematic applications with the checked and unchecked operators that allow to<br />

enable or disable run-time checking for arithmetic overflow for a region of code. It also offers rectangular arrays,<br />

that have advantages over regular nested arrays for certain applications. [13]<br />

Methods<br />

Methods in C# are non-virtual by default, and have to be declared virtual explicitly if desired. In Java, all non-static<br />

non-private methods are virtual. Virtuality guarantees that the most recent override for the method will always be<br />

called, but incurs a certain runtime cost on invocation as these invocations cannot be normally inlined, and require an<br />

indirect call via the virtual method table. However, some JVM implementations, including the Sun reference<br />

implementation, implement inlining of the most commonly called virtual methods.<br />

Java methods are virtual by default (although they can be "sealed" by using the final modifier to disallow<br />

overriding). There is no way to let derived classes define a new, unrelated method with the same name.<br />

This means that by default in C#, and only when explicitly asked in Java, you can define new methods in a derived<br />

class with the same name and signature than the one in its base class. Which implies that when you call that method<br />

on such an object, depending on the current knowledge of the caller (if it knows its exact subclass or not), the result<br />

will be different.<br />

In Java (by default), the subclass's method will be called, but you will be able to call the base class' own method if<br />

needed. In C# (by default), the base class' method will be called, and you won't be able to intercept the call.<br />

In very specific cases, when a base class is designed by a different person, and a new version introduces a method<br />

with the same name and signature as some method already present in the derived class, problems can happen.<br />

In Java, this will mean that the method in the derived class will implicitly override the method in the base class, even<br />

though that may not be the intent of the designers of either class.<br />

To prevent this versioning problem, C# requires that if a method should be overridden, the override keyword must be<br />

specified. Otherwise, the method will "hide" the inherited method. A compiler warning to this effect is issued, which<br />

can be silenced by specifying the new keyword. This avoids the problem which can arise from a base class being

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!