In this section, we will learn what operators are and how to use them in Java.
What Is an Operator in Java? Symbol in Java
Symbols like *, /, +, == etc. in Java programming are called operators and allow developers to do certain operations.
For example, the * symbol is called multiplication operator and is used to multiply two values together.
Or the + symbol is called addition operator and is used to add two values together.
List of Java Operators
In short there are 5 categories of operators and in the next couple of sections we will dive deep in each of these categories:
Name: | Example: |
Arithmetic Operators | +, -, * etc. |
Assignment Operators | =, -=, += etc. |
Comparison Operators | <, >, <=, >=, == etc. |
Logical Operators | &&, ||, ! |
Bit fiddling operators (AKA bitwise operators) | <<, >>, ^, &, | |
Java Operands:
Operators like *, /, + etc. involve two values.
For example:
3+2;
The value 3 on the left side of the + operator is called left side operand and the value 2 on the right side of the operator is called right side operand. An operand can be either a variable (that has a value already assigned to it), a constant value (like 1, 2, 3 etc.) or a method that returns a value.
Together operand and operators create expression. Note: the keynote about expression is that it should return a value.
Example: invoking Java operands
public class Simple { public static void main(String[] args){ int result = 2 * 4; System.out.println(result); } }
Output: 8
In this example we’ve created the variable named result which is of type int and for its value we have the 2 * 4 expression.
The operands in this expression are 2 and 4. Also the operator of this expression is multiplication *. As mentioned before, expressions always return a result. So the result of this expression will be assigned as the value of the result variable.
Java LValues and RValues
When we work with assignment operators like =, the operand on the right side of the operator is called Rvalue and the operand on the left side of the operator is called LValue.
You should know that we can’t put constant values (like 1,2,3,4 etc.) or a function call on the left side of assignment operators. Basically such values can’t be LValues
The LValues should point to a memory-space that allows for storing a value. So because variables point to memory space, they are perfect candidate for being LValues.
For example:
int jackSalary = 200000; 5000 = jackSalary;
The first assignment int jackSalary = 200000; is acceptable because the LValue is a variable that points to a memory space.
But the second assignment causes the compiler to return error. This is because the value 5000 is a constant value and does not point to any memory location that allows for storing a value.
On the other hand, the RValues can be a constant value (like c, f, 10, 233.322 etc.), a variable, an expression that results a value or a method call that returns a value.
Example: using lvalues and rvalues in Java
int jackSalary = 200000; int ellenSalary = 160000; int totalSalary = jackSalary + ellenSalary;
In the third assignment of the example above, because both jackSalary and ellenSalary are pointing to values, they can be used as the RValues in this expression. So the final value that will be stored in the totalSalary variable is: 36000
Java Arithmetic Operators
Arithmetic operators are those operators in Java that we can use for mathematics operations like addition, division, multiplication etc.
Note: The Arithmetic operators are explained in greater details in later sections.
Example: using mathematical operators in Java
class Main{ public static void main(String[] args) { int val1 = 10; int val2 = 400; int result = val1 * val2; System.out.println(result); } }
Output:
4000
Java Comparison Operators
The comparison operators are those operators that are used for comparing two or more value in order to see which one is bigger than the other or if they are equal in value.
Note: The Comparison operators are explained in greater details in later sections.
Example: using comparison operator in Java
class Main{ public static void main(String[] args) { int val1 = 10; int val2 = 400; boolean result = val1 > val2; System.out.println(result); } }
Output:
false
Here we’ve used the bigger than > operator to see if the value1 is bigger than the value2. And as you can see the result is false which means the value1 is less than the value2.
Java Logical Operators
These are operators in Java that are used to combine the result of two or more expressions that result a boolean value and finally logical operators return a boolean value depending on how the expressions resulted.
For example you might have two comparisons and want to know the result of these comparisons and then run a set of instructions based on the result. This is where the logical operator can help. Note: The Logical operators are explained in greater details in later sections.
Example: using logical operators in Java
class Main{ public static void main(String[] args) { int val1 = 10; int val2 = 400; int val3 = 1000; int val4 = 500; boolean result = val1 > val2 && val3> val4; System.out.println(result); } }
Output:
false
Here we’ve used the AND operator which is used to check two expressions that result a boolean value and see if they both are true or not. If even one of them resulted false, the final value will be false as well.
Java Assignment Operators
Operators that are used to assign a value to a variable are called assignment operators.
One of these operators is = and it takes an Rvalue and assign it to the Lvalue (which should be a variable).
Note: The Assignment operators are explained in greater details in later sections.
Example: using Java assignment operator
class Main{ public static void main(String[] args) { int val1 = 10; String name = "John"; int age = 100; boolean male = true; } }
In this example we’ve used the assignment operator to assign multiple values to different types of variables.