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:
- Arithmetic operators
- Unary Operators
- Assignment Operators
- Relational Operators
- Logical Operators
- Ternary Operators
- Bitwise Operators
- Instance of Operator[ps2id id=’Arithmetic’ target=”/]
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.
1 2 3 4 5 |
<strong>+</strong> additive operator (also used for String concatenation) <strong>-</strong> subtraction operator <strong>*</strong> multiplication operator <strong>/</strong> division operator <strong>% </strong> remainder operator[ps2id id='unary' target=''/] |
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.
1 2 3 4 5 6 7 8 9 10 11 |
<strong>+</strong> Unary plus operator; indicates positive value (numbers are positive without this, however) <strong>-</strong> Unary minus operator; negates an expression <strong>++</strong> Increment operator; increments a value by 1 <strong>--</strong> Decrement operator; decrements a value by 1 <strong>! </strong> Logical complement operator; inverts the value of a boolean[ps2id id='assignment' target=''/] |
The Assignment Operators
One of the most common operators that you’ll encounter is the simple assignment operator “=”.
1 2 3 |
int cadence = 0; int speed = 0; int gear = 1;[ps2id id='relational' target=''/] |
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.
1 2 3 4 5 6 |
<strong>== </strong> equal to <strong>!=</strong> not equal to <strong>></strong> greater than <strong>>= </strong> greater than or equal to <strong>< </strong> less than <strong><=</strong> less than or equal to[ps2id id='logical' target=''/] |
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.
1 2 |
<strong>&&</strong> Conditional-AND <strong>||</strong> Conditional-OR[ps2id id='ternary' target=''/] |
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:
1 |
</code>condition <code><strong>?</strong> if true <strong>:</strong> if false</code>[ps2id id='bitwise' target=''/] |
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:
1 2 3 4 5 6 |
& (Bitwise AND) | (Bitwise OR) ^ (Bitwise XOR) ~ (Bitwise Complement) >> (Left shift) << (Right shift) [ps2id id='instance' target=''/] |
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.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
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 { } |
Output
1 2 3 4 5 6 |
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.
For working example on this topic visit GitHub repo of codedbug