I’ve just had convert some C# into Java and have noticed the following differences between these two languages (that are often thought to be so similar as to make no difference). In no particular order:
- Java uses packages, C# uses namespaces
- Java uses “import” to bring in classes, C# uses “using”
- Java classes (public ones) must be in a .java file with the same name as the class, C# doesn’t care
- Java Strings must be Strings, C# has strings as well
- Java doesn’t have ‘out’ parameters
- Java functions must declare if they throw exceptions, C# doesn’t care
- Java doesn’t have unsigned integer types, C# does
- Java system namespaces start with “java.*”, C#’s start with “System.*”
- Java doesn’t have support for accessor/setter methods in the language, C# does
- Java uses words like “implements” and “extends” for inheritance, C# uses the : operator
- Java doesn’t have a .TryParse method, C# does
- Java has final, C# has const and readonly
- Java can’t use enum constants in switch statements, C# can
-
Java can’t use [] on a String to access chars, C# can.
Not a problem for the current exercise, but other differences are:
- Java misses partial classes
- Java misses lamda functions (although there are proposals to include Lamda functions in Java BGGA)
- Java (out of the box) doesn’t have some of the functional features of C#
There are only a couple of things I would conclude from this
- Don’t assume that C# is a superior language just because it has loads more bling features
- Don’t underestimate the task of converting non-trivial C# classes to Java