Introduction
Java Operators are an integral part of the Java programming language. Java operators are generally used to manipulate primitive data types. This article will explain Java operators and how java operators work with code examples. Java operators are classified and listed according to precedence order.
What are Java Operators?
Operators are special symbols that perform mathematical and logical operations on the operands and return the result. For example, the (+) operator symbol adds two or more operands and returns the addition. Several types of operators in Java are used to perform different operations.
Operators in Java
We can divide Java operators into the following seven major categories:
- Assignment Operator
- Arithmetic Operators
- Relational Operators
- Unary Operators
- Logical Operators
- Conditional(Ternary) Operators
- InstanceOf Operator
- Bitwise Operators
1) Java Assignment Operator
An assignment operator is one of the most common operators, which we use to assign a value on its right to the operand on its left. For example, the following code sample assigns the number 5 to an int a variable, and the string "Hello" is assigned to the string msg variable-
int a = 5;
String msg = "Hello";
The complete program is listed below.
Code Example
public class AssignmentOperators {
public static void main(String args []) {
int a = 5;
String msg= "Hello" ;
System.out.println(+a);
System.out.println(msg+" CSharpCorner");
}
}
The output of the above code generates the following output.
2) Java Arithmetic Operators
Arithmetic operators are used to achieving mathematical calculations like algebra. The following table lists arithmetic operators.
Operators |
Explanation. |
+ |
Addition (or concatenation ) operator |
- |
Subtraction operator. |
* |
Multiplication operator. |
/ |
Division operator. |
% |
Remainder operator. |
The complete program is listed below.
Code Example
public class ArithimeticOperators {
public static void main(String[] args) {
int p = 25;
int q = 15;
int res = p + q;
System.out.println("p + q = " + res);
res = p - q;
System.out.println("p - q = " + res);
res = p * q;
System.out.println("p * q = " + res);
res = p / q;
System.out.println("p / q = " + res);
res = p % 2;
System.out.println("p % 2 = " + res);
}
}
The output of the following code generates the following output.
3) Java Relational Operator
Relational operators are used to judging against the two operands and give results in a boolean form. The following table lists all the relational operators. There are two types of Relational operators-
Equality operators
The equality operators are the java relational operators that determine if one operand is equal to the other; it returns a boolean value. These operators can be used to compare the object's references—the following table list all the equality operators.
Operators |
Explanation. |
== |
Equal to |
!= |
Not equal to |
The complete program is listed below.
Code Example
public class EqualityOperators {
public static void main(String[] args) {
int p = 25;
int q = 35;
boolean res ;
res = p == q;
System.out.println("p == q : " + res);
res = p != q;
System.out.println("p != q : " + res);
}
}
The output of the above code generates the following output.
Comparison Operators
Like less than or greater than etc., Java comparison operators are used in Java to compare the values of two operands. The following table list all the comparison operators.
Operators |
Explanation. |
> |
greater than |
>= |
greater than equal to |
< |
less than |
<= |
less than equal to |
The complete program is listed below.
Code Example
public class ComparisonOperators {
public static void main(String[] args) {
int p = 25;
int q = 35;
boolean res;
res = p > q;
System.out.println("p is greater than q : " + res);
res = p >= q;
System.out.println("p is greater than or equal to q : " + res);
res = p < q;
System.out.println("p is less than q : " + res);
res = p <= q;
System.out.println("p is less than or equal to q : " + res);
}
}
The output of the above code generates the following output.
InstanceOf Operators
An instance operator is used as a comparison operator in Java. This operator tests whether the object of a class is an instance of a class or not and gives the result in a boolean value. The following table list all the instanceOf operator,
Operator |
Explanation. |
instanceOf |
instanceOf operator checks the type of the object. |
The complete program is listed below.
Code Example
public class InstanceOfOperator
{
public static void main(String[] args) {
String name = "Hello";
if (name instanceof String) {
System.out.println("Hello is an instance of a String class");
}
else
{
System.out.println("Hello is not an instance of a String class");
}
}
}
The output of the above code generates the following output.
4) Java Unary Operators.
Java Unary Operators are engaged in a single operand. The following table lists Java unary operators.
Operators |
Explanation. |
+ |
The unary plus operator; indicates a positive value. |
- |
The unary minus operator; indicates a negative value. |
++ |
Increment operator; increments a value by 1 |
-- |
The decrement operator decrements a value by 1 |
! |
Logical complement operator; return a boolean value |
The complete program is listed below.
Code Example
public class UnaryOperators {
public static void main(String[] args) {
int p = 20;
int res;
System.out.println( "p : " +p);
res = ++p;
System.out.println("++p = " + res);
res = p++;
System.out.println("p++ : "+res);
res = --p;
System.out.println("--p = " + res);
res = p--;
System.out.println("p-- = " + res);
}
}
The output of the above code generates the following output.
5) Java Logical Operators
Logical operators are used to doing logical operations in Java. It operates on two boolean values and returns a boolean value. The following table lists Java logical operators-
Operators |
Explanation. |
&& |
Logical AND operator;(When two operands are true, the condition gives true). |
|| |
Logical OR operator;(If any two operands are not true, then the condition is true). |
1 |
Logical NOT operator;(It uses to reverse the result. If the value is true, it returns false and vice versa). |
The complete program is listed below.
Code Example
public class LogicalOperators
{
public static void main(String[] args) {
boolean p = true;
boolean q = false;
boolean res = (p && q);
System.out.println("p && q = " + res);
res = (p || q);
System.out.println("p || q = " + res);
res = !p;
System.out.println("!p = " + res);
}
}
The output of the above code generates the following output.
6) Java Conditional Operators (Ternary Operators).
The Java conditional operators select one of two expressions for evaluation based on the first operands' value. It is also called the ternary operator because it takes three arguments. The following table lists Java ternary operators.
Operator |
Explanation. |
? : |
Ternary operator;(If the first operand is true, it returns the second operand. Else, it returns the third operand). |
The complete program is listed below.
Code Example
public class TernaryOperators
{
public static void main(String[] args) {
int p = 10;
int q = 20;
int res = (p > 10) ? p : q;
System.out.println("result 1 is: " + res);
res = (q > 10) ? p : q;
System.out.println("result 2 is: " + res);
}
}
The output of the above code generates the following output.
7) Java Bitwise Operators
Bitwise operators perform bit shift operations on integral types only and not other types—the following table lists bitwise operators.
Operators |
Explanation |
~ |
Unary bitwise complement; inverts a bit pattern |
<< |
signed left shift |
>> |
signed right shift |
>>> |
unsigned right shift |
& |
bitwise exclusive AND |
^ |
bitwise exclusive XOR |
| |
bitwise exclusive OR |
The complete program is listed below.
Code Example
public class BitwiseOper {
public static void main(String[] args) {
//Initial values
int a = 5;
int b = 7;
// bitwise and
// 0101 & 0111=0101 = 5
System.out.println("a&b = " + (a & b));
// bitwise or
// 0101 | 0111=0111 = 7
System.out.println("a|b = " + (a | b));
// bitwise xor
// 0101 ^ 0111=0010 = 2
System.out.println("a^b = " + (a ^ b));
// bitwise and
// ~0101=1010
// will give 2's complement of 1010 = -6
System.out.println("~a = " + ~a);
// can also be combined with
// assignment operator to provide shorthand
// assignment
// a=a&b
a &= b;
System.out.println("a= " + a);
}
}
The output of the above code generates the following output.
Summary
In this tutorial, we learned about various Java operators and how to use them in our Java programs to perform some operations.
If you require any clarification/suggestions on the article, please leave your questions and thoughts in the comment section below. Follow C# Corner to learn more new and amazing things about Java Programming or to explore more technologies.
Thanks for reading, and I hope you like it.