Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Comparison of C Sharp and Java
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Additional features== ===Numeric applications=== To adequately support applications in the field of mathematical and financial computation, several language features exist.<ref name="computation">{{cite web |url=http://www.pds.ewi.tudelft.nl/pubs/papers/scicomp01.pdf |title=Java for Scientific Computation : Prospects and Problems |publisher=Pds.ewi.tudelft.nl |access-date=24 February 2015 |archive-url=https://web.archive.org/web/20070922004052/http://www.pds.ewi.tudelft.nl/pubs/papers/scicomp01.pdf |archive-date=22 September 2007 |url-status=dead |df=dmy-all}}</ref> Java's [[strictfp]] keyword enables strict floating-point calculations for a region of code. Strict floating-point calculations require that even if a platform offers higher precision during calculations, intermediate results must be converted to single/double. This ensures that strict floating-point calculations return exactly the same result on all platforms. Without strict floating-point, a platform implementation is free to use higher precision for intermediate results during calculation. C# allows an implementation for a given hardware architecture to always use a higher precision for intermediate results if available, i.e. C# does not allow the programmer to optionally force intermediate results to use the potential lower precision of single/double.<ref>{{cite web |title=C# Language Specification Version 5.0 |url=http://www.microsoft.com/en-us/download/details.aspx?id=7029 |quote=Floating-point operations may be performed with higher precision than the result type of the operation. For example, some hardware architectures support an "extended" or "long double" floating-point type with greater range and precision than the double type, and implicitly perform all floating-point operations using this higher precision type. Only at excessive cost in performance can such hardware architectures be made to perform floating-point operations with ''less'' precision, and rather than require an implementation to forfeit both performance and precision, C# allows a higher precision type to be used for all floating-point operations. Other than delivering more precise results, this rarely has any measurable effects. However, in expressions of the form x*y/z, where the multiplication produces a result that is outside the double range, but the subsequent division brings the temporary result back into the double range, the fact that the expression is evaluated in a higher range format may cause a finite result to be produced instead of an infinity. |publisher=Microsoft |access-date=28 October 2013 |at=4.1.6 Floating point types}}</ref> Although Java's floating-point arithmetic is largely based on IEEE 754 (Standard for Binary Floating-Point Arithmetic), certain features are unsupported even when using the strictfp modifier, such as Exception Flags and Directed Roundings, abilities mandated by IEEE Standard 754 (see [[Criticism of Java#Floating point arithmetic|Criticism of Java, Floating point arithmetic]]). C# provides a built-in decimal type,<ref>{{cite web |title=decimal vs. 110 |url=http://msdn.microsoft.com/en-us/library/system.decimalv=vs.110.aspx |access-date=24 February 2015}}{{dead link |date=June 2016|bot=medic}}{{cbignore|bot=medic}}</ref> which has higher precision (but less range) than the Java/C# double. The decimal type is a 128-bit data type suitable for financial and monetary calculations. The decimal type can represent values ranging from 1.0 Γ 10<sup>β28</sup> to approximately 7.9 Γ 10<sup>28</sup> with 28β29 significant digits.<ref>{{cite web |title=C# Language Specification Version 5.0 |url=http://www.microsoft.com/en-us/download/details.aspx?id=7029 |publisher=Microsoft | at=4.1.7 The decimal type}}</ref> The structure uses C# operator overloading so that decimals can be manipulated using operators such as {{mono|+}}, {{mono|-}}, {{mono|*}} and {{mono|/}}, like other primitive data types. The {{Javadoc:SE|java/math|BigDecimal}} and {{Javadoc:SE|java/math|BigInteger}} types provided with Java allow arbitrary-precision representation of decimal numbers and integer numbers, respectively. Java standard library does not have classes to deal with complex numbers. The {{csharp|BigInteger}},<ref name="microsoft1"/> and {{csharp|Complex}}<ref>{{cite web |title=Complex |url=http://msdn.microsoft.com/en-us/library/system.numerics.complexv=vs.110.aspx |access-date=24 February 2015}}{{dead link |date=June 2016|bot=medic}}{{cbignore|bot=medic}}</ref> types provided with C# allow representation and manipulation of arbitrary-precision integers and complex numbers, respectively. The structures use C# operator overloading so that instances can be manipulated using operators such as {{mono|+}}, {{mono|-}}, {{mono|*}}, and {{mono|/}}, like other primitive data types. C# standard library does not have classes to deal with arbitrary-precision floating point numbers (see [[Arbitrary-precision arithmetic#Arbitrary-precision software|software for arbitrary-precision arithmetic]]). C# can help mathematical applications with the {{csharp|checked}} and {{csharp|unchecked}} operators that allow the enabling or disabling of run-time checking for [[arithmetic overflow]] for a region of code. === Language integrated query (LINQ) === {{Further|Language Integrated Query}} C#s [[Language Integrated Query]] (LINQ) is a set of features designed to work together to allow in-language querying abilities and is a distinguishing feature between C# and Java. LINQ consists of these features: * Extension methods allow existing interfaces or classes to be extended with new methods. Implementations can be shared or an interface can have a dedicated implementation. * Lambdas allow for expression of criteria in a functional fashion. * Expression trees allow a specific implementation to capture a lambda as an [[abstract syntax tree]] rather than an executable block. This can be utilized by implementations to represent criteria in a different language, e.g. in the form of an SQL where clause as is the case with e.g. [[Language Integrated Query|Linq, LINQ to SQL]]. * Anonymous types and type inference supports capturing and working with the result type of a query. A query may both join and project over query sources that may lead to a result type that cannot be named. * Query expressions to support a syntax familiar to [[SQL]] users. * Nullable (lifted) types to allow for a better match with query providers that support nullable types, like e.g. [[SQL]]. === Native interoperability === {| class="wikitable" style="width:80%;" |- ! style="width:40%;"| Native interoperability !! style="width:30%;"|Java !! style="width:30%;"|C# |- |[[Cross-language interoperability]] || {{yes}} (with [[GraalVM]], [[Nashorn (JavaScript engine)|Nashorn]], [[CORBA]], [[JNI]] or [[Java Native Access|JNA]])<ref name="cross-plattform" /> || {{yes}}; C# was designed for it<ref name="cross-plattform">{{cite web |url=http://www.25hoursaday.com/ |title=A Comparison of Microsoft's C# Programming Language to Sun Microsystems' Java Programming Language: C. An Ever So Slight Feeling of DΓ¨jΓ Vu: 15. Cross Language Interoperability |author=Dare Obasanjo |year=2007 |publisher=Dare Obasanjo |archive-url=https://web.archive.org/web/20120919093308/http://25hoursaday.com/ |archive-date=19 September 2012 |quote=There are a number of ways cross language interoperability works in Java. First of all, there is the Java Native Interface (JNI) β¦ Java also has the ability to interact with distributed objects that use the common object request broker architecture (CORBA) via Java IDL. β¦ C# and the .NET runtime were created with seamless cross-language interoperability as a design goal. |access-date=10 September 2012 |url-status=dead |df=dmy-all}}</ref> |- |External/native methods || {{yes}} || {{yes}} |- |[[Marshalling (computer science)|Marshalling]] || {{no|External glue code needed}} || {{yes}}; metadata controlled |- |[[Pointer (computer programming)|Pointers]] and arithmetics || {{no}}; but see [http://mishadoff.com/blog/java-magic-part-4-sun-dot-misc-dot-unsafe/ sun.misc.Unsafe] || {{yes}} |- |Native types || {{yes}}<ref>{{cite web |url=https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/types.html |title=JNI Types and Data Structures |publisher=Docs.oracle.com |access-date=9 April 2020}}</ref> || {{yes}} |- |Fixed-size buffers || {{no}} || {{yes}} |- |Explicit stack allocation || {{no}} || {{yes}} |- |Address-of || {{no}} || {{yes}} |- |[[Object pinning#Moving vs. non-moving|Object pinning (fix variable to address)]] || {{no}} || {{yes}} |- |Function pointers || {{no}} || {{yes}}<ref>{{cite web |url=https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-9#performance-and-interop |title=Function pointers in C# 9 |publisher=docs.microsoft.com |access-date=27 February 2021}}</ref> |- |Unions || {{no}} || {{yes}}<ref>{{cite web |url=https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/attributes/how-to-create-a-c-cpp-union-by-using-attributes |title=Creating C/C++ unions in C# |publisher=docs.microsoft.com |access-date=27 February 2021}}</ref> |} 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 imposes 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 [[Library (computing)|libraries]] provide Java-[[Component Object Model]] (COM) bridging, e.g., JACOB ([[Free software|free]]), and J-Integra for COM ([[Proprietary software|proprietary]]). .NET Platform Invoke ([[Platform Invocation Services|P/Invoke]]) offers the same ability by allowing calls from C# to what Microsoft terms un[[managed code]]. Through metadata attributes the programmer can control exactly how the parameters and results are [[Marshalling (computer science)|marshalled]], thus avoiding the external glue code needed by the equivalent JNI in Java. 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 first-class .NET objects. C# also allows the programmer to disable the normal type-checking and other safety features of the [[Common Language Runtime|CLR]], which then enables the use of [[Pointer (computer programming)|pointer variables]]. When using this feature, the programmer must mark the code using the {{mono|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 out to unmanaged code. An assembly (program or library) using unsafe code must be compiled with a special switch and will be marked as such. This enables runtime environments to take special precautions before executing potentially harmful code. === Runtime environments === Java (the programming language) is designed to execute on the Java platform via the [[Java Runtime Environment]] (JRE). The Java platform includes the [[Java virtual machine]] (JVM) and a common set of libraries. The JRE was originally designed to support interpreted execution with final compiling as an option. Most JRE environments execute fully or at least partially compiled programs, possibly with [[adaptive optimization]]. The Java compiler produces [[Java bytecode]]. Upon execution the bytecode is loaded by the Java runtime and either interpreted directly or compiled to machine instructions and then executed. {{Further|Java Runtime Environment}} C# is designed to execute on the [[Common Language Runtime]] (CLR). The CLR is designed to execute fully compiled code. The C# compiler produces [[Common Intermediate Language]] instructions. Upon execution the runtime loads this code and compiles to machine instructions on the target architecture. {{Further|Common Language Runtime}}
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)