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.

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.

The Assignment Operators

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

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.

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.

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:

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:

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.

Example

Output

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

Leave a Reply

Contact Us
close slider
Contact Us

How can we help you with?