C# and Java C# and Java are both modern object-oriented languages C# came after Java and so it is more advanced in some ways C# has more functional characteristics (e.g., anonymous functions, closure, etc.) than Java In this brief introduction we will cover some small syntactic differences In chapter 9 of the textbook we will cover major features of C# that may be appear in Java
Unified Type System (C#) C# possesses a unified type system. This means it does not have primitive types as you know about in Java (e.g., int, character, etc.) Although syntactically some types may appear to be primitive (e.g., int) in C# they are true objects and inherit the Object type. If you were to use typeof() on an int enough times, you would get to Object.
Unified Type System (Java) Java does not have a unified type system in the sense that it possesses true primitives that are not objects Primitive types do not inherit Object's methods. Primitives in Java do, however, have wrapper classes that do inherit from Object. Example: new Integer(5);
Unsigned Types C# supports unsigned types. That is, any type that would ordinarily involve a sign bit will have an unsigned equivalent This basically trades the ability to represent negative numbers for a positive range that is twice as large. For example, considering an 8 bit byte, the signed range is -128 to 127 and the unsigned range is 0 to 255 Java does not support unsigned types
High Precision Floating Point (C#) C# supports a built-in primitive type called decimal that can represent high precision values. The decimal type is 128 bits in size. It can represent values in the range 10 28 to -10 28 It cannot represent decimals of arbitrary length
High Precision Floating Point (Java) Java has no primitive type like decimal to handle increased decimal precision, but possesses a built in class called BigDecimal that can handle this situation. (There is a BigInteger too.) BigDecimal allows for decimal values of any length and numeric value. Since values are saved like strings, numeric processing is very slow.
Type Lifting In C#, declaring a variable with a question mark after the type allows that variable to be assigned the null value. Variables declared this way gain a boolean field called HasValue. Java primitives lack this ability, but can be assigned null when instantiated in their wrapper classes. This is somewhat similar to the option type in F#
Type Lifting Example
Parameter Passing (C#) By default, C# passes things by value, but methods can be configured to allow pass by reference. This is done using the ref keyword.
Parameter Passing (Java) Java is a strictly pass by value language and does not support pass by reference behavior. It is possible to pass around a reference to an object and change the object in that way. In this case it is the reference (address of the object) that is passed by value rather than the object itself.
Method Overriding (C#) In C#, it is necessary to explicitly state which methods can be overridden within their source files. Any attempt to override a method not declared virtual in its source file will cause a syntax error. The overriding function must also be declared with the override keyword.
Method Overriding Example (C#)
Method Overriding (Java) In Java, every method is implicitly virtual and can be overridden at any time.
Multidimensional Arrays C# supports two different kinds of syntax for multidimensional arrays: Rectangular Arrays: int[,] x = new int[m,n]; This kind of array minimizes the number of pointers that must be followed and is more efficient. Jagged Arrays: int[][] x = new int[m][n]; Basically an array of arrays. Involves a pointer for each row in the array. This is the only type of multidimensional array Java supports.
Tuples, Pointers, and Structs C# supports both tuples and pointers while Java does not. However, since C# also supports structs, there is little reason to use tuples. Pointers are considered something of a security risk in C# Any code using them must be declared with the unsafe keyword. Structs function exactly as they do in C.
Enumerations The syntax for enumerated types is virtually identical between the two languages. The underlying structure is different though. In C#, enumerations are implemented with structs. An integer value is paired with a label. This makes comparisons in switch statements extremely efficient. Java makes the enumeration a class, which (considering classes are Java's answer to structs) is the same general idea, but is somewhat less efficient.
Enumeration Example (C#)
And More There are obviously more differences than the ones listed here. This is intended to be a quick introduction to some important differences that might otherwise be troublesome.