Operators

Operators are used to perform operations on variables and values. In this tuturial, we categorize the operators in the following groups:

  • Arithmetic operators
  • Unary operators
  • Equality and relational operators
  • Logical  operators
  • instanceof operator

Arithmetic Operators

Operator Description Example Result
+ Additive operator (also used for String concatenation) 2 + 3 5
Subtraction operator 5 – 3 2
* Multiplication operator 4 * 5 20
/ Division operator 4/2 2
% Remainder operator (Modulus) 15 % 4 3

The following class to test the arithmetic operators.

Run The Application

Right click to the ArithmeticDemo class; select Run As -> Java Application.

Output

The Unary Operators

A unary operator is operation with only 1 operand. For example, incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean.

Operator Description Example Result
+ Unary plus operator; indicates positive value (numbers are positive without this, however) +1 1
Unary minus operator; negates an expression -1 -1
++ Increment operator; increments a value by 1 ++1 2
Decrement operator; decrements a value by 1 –1 0
! Logical complement operator; inverts the value of a boolean !true false

The increment/decrement operators can be applied before (prefix) or after (postfix) the operand.

The following program tests the unary operators:

Run The Application

Right click to the UnaryDemo class; select Run As -> Java Application.

Output

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.

Operator Description Example Result
== equal to 1 == 2 true
!= not equal to 1!=2 True
> greater than 1>2 False
>= greater than or equal to 2>=1 true
< less than 1<2 True
<= less than or equal to 1<=2 true

The following program tests the comparison operators:

Run The Application

Right click to the ComparisonDemo class; select Run As -> Java Application.

Output

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.

The following program tests these operators:

Run The Application

Right click to the LogicalOperator class; select Run As -> Java Application.

Output

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 programs define a parent class (named Parent), a simple interface (named TestInterface), and a child class (named Child) that inherits from the parent and implements the interface.

Run The Application

Right click to the InstanceofDemo class; select Run As -> Java Application.

Output