Operators in Java
Introduction
In the programming world, operators are indispensable tools that enable developers to perform various operations on data. In this blog post, we’ll delve into the world of operators in Java.
Types of Operators
Being one of the most popular programming languages, Java offers a rich set of operators that facilitate tasks ranging from simple arithmetic operations to complex logical manipulations. A few of the Operators are:
- Arithmetic Operators
- Assignment Operators
- Bitwise Operators
- Comparison Operators
- Logical Operators
- Unary Operators
Arithmetic Operators
Arithmetic operators perform mathematical operations such as addition, subtraction, multiplication, and division. In Java, the basic arithmetic operators include addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). Let’s see some examples:
class ArithmeticOperators {
public static void main(String[] args) {
int a = 10;
int b = 5;
int sum = a + b; // Addition
int difference = a - b; // Subtraction
int product = a * b; // Multiplication
int quotient = a / b; // Division
int remainder = a % b; // Modulus
System.out.println("a + b = "+sum);
System.out.println("a - b = "+difference);
System.out.println("a * b = "+product);
System.out.println("a / b = "+quotient);
System.out.println("a % b = "+remainder);
}
}
Output
a + b = 15
a — b = 5
a * b = 50
a / b = 2
a % b = 0
Assignment Operators
Assignment Operators are used to assign values to variables. They include the simple assignment operator (=) and compound assignment operators like +=, -=, *=, /=, and %=, which combine arithmetic operations with assignment operations.
class AssignmentOperators {
public static void main(String[] args) {
int a = 10; // Assignment Operator
int b = 5;
int d = 30;
d+=a; // d = d + a;
System.out.println("d+=a "+ d);
System.out.println("d = "+ d);
d-=b; // d = d - b;
System.out.println("d-=b "+ d);
System.out.println("d = "+ d);
a*=b; // a = a * b;
System.out.println("a*=b "+ a);
System.out.println("a = "+ a);
}
}
Output
d+=a 40
d = 40
d-=b 35
d = 35
a*=b 50
a = 50
Bitwise Operators
Bitwise operators are used to perform operations on individual bits of integers. They are often used in low-level programming and for manipulating binary data. Java supports bitwise AND (&), bitwise OR (|), bitwise XOR (^), bitwise complement (~), left shift (<<), and right shift (>>).
class BitwiseOperators {
public static void main(String[] args) {
int a = 10; // binary: 1010
int b = 5; // binary: 0101
int c = 3; // binary: 0011
int bitwiseAnd = b & c; // binary: 0001 (1 in decimal)
int bitwiseOr = a | b; // binary: 1111 (15 in decimal)
System.out.println("b & c = "+ bitwiseAnd);
System.out.println("a | b = "+ bitwiseOr);
}
}
Output
b & c = 1
a | b = 15
Comparison Operators
Comparison operators are used to compare two values. They return a boolean result indicating whether the comparison is true or false. Common comparison operators in Java include == (equal to), != (not equal to), > (greater than), < (less than), >= (greater than or equal to), and <= (less than or equal to).
class ComparisonOperators {
public static void main(String[] args) {
int a = 10;
int b = 5;
int c = 10;
boolean isEqual = (a == b); // false
boolean isGreater = (a > b); // true
boolean isLess = (a < c); // false
boolean isLessOrEqual = (a <= c); // true
System.out.println("is a == b : "+ isEqual);
System.out.println("is a > b : "+ isGreater);
System.out.println("is a < c : "+ isLess);
System.out.println("is a <= c : "+ isLessOrEqual);
}
}
Output
is a == b : false
is a > b : true
is a < c : false
is a <= c : true
Logical Operators
Logical operators are used to perform logical operations on boolean values. Java provides three logical operators: && (logical AND), || (logical OR), and ! (logical NOT).
class LogicalOperators {
public static void main(String[] args) {
boolean b1 = true;
boolean b2 = false;
boolean andOperation = b1 && b2; // false
boolean orOperation = b1 || b2; // true
boolean notOperation = !b1; // false
System.out.println("b1 && b2 = "+ andOperation);
System.out.println("b1 || b2 = "+ orOperation);
System.out.println("!b1 = "+ notOperation);
}
}
Output
b1 && b2 = false
b1 || b2 = true
!b1 = false
Unary Operators
Unary Operators are used to perform operations such as negating an expression, incrementing or decrementing a value by one, and inverting the value of a boolean.
class UnaryOperators {
public static void main(String[] args) {
int a = 10;
int b = -a; // b becomes -10
int c = +a; // c remains 10
int count = 50;
boolean isTrue = true;
boolean isFalse = !isTrue; // false
System.out.println("b = "+ b);
System.out.println("c = "+ c);
System.out.println("isFalse = "+ isFalse);
System.out.println("++count = "+ ++count); // Prefix increment: count becomes 51
System.out.println("count++ = "+ count++); // Postfix increment: count becomes 52
System.out.println("count = "+ count);
System.out.println("--count = "+ --count); // Prefix decrement: count becomes 51
System.out.println("count-- = "+ count--); // Postfix decrement: count becomes 50
System.out.println("count = "+ count);
}
}
Output
b = -10
c = 10
isFalse = false
++count = 51
count++ = 51
count = 52
— count = 51
count — = 51
count = 50
Operator Precedence
Operator precedence defines the order in which operators are evaluated when multiple operators appear in the same expression. Operators with higher precedence are evaluated before operators with lower precedence. Parentheses can be used to override the default precedence and control the order of evaluation.
Importance of Understanding Operator Precedence
Writing correct expressions: Understanding precedence ensures that expressions are evaluated as intended, avoiding unexpected results.
Improving readability: Explicitly specifying the order of evaluation using parentheses enhances code readability and makes it easier for others to understand.
Enhancing efficiency: Proper use of precedence can lead to more efficient code execution by reducing unnecessary evaluations.
Oracle Certified Associate (OCA) certification overview:
Oracle Certified Associate: Java SE 8 Programmer
For more Java and Spring-Boot-related blogs, check out my profile:
https://medium.com/@gauravshah97
Reach me out at:
https://www.linkedin.com/in/gauravshah97/