Java for C# Programmers 3: Operators & Flow Control

Operators

Mathematical Operators

Increment and decrement operators ++, -- work as in C#, including pre- and postfix notation.

The mathematical operators +, -, *, /, %, +=, -=, *=, /=, %= and the bitwise operators ~, &, |, ^, <<, >>, &=, |=, ^=, <<=, >>= work as expected.

In Java, there are two additional bitwise operators: >>> and >>>=.
>>> moves the bits to the right and fills all up with zeroes. In contrast, the >> operator fills all up with the sign bit.

// Java
int a = -128;
System.out.println(a >> 1);     // -64
System.out.println(a >>> 1);    // 2147483584

System.out.println(a >> 4);     // -8
System.out.println(a >>> 4);    // 268435448

a = -1;
System.out.println(a >> 1);     // -1
System.out.println(a >>> 1);    // 2147483647

a = 4;
System.out.println(a >> 1);     // 2
System.out.println(a >>> 1);    // 2

The effect of >>> is reachable in C# by using unsigned integers. Or put differently: The >>> is needed in Java because it doesn’t have unsigned integers.

// C#
int a = -128;
uint b = unchecked((uint) -128);
Console.WriteLine(a >> 1);  // -64
Console.WriteLine(b >> 1);  // 2147483584

Console.WriteLine(a >> 4);  // -8
Console.WriteLine(b >> 4);  // 268435448

a = -1;
b = unchecked((uint) -1);
Console.WriteLine(a >> 1);  // -1
Console.WriteLine(b >> 1);  // 2147483647

a = 4;
b = 4;
Console.WriteLine(a >> 1);  // 2
Console.WriteLine(b >> 1);  // 2

Logical Operators

The comparison operators >, >=, ==, <, <=, != are all the same.

Gotcha: In Java, string comparison with == does not compare the contents of the string. In C# it does.

String a = "abc";
String b = "ab";
b += "c";
if (a == b)     // false in Java, true in C#

The logical operators &&, ||, ^, ! work as expected.
In Java like in C# there are also available the boolean operators & and | which do not do short-circuit evaluation.

System.out.println(true | f());  // f will be calculated.  

Flow Control

If, Else And Switch

If, else, switch/case, ?:-operator are the same in Java as in C#, with these differences:

  • switch/case allows fallthrough in Java generally. In C#, fallthrough is allowed only when the label where fallthrough happens does not contain any code.
  • You can’t switch on long variables in Java.
  • switch/case allows strings in C#, in Java strings in switch/case are allowed only from Java 7 on.

Gotcha: Java treats the null-String in a switch/case in two completely different ways from C#.

string s = null;
switch(s)
{
    case null: Console.WriteLine("null"); break; 
    default: Console.WriteLine("default"); break;
}

The C# code above will print null. Without the case null part, it will print default.

In Java, the corresponding code including the case null statement will result in a case expressions must be constant expressions compiler error.

Without the case null statement, the code will compile. But at runtime, you’ll get a NullPointerException at the switch(s).

Loops And Goto

for, while, do while are all the same. Goto is not available in Java.

The foreach keyword does not exist in Java. Iterating over all of a collection is implemented with a special syntax in for.

// C#
foreach(Type k in array)
// Java
for(Type k: array) 

Java has break and continue with label, these features do not exist in C#.

Break With Label

OuterLoop: // Labels the following loop with 'OuterLoop'
for(int i = 0; i < 3; ++i)
{ 
    for(int j = 0; j < 5; ++j)
    {
        System.out.println("i: " + i + "   j: " + j);
        if(j == 1) 
            break OuterLoop;  // Breaks out of the scope with the 
                       // label OuterLoop. Or put differently: 
                       // jumps behind the end of the statement 
                       // labeled OuterLoop. 
    }
}

The output of the code above is

i: 0  j: 0

Continue With Label

AAA: // Labels the following loop with 'AAA'
for (int i = 0; i < 3; ++i) 
{
    for (int j = 0; j < 5; ++j)
    {
        System.out.println("i: " + i + "   j: " + j);
        if (j == 0)
            continue AAA; // Breaks all inner loops and 
                          // continues the loop labeled AAA.
    }
}

The output of the code above is

i: 0   j: 0
i: 1   j: 0
i: 2   j: 0