- Java Programming Basics
- Java Tutorial
- Java Environment Setup
- Java Separators
- Java Data Types
- Java Variables
- Java Variable Scope
- Java Type Casting
- Java Operators
- Java Increment Decrement
- Java Left Shift
- Java Right Shift
- Java Bitwise Operators
- Java Ternary Operator
- Java Control Statements
- Java if-else statement
- Java for Loop
- Java while Loop
- Java do-while Loop
- Java switch Statement
- Java break Statement
- Java continue Statement
- Java Popular Topics
- Java Arrays
- Java Multidimensional Array
- Java Strings
- Java Methods
- Java Date and Time
- Java Exception Handling
- Java File Handling
- Java OOP
- Java Classes and Objects
- Java Constructors
- Java Constructor Overloading
- Java Object as Parameter
- Java Returning Objects
- Java Encapsulation
- Java Abstraction
- Java Inheritance
- Java Polymorphism
- Java Packages
- Java Import Statement
- Java Multithreading
- Java Suspend Resume Stop Thread
- Java Programming Examples
- Java Programming Examples
Operators in Java with types, lists, and examples
The use of operators is a cornerstone of Java and programming in general. Data manipulation tasks like adding, subtracting, and comparing numbers, as well as assigning values to variables, can all be accomplished with the help of special symbols and keywords known as "operators."
Learn about the various operators available in Java and how they can be used in your own code to perform various operations on data. Now is the time to dive in and study the many operators Java has to offer.
Types of operators in Java
The following is a list of Java operators organized according to the type of operation they perform.
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
- Ternary Operator
- instanceof Operator
The "bitwise" and "ternary" operators will be discussed separately in a separate post, whereas all of the other types will be discussed in this post, beginning with the "arithmetic operators," right after this paragraph.
Arithmetic operators in Java
To perform elementary arithmetic operations on numbers, Java provides a set of operators known as "arithmetic operators." Simple arithmetic operations like plus, minus, multiply, divide, and modulo (remainder) can all be performed with these operators.
- Addition (+): This operator is used to add together two or more numeric values.
- The subtraction operator (-) is utilized to subtract one numeric value from another.
- The multiplication operator (*) is utilized to multiply two or more numeric values.
- The division operator (/) is utilized to divide one numeric value by another. Note that division involving integers may result in truncation, so it is important to consider data types when employing the division operator.
- The modulo (%) operator is utilized to determine the remainder of a division operation. This can be useful for checking if a number is even or odd or for performing cyclic calculations.
For example:
public class ArithmeticOperatorsExample { public static void main(String[] args) { int num1 = 10; int num2 = 5; int sum = num1 + num2; System.out.println("Addition: " + sum); int difference = num1 - num2; System.out.println("Subtraction: " + difference); int product = num1 * num2; System.out.println("Multiplication: " + product); int quotient = num1 / num2; System.out.println("Division: " + quotient); int remainder = num1 % num2; System.out.println("Modulo: " + remainder); } }
Addition: 15 Subtraction: 5 Multiplication: 50 Division: 2 Modulo: 0
There are two more operators that fall under these categories, which are the increment (++) and decrement (--) operators. I described these two operators in a separate (next) post.
Assignment operators in Java
Assignment operators in Java are used to assign a value to a variable. They allow you to perform operations on a variable's value and store the result back in the same variable.
- Simple assignment (=): Java assigns values to variables using the assignment operator (=). It stores values or expressions in variables.
- Addition and assignment (+=): The += operator is a shortcut for adding a value to a variable and assigning it back.
- Subtraction and assignment (-=): The -= operator shortens subtracting a value from a variable and assigning the result to the same variable.
- Multiplication and assignment (*=): The *= operator shortens multiplying a value to a variable and assigning the result back to the same variable.
- Division and assignment (/=): The /= operator shortens dividing a variable by a value and assigning the result back to the same variable.
- Modulus and assignment (%=): The %= operator is a shorthand notation for finding the modulus of a variable with a value and assigning the result to the same variable.
- Left shift and assignment (<<=): The <<= operator performs a left shift on a variable and assigns the result back to it.
- Right shift and assignment (>>=): The >>= operator performs a right shift on a variable and assigns the result back to it.
- Unsigned right shift and assignment (>>>=): The >>>= operator performs an unsigned right shift on a variable and assigns the result back to it.
- Bitwise AND and assignment (&=): The &= operator performs a bitwise AND on a variable and assigns the result to the same variable.
- Bitwise OR and assignment (|=): The |= operator performs a bitwise OR on a variable and assigns the result to the same variable.
- Bitwise XOR and assignment (^=): The ^= operator performs a bitwise XOR on a variable and assigns the result to the same variable.
The following statement:
a += 10;
is equivalent to
a = a+10;
Similarly, the following statement:
a -= 10;
is equivalent to
a = a-10;
and so on.
The left shift, right shift, and bitwise operators are discussed in their separate posts.
Comparison operators in Java
Java uses comparison operators to compare values and establish their relationship. Depending on whether the comparison is accurate or inaccurate, they return a boolean value of true or false. Java comparison operators are listed below:
- Equal to operator (==): determines whether two values are equal. It compares the values on the operator's left and right sides and returns true if they are equal, false otherwise.
- Not equal to operator (!=): determines whether two values are not equal. It compares the values on the operator's left and right sides and returns true if they are not equal and false otherwise.
- Greater than operator (>): determines whether the value on the left is higher than the value on the right. If the value on the left is greater, it returns true; otherwise, it returns false.
- Less than operator (<): determines whether the value on the left is lower than the value on the right. If the value on the left is less, it returns true; otherwise, it returns false.
- Greater than or equal to operator (>=): determines whether the value on the left is higher than or equal to the value on the right. If the value on the left is higher than or equal to the value on the right, it returns true; if not, it returns false.
- Less than or equal to operator (<=): determines whether the value on the left is less than or equal to the value on the right. If the value on the left is less than or equal to the value on the right, it returns true; otherwise, it returns false.
For example:
public class JavaProgram { public static void main(String args[]) { int num1 = 100, num2 = 200; System.out.println("num1 == num2 = " + (num1 == num2)); System.out.println("num1 != num2 = " + (num1 != num2)); System.out.println("num1 > num2 = " + (num1 > num2)); System.out.println("num1 < num2 = " + (num1 < num2)); System.out.println("num2 >= num1 = " + (num2 >= num1)); System.out.println("num2 <= num1 = " + (num2 <= num1)); } }
When the above Java program is compiled and executed, it will produce the following output:
Logical operators in Java
In Java, logical operators are used to operate on boolean values (true or false). In Java, the following three logical operators are available:
- AND operator (&&): If both operands are true, returns true; otherwise, returns false. It determines whether both the left and right operands are true, and if so, the result is true. The result is false if either operand is false.
- OR operator (||): If at least one of the operands is true, returns true; otherwise, returns false. It checks to see if either the left or right operand is true, and if both are true, the result is true. The result is false if both operands are false.
- NOT operator (!): It is a unary operator that flips the operand's boolean value. If the operand is false, it returns true; otherwise, it returns false.
For example:
public class LogicalOperatorExample { public static void main(String[] args) { int a = 5; int b = 10; int c = 15; // Logical AND (&&) Operator if (a < b && b < c) { System.out.println("a < b < c"); } else { System.out.println("a, b, or c is not in ascending order"); } // Logical OR (||) Operator if (a < b || a < c) { System.out.println("Either a < b or a < c (or both)"); } else { System.out.println("Neither a < b nor a < c"); } // Logical NOT (!) Operator boolean isTrue = true; if (!isTrue) { System.out.println("This will not be executed"); } else { System.out.println("isTrue is true"); } } }
a < b < c Either a < b or a < c (or both) isTrue is true
instanceof operator in Java
Java's instanceof operator compares objects to classes and subclasses. It checks if an object is a class instance or a class that inherits from a class. The instanceof operator has the following syntax:
object instanceof class
where "object" refers to the reference variable of the object being tested and "class" refers to the class or interface being tested.
The instanceof operator returns a boolean value indicating whether the object is an instance of the given class or not. For example:
public class JavaProgram { public static void main(String args[]) { String str_name = "James"; boolean res = str_name instanceof String; System.out.println(res); } }
Here is the output that the aforementioned Java program produced:
Java operator precedence
The order in which Java evaluates expressions containing multiple operators is determined by operator precedence. The precedence of operators in Java is as follows, with operators listed from highest to lowest precedence:
- Postfix operators: expr++ (post-increment), expr-- (post-decrement)
- Prefix operators: ++expr (pre-increment), --expr (pre-decrement)
- Unary operators: +expr (unary plus), -expr (unary minus), ! (logical complement), ˜ (bitwise complement)
- Multiplicative operators: * (multiplication), / (division), % (modulus)
- Additive operators: + (addition), - (subtraction)
- Shift operators: << (left shift), >> (signed right shift), >>> (unsigned right shift)
- Relational operators: < (less than), <= (less than or equal to), > (greater than), >= (greater than or equal to), instanceof
- Equality operators: == (equal to), != (not equal to)
- Bitwise AND operator: &
- Bitwise XOR operator: ^
- Bitwise OR operator: |
- Logical AND operator: &&
- Logical OR operator: ||
- Conditional operator: ?:
- Assignment operators: = (simple assignment), += (add and assign), -= (subtract and assign), *= (multiply and assign), /= (divide and assign), %= (modulus and assign), &= (bitwise AND and assign), ^= (bitwise XOR and assign), |= (bitwise OR and assign), <<= (left shift and assign), >>= (signed right shift and assign), >>>= (unsigned right shift and assign)
Note: To override the default order of operations, use parentheses. The operators enclosed in parentheses are evaluated first.
« Previous Tutorial Next Tutorial »