How do I use the shift left “<<" operator?

The signed shift left operator << shifts a bit pattern to the left. This operator operates with two operand, the left-hand operand to be shifted and the right-hand operand tells how much position to shift.

Shifting a 0010 bit pattern using the << operator 2 position will produce a 1000 bit pattern. The signed shift left operator produce a result that equals to multiplying a number by 2, which double the value of a number.

package org.kodejava.basic;

public class SignedLeftShiftOperator {
    public static void main(String[] args) {
        // The binary representation of 2 is "0010"
        int number = 2;
        System.out.println("number      = " + number);
        System.out.println("binary      = " +
                Integer.toBinaryString(number));

        // Using the shift left operator we shift the bits two
        // times to the left. This will shift the "0010" into
        // "1000"
        int shiftedLeft = number << 2;
        System.out.println("shiftedLeft = " + shiftedLeft);
        System.out.println("binary      = " +
                Integer.toBinaryString(shiftedLeft));
    }
}

The result of the code snippet:

number      = 2
binary      = 10
shiftedLeft = 8
binary      = 1000

How do I use the unary bitwise complement “~” operator?

The unary bitwise complement operator (~) inverts a bit pattern; it can be applied to any of the integral types, making every 0 a 1 and every 1 a 0.

For example, an integer contains 32 bits; applying this operator to a value whose bit pattern is 00000000000000000000000000001000 would change its pattern to 11111111111111111111111111110111.

package org.kodejava.basic;

public class UnaryBitwiseComplementOperator {
    public static void main(String[] args) {
        // An integer type contains 32 bit information.
        // 8 = 00000000000000000000000000001000
        int number = 8;
        System.out.println("number = " + number);
        System.out.println(Integer.toBinaryString(number));

        // Using the ~ operator inverts the number by change the
        // every "0" to "1" and every "1" to "0".
        // 00000000000000000000000000001000
        // 11111111111111111111111111110111
        //
        int invertedNumber = ~number;
        System.out.println("invertedNumber = " + invertedNumber);
        System.out.println(Integer.toBinaryString(invertedNumber));
    }
}

The result of the code snippet:

number = 8
1000
invertedNumber = -9
11111111111111111111111111110111

How do I convert double value into int value?

To convert double value into an int value we can use type casting or using the Double.intValue() method call. The code snippet below show you how to do it.

package org.kodejava.basic;

public class DoubleToInt {
    public static void main(String[] args) {
        Double numberA = 49.99;
        System.out.println("numberA = " + numberA);

        // Converting Double value to int value by calling
        // the Double.intValue() method.
        int numberB = numberA.intValue();
        System.out.println("numberB = " + numberB);

        // Converting Double value to int value by casting
        // the primitive double value of the Double instance
        int numberC = (int) numberA.doubleValue();
        System.out.println("numberC = " + numberC);

        double numberD = 99.99;
        System.out.println("numberD = " + numberD);

        // Converting double value into int value using
        // type casting
        int numberE = (int) numberD;
        System.out.println("numberE = " + numberE);
    }
}

How do I use the unary operators?

The unary operators requires only one operand to operate on, it can perform operations such as incrementing or decrementing value by one, negating a value or inverting a value of a boolean expression.

The unary operators use the following symbols:

Symbol Description
+ unary plus operator; indicates positive value
- unary minus operator; negates a value
++ unary increment operator; increments value by one
-- unary decrement operator; decrements value by one
! unary logical complement operator; inverts a boolean value
package org.kodejava.basic;

public class UnaryOperatorsDemo {
    public static void main(String[] args) {
        int result = +10;  // result = 10
        System.out.println("result = " + result);
        result--;          // result = 9
        System.out.println("result = " + result);
        result++;          // result = 10
        System.out.println("result = " + result);
        result = -result;  // result = -10;
        System.out.println("result = " + result);

        // The increment and decrement operators can be applied
        // before (prefix) or after (postfix) the operand. Both
        // of them will increment or decrement value by one. The
        // different is that the prefix version evaluates to the
        // incremented or decremented value while the postfix
        // version evaluates to the original value;
        --result;
        System.out.println("result = " + result);
        ++result;
        System.out.println("result = " + result);

        boolean status = result == -10;  // status = true
        System.out.println("status = " + status);
        status = !status;                // status = false;
        System.out.println("status = " + status);
    }
}

How do I use the arithmetic operators?

The following example show you how to use Java arithmetic operators. The operators consist of the multiplicative operators (* for multiplication, / for division), % for remainder of division) and the additive operators (+ for addition,- for subtraction).

You’ll also see we are using a combination of a simple assignment operator with the arithmetic operators to create compound assignments.

package org.kodejava.basic;

public class ArithmeticOperatorsDemo {
    public static void main(String[] args) {
        int result = 5 + 4;  // result = 9
        System.out.println("result = " + result);

        result = result - 2; // result = 7
        System.out.println("result = " + result);

        result = result * 4; // result = 28
        System.out.println("result = " + result);

        result = result / 7; // result = 4
        System.out.println("result = " + result);

        result = result % 3; // result = 1
        System.out.println("result = " + result);

        // Combining the arithmetic operators with a simple assignment
        // operators give us a compound assignment. We can write the
        // operation above in the following form. But as you can see
        // the above snippets is easier to read.
        result = 5 + 4; // result = 9
        System.out.println("result = " + result);

        result -= 2;    // result = 7
        System.out.println("result = " + result);

        result *= 4;    // result = 28
        System.out.println("result = " + result);

        result /= 7;    // result = 4
        System.out.println("result = " + result);

        result %= 3;    // result = 1
        System.out.println("result = " + result);
    }
}