Comparison of C Sharp and Java

Both languages offer library-defined arbitrary-precision arithmetic types for arbitrary-size integers and decimal point calculations.

[24] In contrast, the C# System.DateTime is an immutable struct value type for date-and-time information with 100-nanosecond precision; the .NET 6 API also added System.DateOnly and System.TimeOnly, similar structures for date-only or time-only operations.

They can also be part of an object (either as a field or boxed), or stored in an array without the memory indirection that normally exists for class types.

As such, enumerations are one area where tools designed to automatically translate code between the two languages (such as Java to C# converters) fail.

Delegates were deliberately left out of Java because they were considered unnecessary and detrimental to the language, and because of potential performance issues.

The wrapper pattern, which resembles the delegates of C# in that it allows the client to access one or more client-defined methods through a known interface, is one such mechanism.

[citation needed] Another is the use of adapter objects using inner classes, which the designers of Java argued are a better solution than bound method references.

According to the Java spec, any attempt to dereference the null reference must result in an exception being thrown at run-time, specifically a NullPointerException.

The Java language designers reasoned that pointers are one of the main features that enable programmers to put bugs in their code and chose not to support them.

They are much like weak references, but the Java virtual machine (JVM) will not deallocate softly-referenced objects until the memory is needed.

An example of the latter can be found in how the extends and (especially) the super keywords were reused for specifying type bounds when generics were introduced in Java 1.5.

[54] Java does not include operator overloading, nor custom conversions to prevent abuse of the feature and to keep the language simple.

As a convention, these variable names are capital-only with words separated with an underscore but the Java language doesn't insist on this.

A parameter that is only final is not considered as a constant, although it may be so in the case of a primitive data type or an immutable class, like a String.

Instead, generators are typically defined by providing a specialized implementation of a well-known collection or iterable interface, which will compute each element on demand.

In C#, it is possible to enforce a reference with the ref keyword, similar to C++ and in a sense to C. This feature of C# is particularly useful when one wants to create a method that returns more than one object.

However Anders Hejlsberg, chief C# language architect, argues that they were to some extent an experiment in Java and that they have not been shown to be worthwhile except in small example programs.

In particular, the original requirement was "… there should be a clean, demonstrable migration path for the Collections APIs that were introduced in the Java 2 platform".

Java has use-site variance that allows a single generic class to declare members using both co- and contravariance.

This approach also has some differences compared to real closures, notably more controlled access to variables from the enclosing scopes: only final members can be referenced.

C#, unlike Java, allows the use of lambda functions as a way to define special data structures called expression trees.

While it is not strictly necessary for a Java source file location to mirror its package directory structure, it is the conventional organization.

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.

These language extensions included the notion of async methods and the await statement that make the program flow appear synchronous.

In addition, third party libraries provide Java-Component Object Model (COM) bridging, e.g., JACOB (free), and J-Integra for COM (proprietary).

Through metadata attributes the programmer can control exactly how the parameters and results are marshalled, thus avoiding the external glue code needed by the equivalent JNI in Java.

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.

JNI, P/Invoke, and "unsafe" code are equally risky features, exposing possible security holes and application instability.

C# allows library-defined types to be integrated with existing types and operators by using custom implicit/explicit conversions and operator overloading as illustrated by the following example: Java doesn't have this feature, although a similar effect is possible with the Optional class This example illustrates how Java and C# can be used to create and invoke an instance of class that is implemented in another programming language.

In addition to the conventional way, Java has GraalVM, a virtual machine capable to run any implemented programming language.