Java Operators
In this article, you'll learn everything about different types of operators in Java programming language, their syntax and how to use them with examples.Operators are special symbols (characters) that carry out operations on operands (variables and values). For example, + is an operator that performs addition.
In Java variables article, you learned to declare variables and assign values to variables. Now, you will learn to use operators to manipulate variables.
Assignment Operators
Assignment operators are used in Java to assign values to variables. For example,
int age;
age = 5;
The assignment operator assigns the value on its right to the variable on its left. Here, 5 is assigned to the variable age using = operator.
There are other assignment operators too. However, to keep things simple, we will learn other assignment operators later in this article.
Example 1: Arithmetic Operators
class AssignmentOperator {
public static void main(String[] args) {
int number1, number2;
// Assigning 5 to number1
number1 = 5;
System.out.println(number1);
// Assigning value of variable number2 to number1
number2 = number1;
System.out.println(number2);
}
}
When you run the program, the output will be:
5 5
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, devide etc.Operator | Meaning |
---|---|
+ | Addition (also used for string concatenation) |
- | Subtraction Operator |
* | Multiplication Operator |
÷ | Division Operator |
% | Remainder Operator |
Example 2: Arithmetic Operator
class ArithmeticOperator { public static void main(String[] args) { double number1 = 12.5, number2 = 3.5, result; // Using addition operator result = number1 + number2; System.out.println("number1 + number2 = " + result); // Using subtraction operator result = number1 - number2; System.out.println("number1 - number2 = " + result); // Using multiplication operator result = number1 * number2; System.out.println("number1 * number2 = " + result); // Using division operator result = number1 / number2; System.out.println("number1 / number2 = " + result); // Using remainder operator result = number1 % number2; System.out.println("number1 % number2 = " + result); } }When you run the program, the output will be:
number1 + number2 = 16.0 number1 - number2 = 9.0 number1 * number2 = 43.75 number1 / number2 = 3.5714285714285716 number1 % number2 = 2.0In above example, all operands used are variables. However, it's not necessary at all. Operands used in arithmetic operators can be literals as well. For example,
result = number1 + 5.2; result = 2.3 + 4.5; number2 = number1 -2.9;
Unary Operators
Unary operator performs operation on only one operand.Operator | Meaning |
---|---|
+ | Unary plus (not necessary to use since numbers are positive without using it) |
- | Unary minus; inverts the sign of an expression |
++ | Increment operator; increments value by 1 |
-- | decrement operator; decrements value by 1 |
! | Logical complement operator; inverts the value of a boolean |
Example 3: Unary Operator
class UnaryOperator { public static void main(String[] args) { double number = 5.2, resultNumber; boolean flag = false; System.out.println("+number = " + +number); // number is equal to 5.2 here. System.out.println("-number = " + -number); // number is equal to 5.2 here. // ++number is equivalent to number = number + 1 System.out.println("number = " + ++number); // number is equal to 6.2 here. // -- number is equivalent to number = number - 1 System.out.println("number = " + --number); // number is equal to 5.2 here. System.out.println("!flag = " + !flag); // flag is still false. } }When you run the program, the output will be:
+number = 5.2 -number = -5.2 number = 6.2 number = 5.2 !flag = trueSimple enough till now. However, there is a crucial difference while using increment and decrement operator as prefix and postfix. Consider this example,
Example 4: Unary Operator
class UnaryOperator { public static void main(String[] args) { double number = 5.2; System.out.println(number++); System.out.println(number); System.out.println(++number); System.out.println(number); } }When you run the program, the output will be:
5.2 6.2 7.2 7.2When System.out.println(number++); statement is executed, the original value is evaluated first. The number is increased only after that. That's why you are getting 5.2 as an output. Then, when System.out.println(number); is executed, the increased value 6.2 is displayed.
However, when System.out.println(++number); is executed, number is increased by 1 first before it's printed on the screen. Similar is the case for decrement -- operator.
Relational Operators
The equality and relational operators determines the relationship between two operands. It checks if an operand is greater than, less than, equal to, not equal to and so on. Depending on the relationship, it results to either true or false.Operator | Description | Example |
---|---|---|
== | equal to | 5 == 3 is evaluated to false |
!= | not equal to | 5 != 3 is evaluated to true |
> | greater then | 5 > 3 is evaluated to true |
< | less then | 5 < 3 is evaluated to false |
>= | greater then or equal to | 5 >= 5 is evaluated to true |
<= | less then or equal to | 5 <= 5 is evaluated to true |
Equality and relational operators are used in decision making and loops (which will be discussed later). For now, check this simple example.
Example 5: Equality and Relational Operators
class RelationalOperator { public static void main(String[] args) { int number1 = 5, number2 = 6; if (number1 > number2) { System.out.println("number1 is greater than number2."); } else { System.out.println("number2 is greater than number1."); } } }When you run the program, the output will be:
number2 is greater than number1.Here, we have used > operator to check if number1 is greater than number2 or not.
Since, number2 is greater than number1, the expression number1 > number2 is evaluated to false.
Hence, the block of code inside else is executed and the block of code inside if is skipped.
If you didn't understand the above code, don't worry. You will learn it in detail in Java if...else article.
For now, just remember that the equality and relational operators compares two operands and is evaluated to either true or false.
Logical Operators
The logical operators || (conditional-OR) and && (conditional-AND) operates on boolean expressions. Here's how they work.Operator | Description | Example |
---|---|---|
|| | conditional-OR; true if either of the boolean expression is true | false || true is evaluated to true |
&& | conditional-AND; true if all boolean expressions are true | false && true is evaluated to false |
Example 6: Logical Operators
class LogicalOperator { public static void main(String[] args) { int number1 = 1, number2 = 2, number3 = 9; boolean result; // At least one expression needs to be true for result to be true result = (number1 > number2) || (number3 > number1); // result will be true because (number1 > number2) is true System.out.println(result); // All expression must be true from result to be true result = (number1 > number2) && (number3 > number1); // result will be false because (number3 > number1) is false System.out.println(result); } }When you run the program, the output will be:
true false
Bitwise and Bit Shift Operators
To perform bitwise and bit shift operators in Java, these operators are used.Operator | Description |
---|---|
~ | Bitwise Complement |
<< | Left Shift |
>> | Right Shift |
>>> | Unsigned Right Shift |
& | Bitwise AND ^ |
^ | Bitwise exclusive OR |
| | Bitwise inclusive OR |
Assignment Operators
We have only discussed about one assignment operator = in the beginning of the article. Except this operator, there are quite a few assignment operators that helps us to write cleaner code.Operator | Description | Example |
---|---|---|
+= | x += 5 | x = x + 5 |
-= | x *= 5 | x = x * 5 |
/= | x /= 5 | x = x / 5 |
%= | x %= 5 | x = x / 5 |
<<= | x <<= 5 | x << 5 |
>>= | x >>= 5 | x = x >> 5 |
|= | x |= 5 | x = x | 5 |
^= | x ^= 5 | x = x ^ 5 |
&= | x &= 5 | x = x & 5 |
0 Comments