This tutorial explains how to use operators in Java programs. It is one of the essential topics for you to learn and use in real Java programming.
How to Use Operators in Java Programs
The tutorial has the following sections to help you learn quickly.
What are Operators in Programming?
An operator is a symbol or a character that acts as an inline function.
What Type of Operators Does Java Support?
Java supports several operators to manipulate data, generally stored in variables. You can use them to perform operations on variables.
You can perform the operations on hard-coded values too. The following types of operators have support in Java:
Arithmetic Operators
As the name signifies, arithmetic operators let you do arithmetic operations on variables of primitive data types.
These include:
· + (Addition) · - (Subtraction) · * (Multiplication) · / (Division) · % (Modulo) To store the remainder after the division of two numbers
Example:
int a = 10; int b = 20; int c = a + b; // var c will be 30 int d = b - a; // var d will evaluate to 10 int e = a * b; // var e will store 200 int f = b / a; // var f will result in 2 int g = b % a; // var g will be 0
Must Read – Data Types in Java
Logical Operators
In Java, you can get these operators to perform logical AND, NOT, and OR operations similar to the ones in digital electronics.
AND and OR, both use binary operands whereas the NOT uses unary.
The AND and OR operators skip the second condition if the first evaluates to false. We call it the short-circuiting effect.
Example:
boolean a = true; boolean b = false; boolean c = !a; // var c becomes false, the invert of a boolean d = a && b; // var d will come to false as one of the a and b is false. boolean e = a || b; // var e will come to true as one of the a and b is true.
Bitwise Operators
Bitwise operators provide a mechanism to manipulate the individual bits of a number.
You can use them with integer data types. They include:
& (Bitwise AND) | (Bitwise OR) ^ (Bitwise XOR) ~ (Bitwise Complement)
Example:
int a = 4; int b = 2; int c = a & b; // var c will get a 0 (0000) int d = a ^ b; // var d will get a 6 (0110) int e = a | b; // var e will also get a 6 (0110) int f = ~b; // var f will get a 13 (1101)
Relational Operators
Relational operators let you perform relations operations like equality, greater than, less than, less than equal to, etc.
These operators return a boolean value (true/false) in Java and can compare integer and floating data types.
Example:
int a = 1; int b = 2; int c = 3; int d = a == b; // var d will get to false int e = a < b; // var e will get to true int f = a > b; // var f will get to false int g = c >= b; // var g will get to true int h = c != a; // var h will get to true
Assignment Operators
Assignment operators let you store the right operand or the result in the left operand.
Java allows them to group other operators (generally arithmetic) to form a compound statement.
Example:
int a = 1; // simple assignment int b = 0; b += 2; // compound statement
Shift Operators
Java supports shift operators. They help you shift the bits of a number in either direction left or right as per the value mentioned. The operator brings a similar effect as you get from multiplication and division.
These include:
<< (Left shift) To shift the bits of a number to the left >> (Signed Right shift) To shift the bits of a number to the right. The leftmost bit depends on the sign of the initial number. >>> (Unsigned Right shift) To shift the bits of a number to the right. The leftmost bit is set to 0.
Example:
int a = 60; // a gets stroed as 0011 1100 int b = a << 2; // b gets the value 240 (1111 0000) int c = a >> 2; // c gets the value 15 (11111) int d = a >>> 2; // d gets the value 15 (0000 1111)
Unary Operators
Unary operators need only one operand. Operands are the values that act as input in the operations. These operators have the following syntax in Java:
++ Increment by one -- Decrement by one
Example:
int a = 1; // simple assignment int b = a++; // b will become 2 and a will also increment by 1, i.e., 2 int c = a--; // c will evaluate to 1 and a will also decrement by 1 and will become 1
Ternary Operators
It is a conditional operator like a shorthand version for if-else. It includes three operands.
This operator has the following syntax in Java:
Condition? If true: if false
Example:
int a = 1; int b = 2; int c = a > b ? 3 : 4; // c will get 4 as the result as the condition will turn false. int d = a < b ? 3 : 4; // d will get 3 as the result as the condition will turn true.
instanceof Operator
This operator can check whether an object belongs to a particular class or interface.
It uses the following syntax:
(Object name) instanceof (class/interface type)
After the check, if the object turns out to be of the class or interface type, the operator returns success, i.e., true.
Example:
String val = "Hello"; System.out.print(val instanceof String);
The above Java statement uses the instanceof operator and printing true.
Must Read – Write Your First Java Program
Precedence of Operators
Operator priority or precedence defines the order for evaluating an expression. It helps in ascertaining which operation to execute first.
Firstly, Java evaluates operators with a higher priority and later processes those with lower precedence.
If two operators have the same priority order, they get assessed from left to right except for the exception of the assignment operator.
Our site needs your support to remain free. Share this tutorial on social media (Linkedin/Twitter) if you gained some knowledge.
Enjoy coding,
TechBeamers.