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 79<br />

There are also differences between the two languages in treating the try-finally statement. The finally is always<br />

executed, even if the try block contains control-passing statements like throw or return. In Java, this may result in<br />

unexpected behavior if the try block is left by a return statement with some value, and then the finally block that is<br />

executed afterwards is also left by a return statement with a different value. C# resolves this problem by prohibiting<br />

any control-passing statements like return or break in the finally block.<br />

A common reason for using try-finally blocks is to guard resource managing code, thus guaranteeing the release of<br />

precious resources in the finally block. C# features the using statement as a syntactic shorthand for this common<br />

scenario, in which the Dispose() method of the object of the using is always called.<br />

Finally blocks<br />

Java allows flow of control to leave the finally block of a try statement, regardless of the way it was entered. This<br />

can cause another control flow statement (such as return) to be terminated mid-execution. For example:<br />

int foo() {<br />

}<br />

try {<br />

return 0;<br />

} finally {<br />

}<br />

return 1;<br />

In the above code, the return statement within try block causes control to leave it, and therefore finally block is<br />

executed before the actual return happens. However, finally block itself performs a return as well; thus, the original<br />

return that caused it to be entered is not actually executed, and the above method returns 1 rather than 0.<br />

C# does not allow any statements which allow control flow to leave the finally block prematurely, except for throw.<br />

In particular, return is not allowed at all, goto is not allowed if the target label is outside the finally block, and<br />

continue and break are not allowed if the nearest enclosing loop is outside the finally block.<br />

Lower level code<br />

The Java Native Interface (JNI) feature allows Java programs to call non-Java code. However, JNI does require the<br />

code being called to follow several conventions and impose restrictions on types and names used. This means that an<br />

extra adaption layer between legacy code and Java is often needed. This adaption code must be coded in a non-Java<br />

language, often C or C++. Java Native Access (JNA) allows easier calling of native code that only requires writing<br />

Java code, but comes at a performance cost.<br />

In addition, third party libraries provide for Java-COM bridging, e.g. JACOB [19] (free), and J-Integra for COM [20]<br />

(proprietary).<br />

.NET Platform Invoke (P/Invoke) offers the same capability by allowing calls from C# to what Microsoft refers to as<br />

unmanaged code. Through metadata attributes the programmer can control exactly how the parameters and results<br />

are marshalled, thus avoiding the need for extra adaption code. P/Invoke allows almost complete access to<br />

procedural APIs (such as Win32 or POSIX), but limited access to C++ class libraries.<br />

In addition, .NET Framework also provides a .NET-COM bridge, allowing access to COM components as if they<br />

were native .NET objects.<br />

C# also allows the programmer to disable the normal type-checking and other safety features of the CLR, which then<br />

enables the use of pointer variables. When using this feature, the programmer must mark the code using the unsafe<br />

keyword. JNI, P/Invoke, and "unsafe" code are equally risky features, exposing possible security holes and<br />

application instability. An advantage of unsafe, managed code over P/Invoke or JNI is that it allows the programmer<br />

to continue to work in the familiar C# environment to accomplish some tasks that otherwise would require calling

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

Saved successfully!

Ooh no, something went wrong!