How do I use the diamond syntax?

In Java 7 a new feature called diamond syntax or diamond operator was introduced. This diamond syntax <> simplify how we instantiate generic type variables. In the previous version of Java when declaring and instantiating generic types we’ll do it like the snippet below:

List<String> names = new ArrayList<String>();
Map<String, List<Integer>> map = new HashMap<String, List<Integer>>();

As you can see in the snippet, we were repeating our self by defining the generic type two times. We define the object type we’ll be stored in the List and the Map on both left and the right side. By using the diamond syntax the compiler will infer the type of the right side expression argument automatically. So in Java 7 we can write the above code snippet like this:

List<String> names = new ArrayList<>();
Map<String, List<Integer>> map = new HashMap<>();

This make our code simpler and more readable and by using the diamond syntax the compiler will ensure that we have the generic type safe checking available in our code. This will make any error due to type incompatibility captured at the compile time.

How do I use the || operator in Java?

The || operator or conditional OR operator operates on two boolean expressions. This operator exhibit “short-circuiting” behavior, which means that the second operand is evaluated only if needed.

The || operator evaluate only boolean values. For an OR (||) expression it will return true if either of the operand is true. If the first operand resolves true, then the second operand will not evaluated, because the complete expression will return true.

package org.kodejava.example.fundamental;

public class ConditionalORDemo {
    public static void main(String[] args) {
        // the second operand (5<3) is not evaluated, because the
        // first operand return true, the result of complete
        // expression will be true
        boolean a = (1 == 1) || (5 < 3);

        // the first operand return false, the second operand is
        // evaluated to check the result of the second expression.
        // If the second operand resolves to true, the complete
        // expression return true, otherwise return false.
        boolean b = (5 < 3) || (2 == 3);
        boolean c = (5 < 3) || (1 == 1);

        System.out.println("result a: " + a);
        System.out.println("result b: " + b);
        System.out.println("result c: " + c);
    }
}

The program prints the following output:

result a: true
result b: false
result c: true

How do I use the boolean negation (!) operator in Java?

The ! operator is a logical compliment operator. The operator inverts the value of a boolean expression.

package org.kodejava.example.fundamental;

public class NegationOperator {
    public static void main(String[] args) {
        // negate the result of boolean expressions
        boolean negate = !(2 < 3);
        boolean value = !false;

        System.out.println("result: " + negate);
        System.out.println("value : " + value);
    }
}

Here is the result of the program:

result: false
value : true