Contents - Cultural View
Contents - Cultural View Contents - Cultural View
Comparison of Java and C Sharp 66 size="7.15"> define a delegate terface SomeDelegate { boolean Invoke(String arg); a target class ass Target { public boolean TargetMethod(String arg) { // do something return true; } usage id DoSomething() { > // construct a target with the target method final Target target = new Target(); // capture the delegate for later invocation SomeDelegate dlg = new SomeDelegate() { public boolean Invoke(String arg) { return target.TargetMethod(arg); } }; // invoke the delegate boolean result = dlg.Invoke("argumentstring"); Lifted (nullable) types // define a delegate private delegate bool SomeDelegate(string resou } // a target class class Target { public bool TargetMethod(string arg) { // do something return true; } } // usage void DoSomething() { // construct a target with the target metho var target = new Target(); // capture the delegate for later invocatio SomeDelegate dlg = target.TargetMethod; // invoke the delegate bool result = dlg("argumentstring"); C# allows value/primitive/simple types to be "lifted" to allow the special null value in addition to the type's native values. A type is lifted by adding a ? suffix to the type name. Conversions are implicitly defined to convert between values of the base and the lifted type. The lifted type can be compared against null or it can be tested for HasValue. Also, lifted operators are implicitly and automatically defined based on their non-lifted base, where - with the exception of some boolean operators - a null argument will propagate to the result. Java does not support type lifting as a concept, but the fixed set of built-in primitive types all have corresponding (boxed) wrapper types which do support the null value by virtue of being reference types. However, Java operators such as + are not defined for these companion wrapper types. Instead Java will fall back to the primitive operator and will attempt to unbox the operands before invocation. If one or both of the operands are null, this unboxing will cause a NullPointerException to be thrown. The following example illustrates how the lifted C# operator propagates the null value of the operand where the corresponding code in Java will throw an exception. Java C#
e="6.73"> = 42; = null; Comparison of Java and C Sharp 67 int? a = 42; int? b = null; ill generate a runtime NullPointerException // c will receive the null value e the * operator will attempt to unbox the null value // because * is lifted and one of the operands = a * b; int? c = a * b; Not all C# lifted operators have been defined to propagate null unconditionally if one of the operands is null. Specifically, the boolean operators have been lifted to support ternary logic thus keeping impedance with SQL. Late-bound (dynamic) type C# features a late bound dynamic type which supports no-reflection dynamic invocation, interoperability with dynamic languages as well as ad-hoc binding to (for example) document object models. The dynamic type resolves member access dynamically at runtime as opposed to statically/virtual a compile time. The member lookup mechanism is extensible with traditional reflection as a fall-back mechanism. There are several use cases for the dynamic type in C#: • Less verbose use of reflection: By casting an instance to the dynamic type, members such as properties, methods, events etc. can be directly invoked on the instance without using the reflection API directly. • Interoperability with dynamic languages: The dynamic type comes with a hub-and-spoke support for implementing dynamically typed objects and common runtime infrastructure for efficient member lookup. • Creating dynamic abstractions on the fly: For instance, a dynamic object could provide simpler access to document object models such as XML- or XHTML documents. Java does not support a late-bound type. The use cases for C# dynamic type have different corresponding constructs in Java: • For dynamic late-bound by-name invocation of preexisting types, reflection should be used. • For interoperability with dynamic languages, some form of interoperability API specific to that language will have to be used. The Java Virtual Machine platform does have multiple dynamic languages implemented on top if it, but there is no common standard for how to pass objects between languages. Usually this will involve some form of reflection or reflection-like API. As an example of how to use JavaFX objects from Java, see How to Use JavaFX in Your Swing Application [1] . • For creating and interacting with objects entirely at runtime, e.g. interaction with a document object model abstraction, a specific abstraction API will have to be used. Pointers C# allows use of pointers and corresponding pointer arithmetic. Pointers and pointer-arithmetic are potentially unsafe in a managed environment as they can be used to bypass the strict rules for object access. C# addresses that concern by requiring that code blocks or methods that use the feature be marked with the unsafe keyword, so that all clients of such code can be aware that the code may be less secure than otherwise. The compiler requires the /unsafe switch to allow compilation of a program that uses such code, and assemblies containing unsafe code may only execute if explicitly granted security permissions. Generally, unsafe code is either used to allow better interoperability with unmanaged APIs or system calls (which are inherently "unsafe"), or for performance reasons. Java does not permit pointers or pointer-arithmetic within the Java runtime environment and native interop is handled externally through JNI or other mechanisms.
- Page 19 and 20: Java (programming language) 15 [41]
- Page 21 and 22: AgentSheets 17 year, per school. Of
- Page 23 and 24: AgentSheets 19 References [1] Cyber
- Page 25 and 26: Lightweight Java 21 Lightweight Jav
- Page 27 and 28: .properties 23 See also • java.ut
- Page 29 and 30: Apache Harmony 25 Sun answered on a
- 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: Comparison of Java and C Sharp 65 f
- 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 and 82: Comparison of Java and C Sharp 77 L
- Page 83 and 84: Comparison of Java and C Sharp 79 T
- 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
e="6.73"><br />
= 42;<br />
= null;<br />
Comparison of Java and C Sharp 67<br />
<br />
int? a = 42;<br />
int? b = null;<br />
ill generate a runtime NullPointerException<br />
// c will receive the null value<br />
e the * operator will attempt to unbox the null value // because * is lifted and one of the operands<br />
= a * b; <br />
int? c = a * b;<br />
<br />
Not all C# lifted operators have been defined to propagate null unconditionally if one of the operands is null.<br />
Specifically, the boolean operators have been lifted to support ternary logic thus keeping impedance with SQL.<br />
Late-bound (dynamic) type<br />
C# features a late bound dynamic type which supports no-reflection dynamic invocation, interoperability with<br />
dynamic languages as well as ad-hoc binding to (for example) document object models. The dynamic type resolves<br />
member access dynamically at runtime as opposed to statically/virtual a compile time. The member lookup<br />
mechanism is extensible with traditional reflection as a fall-back mechanism.<br />
There are several use cases for the dynamic type in C#:<br />
• Less verbose use of reflection: By casting an instance to the dynamic type, members such as properties, methods,<br />
events etc. can be directly invoked on the instance without using the reflection API directly.<br />
• Interoperability with dynamic languages: The dynamic type comes with a hub-and-spoke support for<br />
implementing dynamically typed objects and common runtime infrastructure for efficient member lookup.<br />
• Creating dynamic abstractions on the fly: For instance, a dynamic object could provide simpler access to<br />
document object models such as XML- or XHTML documents.<br />
Java does not support a late-bound type. The use cases for C# dynamic type have different corresponding constructs<br />
in Java:<br />
• For dynamic late-bound by-name invocation of preexisting types, reflection should be used.<br />
• For interoperability with dynamic languages, some form of interoperability API specific to that language will<br />
have to be used. The Java Virtual Machine platform does have multiple dynamic languages implemented on top if<br />
it, but there is no common standard for how to pass objects between languages. Usually this will involve some<br />
form of reflection or reflection-like API. As an example of how to use JavaFX objects from Java, see How to Use<br />
JavaFX in Your Swing Application [1] .<br />
• For creating and interacting with objects entirely at runtime, e.g. interaction with a document object model<br />
abstraction, a specific abstraction API will have to be used.<br />
Pointers<br />
C# allows use of pointers and corresponding pointer arithmetic. Pointers and pointer-arithmetic are potentially<br />
unsafe in a managed environment as they can be used to bypass the strict rules for object access. C# addresses that<br />
concern by requiring that code blocks or methods that use the feature be marked with the unsafe keyword, so that all<br />
clients of such code can be aware that the code may be less secure than otherwise. The compiler requires the /unsafe<br />
switch to allow compilation of a program that uses such code, and assemblies containing unsafe code may only<br />
execute if explicitly granted security permissions. Generally, unsafe code is either used to allow better<br />
interoperability with unmanaged APIs or system calls (which are inherently "unsafe"), or for performance reasons.<br />
Java does not permit pointers or pointer-arithmetic within the Java runtime environment and native interop is<br />
handled externally through JNI or other mechanisms.