Basic Operators in Java
Operators are special symbols like +, -, *, /, %, etc, which are used to perform operations on operands (values) in Java.
List of Basic Operators in Java
Following is the list of basic operators in Java:
- Arithmetic Operators
- Assignment Operators
- Logical Operators
- Relational Operators
- Ternary Operator
- Unary Operator
- Bitwise Operators
Arithmetic Operator
Arithmetic operators are used to perform calculations, such as addition, subtraction, multiplication, and division.
Following is the list of Arithmetic operators in Java:
Operators | Name | Description | Example |
---|---|---|---|
+ | Addition | Adds values. | 10 + 5 |
- | Substraction | Substracts values. | 10 - 5 |
* | Multiplication | Multiplies values. | 2 * 5 |
/ | Division | Divides values. | 10 / 2 |
% | Modulus | Returns remainder after division. | 10 % 2 |
Example
Here is an example of a Java program to add values of two variables using the + (plus) Arithmetic operator:
public class ArithmeticOperatorExample {
public static void main(String[] args) {
int num1 = 100;
int num2 = 200;
int result = num1 + num2;
System.out.println(result);
}
}
Output
Assignment Operators
Assignment operators are used to assign a value to a variable.
Following is the list of assignment operators in Java:
Operators | Description | Example |
---|---|---|
= | Assign value from the right to a variable on the left. | a = 10 |
+= | Add right operand with the left operand and assign result to the left operand. | a += b |
-= | Substract the right operand from the left operand and assign the result to the left operand. | a -= b |
*= | Multiplies the operand on the right with the operand on the left and assign the result to the left operand. | a *= b |
/= | Divide the left operand by the right operand and assign the result to the left operand. | a /= b |
%= | Modulus and assign operator. It calulates the remainder out of two operand and assign the result to the left operand. | a %= b |
&= | Bitwise AND assignment operator. | a &= b |
^= | Bitwise exclusive OR and assignment operator. | a ^= b |
|= | Bitwise inclusive OR and assignment operator. | a |= b |
<<=< /td> | Left shift and assignment operator. | a <<= b |
>>= | Right shift and assignment operator. | a >>= b |
>>>= | Shift right zero fill and assignment operator. | a >>>= b |
Example
Here is an example of a Java program to assign values to variables using the = Assignment operator:
public class AssignmentOperatorExample {
public static void main(String[] args) {
String message1 = "Hello";
String message2 = message1;
int num1 = 10000;
int num2 = num1;
System.out.println(message2);
System.out.println(num1);
System.out.println(num2);
}
}
Relational Operators
Relational operators returns a boolean value (true/false) by comparing two variables or numbers.
Following is the list of relational operators in Java:
Operators | Name | Description | Example |
---|---|---|---|
== | Equal | Compares and return true if the left operand is equal to the right operand. | a == 10 |
!= | Not equal | Compares and return true if the left operand is not equal to the right operand. | a != 10 |
> | Greater than | Compares and return true if the left operand is greater than the right operand. | a > 10 |
< | Less than | Compares and return true if the left operand is lesser than the right operand. | a < 10 |
>= | Greater than or equal to | Compares and return true if the left operand is greater than or equal to the right operand. | a >= 10 |
<= | Less than or equal to | Compares and return true if the left operand is lesser than or equal to the right operand. | a <= 10 |
Example
Here is an example of a Java program to compare values of two variables using the Relational operators:
public class RelationalOperatorsExample {
public static void main(String[] args) {
int a = 10;
int b = 12;
boolean result1 = a == b;
boolean result2 = a != b;
boolean result3 = a > b;
boolean result4 = a < b;
boolean result5 = a <= b;
boolean result6 = a >= b;
System.out.println(result1);
System.out.println(result2);
System.out.println(result3);
System.out.println(result4);
System.out.println(result5);
System.out.println(result6);
}
}
Output
true
false
true
true
false
Logical Operators
Logical operators are used to determine the logic between variables(values).
Operators | Name | Description | Example |
---|---|---|---|
&& | Logical AND | Returns true, if and only if both statements are true. | 175 > 156 && 253 > 130 |
|| | Logical OR | Returns true, if one of the given statements is true. | 147 < 112 || 350> 330 |
! | Logical NOT | Change the result to the opposite, returns true if the result is false. | !(2 > 5) |
Example
Here is an example of a Java program to see how Logical operators work:
public class LogicalOperatorsExample {
public static void main(String[] args) {
int a = 10;
int b = 12;
int c = 30;
boolean result1 = a < b && b < c;
boolean result2 = a > b || b < c;
boolean result3 = !(a < b);
System.out.println(result1);
System.out.println(result2);
System.out.println(result3);
}
}
Output
true
false
Ternary Operator
A ternary operator takes three arguments (or operands) to decide, which value should be assigned to the variable. A ternary operator is also called conditional operator. It works as an inline if-else statement.
Syntax
(if this condition is true ? use this : else use this)
Example
public class TernaryOperatorsExample {
public static void main(String[] args) {
int a = 10;
int b = 12;
int c = 30;
int result1 = (a > b ? 1 : 2);
int result2 = (a < b ? 1 : 2);
boolean result3 = (a > c ? false : true);
System.out.println(result1);
System.out.println(result2);
System.out.println(result3);
}
}
Output
true
false
Unary Operator
In Java, the unary operators are used to increase or decrease the value of an operand by 1 or a given number. The unary operators have two forms: Postfix and Prefix. Both do increment or decrement in appropriate variables.
Operators | Name | Description | Example |
---|---|---|---|
i++ | Postfix Increment | Assigns the value of "i" in "a" and then increments the operand i by 1. | int a = i++; |
++i | Prefix Increment | Increments the value of operand "i" by 1 and then assigns the value of "i" to "a". | int a = ++i; |
i-- | Postfix Decrement | Assigns the value of "i" in "a" and then decreases the operand i by 1. | int a = i--; |
--i | Prefix Decrement | Decreases the value of operand "i" by 1 and then assigns the value of "i" to "a". | int a = --i; |
Example
public static void main(String[] args) {
int a = 20;
int b = 15;
++a;
--b;
System.out.println(a);
System.out.println(b);
}
Output
14
Bitwise Operators
Bitwise operators are used to perform manipulation on individual bits of integer types, long, int, short, byte and char values. Bitwise operations are generally faster than division, multiplication, and sometimes faster than addition.
Following are the bitwise operators in Java:
- Bitwise AND &: The bitwise & operator is used to perform a bitwise AND operation. It is a binary operator. It is denoted by & symbol. This operator returns 1 if both of the bits are 1, else it gives 0.
Example
public class BitwiseANDExample {
public static void main(String[] args) {
int a = 4;
int b = 7;
int result = a & b;
System.out.println(result);
}
}
Output
Example Explained
a = 4 (in decimal) = 0100 (in binary)
b = 7 (in decimal) = 0111 (in binary)
Bitwise AND Operation of 4 and 7
0100
& 0111
0100 = 4 (in decimal)
Example
public class BitwiseORExample {
public static void main(String[] args) {
int a = 4;
int b = 7;
int result = a | b;
System.out.println(result);
}
}
Output
Example Explained
a = 4 (in decimal) = 0100 (in binary)
b = 7 (in decimal) = 0111 (in binary)
Bitwise OR Operation of 4 and 7
0100
| 0111
0111 = 7 (in decimal)
Example
public class BitwiseXORExample {
public static void main(String[] args) {
int a = 4;
int b = 7;
int result = a ^ b;
System.out.println(result);
}
}
Output
Example Explained
a = 4 (in decimal) = 0100 (in binary)
b = 7 (in decimal) = 0111 (in binary)
Bitwise XOR Operation of 4 and 7
0100
^ 0111
0011 = 3 (in decimal)
Example
a = 4 (in decimal) = 0100 (in binary)
Bitwise Compliment Operation of 4
~ 0100
1011 = 11 (in decimal)
Two's complement of 11 will be -5
Example
14 (decimal value) = 00001110 (binary value)
(14 << 2) Move 14 by 2 places to the left
00001110
00111000 = 56 (in decimal) (After moving 00001110 by 2 places to the left.)
Example
14 (decimal value) = 00001110 (binary value)
(14 >> 2) Move 14 by 2 places to the right
00001110
00000011 = 3 (in decimal) (After moving 00001110 by 2 places to the right.)
Example
14 (decimal value) = 00001110 (binary value)
(14 >>> 2) Move 14 by 2 places to the unsigned right.
00001110
00000011 = 3 (in decimal) (After moving 00001110 by 2 places to the unsigned right.)