In both Java and C# a semicolon must be used to end any single statement
and to separate multiple statements on a line
Both Java and C# have single-line comments that start with //; they also both
have multiple-line comments that start with /* and end with */
o C#s special documentation comments start with ///; Javas start with /**
and end with */
Naming conventions are somewhat different in Java and C#:
o Java methods begin with a lower case letter, where C# methods start
with a capital letter (toString vs. ToString, main vs. Main)
o Interfaces in C# usually begin with a capital I (the letter i), like
IComparable, Javas dont
o Although both Java and C# class names generally start with a capital
letter, a few built-in C# types start with a lower case letter: in particular
object and string; they must be called Object and String in Java
o In all other cases capitalization is the same in both languages; both use
both Camel case (starts with a lower case letter) and Pascal case (starts
with a capital) in names
Java programs start execution with the public static void main(String[]
args) method, whereas C# programs start with the similar Main() method; in
Java Main() must have a string[] parameter, which is a 0-length array if there
are no arguments passed in to the program when it starts
In both Java and C# variables must be declared to have one specified type, and
they can only be assigned values of that type or of a compatible type
o For Java primitive types and C# built-in value types the following
automatic promotions are possible: double float long int
short byte and also int char
Converting in the other direction requires a cast: int x = (int)
3.14159; // x == 3
o For both Java and C# reference types (objects) a derived or inherited
type can be assigned to a variable of a type of any of its ancestors, for
example Object oVar = "abc";
This includes any interfaces that the reference type implements
o In Java you cannot declare a variable to be of type var, each variable
must have an explicitly-designated type
Java and C# built-in data types:
o As mentioned above, Javas primitive and C#s built-in value data types
are the same
Java does not have the C# struct keyword to create additional
primitive types
o The numeric types are byte, short, int, long, float, and double; a
char represents a single Unicode character, and it is treated as a
number when combined with numeric values
Java does not have C# types signed sbyte and unsigned ushort,
uint, and ulong
The arithmetic operators are +, , *, /, and %, the modulus
operator; when / is used with two integer numeric
The Mono Project installation comes equipped with an interactive csharp shell
interpreter where you can try C# statements/expressions to see how they work
by following them with a semicolon ;
A similar interpreter / REPL that works pretty well for Java is Groovy Console
see this website:
http://forgetfulprogrammer.wordpress.com/2013/12/05/groovy-console-pythonlike-repl-for-java/.
Java and C# arrays are declared, initialized, and used in exactly the same way;
Java only provides multidimensional arrays as arrays of arrays: int[][] a =
new int[3][5];
Java provides data structures like C#s List<T> and Dictionary<TKey,
TValue>, implementations of interfaces List<T> and Map<K,V>, which are
contained in java.util.*. One standard Java List<T> implementation is
ArrayList<T>; a standard Java Map<K,V> implementation is
HashMap<K,V>.
o ArrayList<String> als = new ArrayList<>(); // an ArrayList that
can only contain Strings
Note: the diamond designator on the right side, <>, is only
in Java 7 and later
This can also be specified as List<String> als = new
ArrayList<>(); // as interface
o int alss = als.size(); // the number of elements in this list,
initially 0
o HashMap<String, Integer> hmsi = new HashMap<,>(); // a Map
with String keys and Integer values (ints are autoboxed to
Integers, and auto-unboxed as necessary)
This can also be specified as Map<String, Integer> hmsi =
new HashMap<,>();
o Int hmsis = hmsi.size(); // the number of key-value pairs in this
map, initially 0
Unlike C#, in Java, Lists and Maps cannot be indexed using square brackets
(array index notation)
Java and C# both have all of the standard comparison operators: ==, !=, <=,
>=, <, >; these produce the bool value true or false
o Unlike in C#, in Java == and != do not use the equals method, and only
test for reference equality; if you want to test for content equality,
always use equals. You may have to override equals in your classes to
get this to work; if you do, also override getHashCode!!
Both Java and C# use &&, ||, and ! to combine bool values, and && and ||
short-circuit: if the left-hand operand determines the resulting truth value then
the right operand is not evaluated
In both Java and C#, conditions must be written in parentheses, for example:
if (x < 3) {
System.out.println("x is less than 3"); x = x + 1;
}
o Both languages also provide else and use else if to avoid the need for
double indentation
Both Java and C# have a ternary operator: int y = (x < 3) ? -1 : +1; // y == -1
if x is less than 3
The Java and C# while, do-while, and for loops work essentially the same way
Java has an extended for loop, for (int x : intcollection) { }; the
corresponding C# loop is the foreach loop, foreach (int x in intcollection) {
}