Contents - Cultural View
Contents - Cultural View Contents - Cultural View
Comparison of Java and C Sharp 78 Conditional compilation Unlike Java, C# implements conditional compilation using preprocessor directives. It also provides a Conditional attribute to define methods that are only called when a given compilation constant is defined. This way, assertions can be provided as a framework feature with the method Debug.Assert(), which is only evaluated when the DEBUG constant is defined. Since version 1.4, Java provides a language feature for assertions, which are turned off at runtime by default but can be enabled using the "-enableassertions" or "-ea" switch when invoking the JVM. Namespaces and source files C#'s namespaces are similar to those in C++. Unlike package names in Java, a namespace is not in any way tied to location of the source file. While it's not strictly necessary for a Java source file location to mirror its package directory structure, it is the conventional organization. Both languages allow importing of classes (e.g., import java.util.* in Java), allowing a class to be referenced using only its name. Sometimes classes with the same name exist in multiple namespaces or packages. Such classes can be referenced by using fully qualified names, or by importing only selected classes with different names. To do this, Java allows importing a single class (e.g., import java.util.List). C# allows importing classes under a new local name using the following syntax: using Console = System.Console. It also allows importing specializations of classes in the form of using IntList = System.Collections.Generic.List. Java has a static import syntax that allows using the short name of some or all of the static methods/fields in a class (e.g., allowing foo(bar) where foo() can be statically imported from another class). C# has a static class syntax (not to be confused with static inner classes in Java), which restricts a class to only contain static methods. C# 3.0 introduces extension methods to allow users to statically add a method to a type (e.g., allowing foo.bar() where bar() can be an imported extension method working on the type of foo). The Sun Microsystems Java compiler requires that a source file name must match the only public class inside it, while C# allows multiple public classes in the same file, and puts no restrictions on the file name. C# 2.0 and later allows splitting a class definition into several files by using the partial keyword in the source code. In Java, a public class will always be in its own source file. In C#, source code files and logical units separation are not tightly related. Exception handling Java supports checked exceptions (in addition to unchecked exceptions). C# only supports unchecked exceptions. Checked exceptions force the programmer to either declare the exception thrown in a method, or to catch the thrown exception using a try-catch clause. Checked exceptions can encourage good programming practice, ensuring that all errors are dealt with. However Anders Hejlsberg, chief C# language architect, argues that they were to some extent an experiment in Java and that [17] [18] they haven't been shown to be worthwhile except in small example programs. One criticism is that checked exceptions encourage programmers to use an empty catch block (catch (Exception e) {}), which silently eats exceptions, rather than letting the exceptions propagate to a higher-level exception-handling routine. Another criticism of checked exceptions is that a new implementation of a method may cause unanticipated checked exceptions to be thrown, which is a contract-breaking change. This can happen in methods implementing an interface that only declares limited exceptions, or when the underlying implementation of a method changes. To allow for such unanticipated exceptions to be thrown, some programmers simply declare the method can throw any type of exception ("throws Exception"), which defeats the purpose of checked exceptions. In some cases however, exception chaining can be applied instead; re-throwing the exception in a wrapper exception. For example, if an object is changed to access a database instead of a file, an SQLException could be caught and re-thrown as an IOException, since the caller may not need to know the inner workings of the object.
Comparison of Java and C Sharp 79 There are also differences between the two languages in treating the try-finally statement. The finally is always executed, even if the try block contains control-passing statements like throw or return. In Java, this may result in unexpected behavior if the try block is left by a return statement with some value, and then the finally block that is executed afterwards is also left by a return statement with a different value. C# resolves this problem by prohibiting any control-passing statements like return or break in the finally block. A common reason for using try-finally blocks is to guard resource managing code, thus guaranteeing the release of precious resources in the finally block. C# features the using statement as a syntactic shorthand for this common scenario, in which the Dispose() method of the object of the using is always called. Finally blocks Java allows flow of control to leave the finally block of a try statement, regardless of the way it was entered. This can cause another control flow statement (such as return) to be terminated mid-execution. For example: int foo() { } try { return 0; } finally { } return 1; In the above code, the return statement within try block causes control to leave it, and therefore finally block is executed before the actual return happens. However, finally block itself performs a return as well; thus, the original return that caused it to be entered is not actually executed, and the above method returns 1 rather than 0. C# does not allow any statements which allow control flow to leave the finally block prematurely, except for throw. In particular, return is not allowed at all, goto is not allowed if the target label is outside the finally block, and continue and break are not allowed if the nearest enclosing loop is outside the finally block. Lower level code The Java Native Interface (JNI) feature allows Java programs to call non-Java code. However, JNI does require the code being called to follow several conventions and impose restrictions on types and names used. This means that an extra adaption layer between legacy code and Java is often needed. This adaption code must be coded in a non-Java language, often C or C++. Java Native Access (JNA) allows easier calling of native code that only requires writing Java code, but comes at a performance cost. In addition, third party libraries provide for Java-COM bridging, e.g. JACOB [19] (free), and J-Integra for COM [20] (proprietary). .NET Platform Invoke (P/Invoke) offers the same capability by allowing calls from C# to what Microsoft refers to as unmanaged code. Through metadata attributes the programmer can control exactly how the parameters and results are marshalled, thus avoiding the need for extra adaption code. P/Invoke allows almost complete access to procedural APIs (such as Win32 or POSIX), but limited access to C++ class libraries. In addition, .NET Framework also provides a .NET-COM bridge, allowing access to COM components as if they were native .NET objects. C# also allows the programmer to disable the normal type-checking and other safety features of the CLR, which then enables the use of pointer variables. When using this feature, the programmer must mark the code using the unsafe keyword. JNI, P/Invoke, and "unsafe" code are equally risky features, exposing possible security holes and application instability. An advantage of unsafe, managed code over P/Invoke or JNI is that it allows the programmer to continue to work in the familiar C# environment to accomplish some tasks that otherwise would require calling
- Page 31 and 32: Apache Harmony 27 Component Descrip
- Page 33 and 34: Apache Harmony 29 [10] Gilbert, Dav
- Page 35 and 36: Apache Shiro 31 Apache Shiro Develo
- Page 37 and 38: Java applet 33 there are frequently
- Page 39 and 40: Java applet 35 } } g.drawString("He
- Page 41 and 42: Java applet 37 The 2002 Sun - Micro
- Page 43 and 44: Java applet 39 [18] W3.org (http:/
- Page 45 and 46: Associació d'Usuaris de Java de Ca
- Page 47 and 48: Associació d'Usuaris de Java de Ca
- Page 49 and 50: Associació d'Usuaris de Java de Ca
- Page 51 and 52: Associació d'Usuaris de Java de Ca
- Page 53 and 54: Bean Scripting Framework 49 Bean Sc
- Page 55 and 56: CJAN 51 Ostensibly, CJAN.org failed
- Page 57 and 58: Chainsaw (log file viewer) 53 Chain
- Page 59 and 60: clone (Java method) 55 Alternatives
- Page 61 and 62: clone (Java method) 57 } } abstract
- Page 63 and 64: Comparison of Java and C Sharp 59 A
- Page 65 and 66: Comparison of Java and C Sharp 61 R
- Page 67 and 68: Comparison of Java and C Sharp 63 T
- Page 69 and 70: Comparison of Java and C Sharp 65 f
- Page 71 and 72: e="6.73"> = 42; = null; Comparison
- Page 73 and 74: Comparison of Java and C Sharp 69 L
- Page 75 and 76: Comparison of Java and C Sharp 71 I
- Page 77 and 78: Comparison of Java and C Sharp 73 t
- Page 79 and 80: Comparison of Java and C Sharp 75 N
- Page 81: Comparison of Java and C Sharp 77 L
- Page 85 and 86: Comparison of Java and C Sharp 81 C
- Page 87 and 88: Comparison of Java and C++ 83 C++ J
- Page 89 and 90: Comparison of Java and C++ 85 e="7.
- Page 91 and 92: Comparison of Java and C++ 87 the c
- Page 93 and 94: Comparison of Java and C++ 89 • D
- Page 95 and 96: Comparison of Java and C++ 91 Sever
- Page 97 and 98: Comparison of the Java and .NET pla
- Page 99 and 100: Comparison of the Java and .NET pla
- Page 101 and 102: Comparison of the Java and .NET pla
- Page 103 and 104: Comparison of the Java and .NET pla
- Page 105 and 106: Constant interface 101 } public sta
- Page 107 and 108: Java 4K Game Programming Contest 10
- Page 109 and 110: Java 4K Game Programming Contest 10
- Page 111 and 112: CookXml 107 Creators, setters, adde
- Page 113 and 114: Cougaar 109 Cougaar Cougaar is a Ja
- Page 115 and 116: Devoxx 111 Related links • Devoxx
- Page 117 and 118: Elastic Path 113 References [1] htt
- Page 119 and 120: Electronic Disturbance Theater 115
- Page 121 and 122: Event dispatching thread 117 • Sw
- Page 123 and 124: Facelets 119 The above code can be
- Page 125 and 126: final (Java) 121 } Sphere(double x,
- Page 127 and 128: FormEngine 123 FormEngine Developer
- Page 129 and 130: Generics in Java 125 Motivation for
- Page 131 and 132: Generics in Java 127 Pair grade440
Comparison of Java and C Sharp 78<br />
Conditional compilation<br />
Unlike Java, C# implements conditional compilation using preprocessor directives. It also provides a Conditional<br />
attribute to define methods that are only called when a given compilation constant is defined. This way, assertions<br />
can be provided as a framework feature with the method Debug.Assert(), which is only evaluated when the DEBUG<br />
constant is defined. Since version 1.4, Java provides a language feature for assertions, which are turned off at<br />
runtime by default but can be enabled using the "-enableassertions" or "-ea" switch when invoking the JVM.<br />
Namespaces and source files<br />
C#'s namespaces are similar to those in C++. Unlike package names in Java, a namespace is not in any way tied to<br />
location of the source file. While it's not strictly necessary for a Java source file location to mirror its package<br />
directory structure, it is the conventional organization.<br />
Both languages allow importing of classes (e.g., import java.util.* in Java), allowing a class to be referenced using<br />
only its name. Sometimes classes with the same name exist in multiple namespaces or packages. Such classes can be<br />
referenced by using fully qualified names, or by importing only selected classes with different names. To do this,<br />
Java allows importing a single class (e.g., import java.util.List). C# allows importing classes under a new local name<br />
using the following syntax: using Console = System.Console. It also allows importing specializations of classes in<br />
the form of using IntList = System.Collections.Generic.List.<br />
Java has a static import syntax that allows using the short name of some or all of the static methods/fields in a class<br />
(e.g., allowing foo(bar) where foo() can be statically imported from another class). C# has a static class syntax (not<br />
to be confused with static inner classes in Java), which restricts a class to only contain static methods. C# 3.0<br />
introduces extension methods to allow users to statically add a method to a type (e.g., allowing foo.bar() where bar()<br />
can be an imported extension method working on the type of foo).<br />
The Sun Microsystems Java compiler requires that a source file name must match the only public class inside it,<br />
while C# allows multiple public classes in the same file, and puts no restrictions on the file name. C# 2.0 and later<br />
allows splitting a class definition into several files by using the partial keyword in the source code. In Java, a public<br />
class will always be in its own source file. In C#, source code files and logical units separation are not tightly related.<br />
Exception handling<br />
Java supports checked exceptions (in addition to unchecked exceptions). C# only supports unchecked exceptions.<br />
Checked exceptions force the programmer to either declare the exception thrown in a method, or to catch the thrown<br />
exception using a try-catch clause.<br />
Checked exceptions can encourage good programming practice, ensuring that all errors are dealt with. However<br />
Anders Hejlsberg, chief C# language architect, argues that they were to some extent an experiment in Java and that<br />
[17] [18]<br />
they haven't been shown to be worthwhile except in small example programs.<br />
One criticism is that checked exceptions encourage programmers to use an empty catch block (catch (Exception e)<br />
{}), which silently eats exceptions, rather than letting the exceptions propagate to a higher-level exception-handling<br />
routine. Another criticism of checked exceptions is that a new implementation of a method may cause unanticipated<br />
checked exceptions to be thrown, which is a contract-breaking change. This can happen in methods implementing an<br />
interface that only declares limited exceptions, or when the underlying implementation of a method changes. To<br />
allow for such unanticipated exceptions to be thrown, some programmers simply declare the method can throw any<br />
type of exception ("throws Exception"), which defeats the purpose of checked exceptions. In some cases however,<br />
exception chaining can be applied instead; re-throwing the exception in a wrapper exception. For example, if an<br />
object is changed to access a database instead of a file, an SQLException could be caught and re-thrown as an<br />
IOException, since the caller may not need to know the inner workings of the object.