Operators in Java

Assignment, Arithmetic, and Unary Operators

The Simple Assignment Operator

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 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 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 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 Type Comparison Operator instanceof

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.