Operators in Java are symbols that perform operations on variables and values. They are essential for performing calculations, comparisons, and logical decisions in your programs. This tutorial will cover the different types of operators in Java, including arithmetic, relational, logical, bitwise, and assignment operators.
Java Operators Tutorial
By the end of this tutorial, you'll understand how to use Java operators effectively to manipulate data and control the flow of your programs.
Types of Operators in Java
- Arithmetic Operators: Perform basic mathematical operations.
- Relational Operators: Compare two values or expressions.
- Logical Operators: Perform logical operations on boolean values.
- Bitwise Operators: Perform operations on individual bits of integer values.
- Assignment Operators: Assign values to variables.
- Miscellaneous Operators: Includes ternary and instanceOf operators.
Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, and division.
+: Addition (e.g.,5 + 3results in8).-: Subtraction (e.g.,5 - 3results in2).*: Multiplication (e.g.,5 * 3results in15)./: Division (e.g.,10 / 2results in5).%: Modulus (e.g.,10 % 3results in1).++: Increment (e.g.,int x = 5; x++;results inx = 6).--: Decrement (e.g.,int x = 5; x--;results inx = 4).
int a = 10, b = 3;
System.out.println("Addition: " + (a + b)); // 13
System.out.println("Subtraction: " + (a - b)); // 7
System.out.println("Multiplication: " + (a * b)); // 30
System.out.println("Division: " + (a / b)); // 3
System.out.println("Modulus: " + (a % b)); // 1Relational Operators
Relational operators are used to compare two values or expressions and return a boolean result (true or false).
==: Equal to (e.g.,5 == 3results infalse).!=: Not equal to (e.g.,5 != 3results intrue).>: Greater than (e.g.,5 > 3results intrue).<: Less than (e.g.,5 < 3results infalse).>=: Greater than or equal to (e.g.,5 >= 5results intrue).<=: Less than or equal to (e.g.,5 <= 3results infalse).
int x = 5, y = 3;
System.out.println("Equal to: " + (x == y)); // false
System.out.println("Not equal to: " + (x != y)); // true
System.out.println("Greater than: " + (x > y)); // true
System.out.println("Less than: " + (x < y)); // falseLogical Operators
Logical operators are used to combine multiple conditions and return a boolean result.
&&: Logical AND (e.g.,true && falseresults infalse).||: Logical OR (e.g.,true || falseresults intrue).!: Logical NOT (e.g.,!trueresults infalse).
boolean a = true, b = false;
System.out.println("AND: " + (a && b)); // false
System.out.println("OR: " + (a || b)); // true
System.out.println("NOT: " + (!a)); // falseAssignment Operators
Assignment operators are used to assign values to variables.
=: Simple assignment (e.g.,int x = 5;).+=: Add and assign (e.g.,x += 3;is equivalent tox = x + 3;).-=: Subtract and assign (e.g.,x -= 3;is equivalent tox = x - 3;).*=: Multiply and assign (e.g.,x *= 3;is equivalent tox = x * 3;)./=: Divide and assign (e.g.,x /= 3;is equivalent tox = x / 3;).%=: Modulus and assign (e.g.,x %= 3;is equivalent tox = x % 3;).
int x = 10;
x += 5; // x = 15
x -= 3; // x = 12
x *= 2; // x = 24
x /= 4; // x = 6
x %= 5; // x = 1Ternary Operator
The ternary operator is a shorthand for an if-else statement.
- Syntax:
condition ? expression1 : expression2. - If the condition is
true,expression1is executed; otherwise,expression2is executed.
int x = 10, y = 5;
String result = (x > y) ? "x is greater" : "y is greater";
System.out.println(result); // x is greaterThis tutorial covered the most commonly used operators in Java. Practice using these operators to become proficient in writing Java programs.