Operators in Java

Java provides a rich set of operators environment which are used to manipulate primitive data types. There are many types of operators in java which are given below:

The Arithmetic Operators

The Java programming language provides operators that perform addition, subtraction, multiplication, and division. There’s a good chance you’ll recognize them by their counterparts in basic mathematics. The only symbol that might look new to you is “%”, which divides one operand by another and returns the remainder as its result.

+       additive operator (also used for String concatenation)
-       subtraction operator
*       multiplication operator
/       division operator
      remainder operator

The Unary Operators

The unary operators require only one operand; they perform various operations such as incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean.

+       Unary plus operator; indicates 
        positive value (numbers are 
        positive without this, however)
-       Unary minus operator; negates
        an expression
++      Increment operator; increments
        a value by 1
--      Decrement operator; decrements
        a value by 1
      Logical complement operator; 
        inverts the value of a boolean

The Assignment Operators

One of the most common operators that you’ll encounter is the simple assignment operator “=”.

 int cadence = 0;
 int speed = 0;
 int gear = 1;

The Equality and Relational Operators

The equality and relational operators determine if one operand is greater than, less than, equal to, or not equal to another operand. The majority of these operators will probably look familiar to you as well. Keep in mind that you must use “==”, not “=”, when testing if two primitive values are equal.

==      equal to
!=      not equal to
>       greater than
>=      greater than or equal to
      less than
<=      less than or equal to

The Conditional/Logical Operators

The && and || operators perform Conditional-AND and Conditional-OR operations on two boolean expressions. These operators exhibit “short-circuiting” behavior, which means that the second operand is evaluated only if needed.

&& Conditional-AND
|| Conditional-OR

The Ternary Operators

The ternary operators is a shorthand version of the if-else statement. This operator consists of three operands and is used to evaluate Boolean expressions. The goal of the operator is to decide, which value should be assigned to the variable. The operator is written as:

condition ? if true : if false

The Bitwise Operators

There are various types of Bitwise operators which can be applied to the integer types, long, int, short, char, and byte. They are used for manipulation of individual bits of a number. Various types of Bitwise operators are as follows:

&          (Bitwise AND)
|          (Bitwise OR)
^          (Bitwise XOR)
~          (Bitwise Complement)
>>         (Left shift)
<<         (Right shift)                     

The Instance of Operators

The instanceof operator compares an object to a specified type. You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.

The following program, InstanceofDemo, defines a parent class (named Parent), a simple interface (named MyInterface), and a child class (named Child) that inherits from the parent and implements the interface.


class InstanceofDemo {
    public static void main(String[] args) {
        Parent obj1 = new Parent();
        Parent obj2 = new Child();
        System.out.println("obj1 instanceof Parent: "+ (obj1 instanceof Parent));
        System.out.println("obj1 instanceof Child: "+ (obj1 instanceof Child));
        System.out.println("obj1 instanceof MyInterface: "+ (obj1 instanceof MyInterface));
        System.out.println("obj2 instanceof Parent: "+ (obj2 instanceof Parent));
        System.out.println("obj2 instanceof Child: "+ (obj2 instanceof Child));
        System.out.println("obj2 instanceof MyInterface: "+ (obj2 instanceof MyInterface));
class Parent {
class Child extends Parent implements MyInterface {
interface MyInterface {


obj1 instanceof Parent: true
obj1 instanceof Child: false
obj1 instanceof MyInterface: false
obj2 instanceof Parent: true
obj2 instanceof Child: true
obj2 instanceof MyInterface: true

When using the instanceof operator, keep in mind that null is not an instance of anything.

Octocat - CodedBug github

For working example on this topic visit GitHub repo of codedbug