Lesson 8
Operators
Java Operators are special symbols used to perform mathematical, logical, relational, and bit-level operations on variables and values.
Lesson content
What are Operators in Java?
An operator is a symbol that tells the compiler to perform specific mathematical or logical operations.
Most operators in Java are binary operators, meaning they require two operands to perform an operation. Some operators, such as increment (++) and decrement (--), are unary operators because they work on only one operand.
Java also provides a ternary operator called the conditional operator (?:) that works with three operands.
Definition List
- Operator: A symbol used to perform operations on variables and values.
- Binary Operator: An operator that requires two operands.
- Unary Operator: An operator that works with a single operand.
- Ternary Operator: An operator that works with three operands.
Types of Operators in Java
Java supports the following types of operators:
- Arithmetic Operators
- Assignment Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Conditional (Ternary) Operator
Arithmetic Operators in Java
Arithmetic operators are used to perform basic mathematical operations on operands.
Arithmetic Operator Table
- + (Addition): Adds two values. Example: x + y
- - (Subtraction): Subtracts one value from another. Example: x - y
- * (Multiplication): Multiplies two values. Example: x * y
- / (Division): Divides one value by another. Example: x / y
- % (Modulus): Returns remainder after division. Example: x % y
- ++ (Increment): Increases value by 1. Example: ++x
- -- (Decrement): Decreases value by 1. Example: --x
Example Program for Arithmetic Operators
package Operators;
//Arithmetic Operators
public class Arithmetic {
public static void main(String args[])
{
int a=123,b=10;
System.out.println("Addition : "+(a+b)); //133
System.out.println("Subtraction : "+(a-b));//113
System.out.println("Multiplication : "+(a*b));//1230
System.out.println("Division : "+(a/b));//12
System.out.println("Modulus : "+(a%b));//3
}
}Unary Operators in Java
Unary operators work with only one operand. The increment (++) and decrement (--) operators are commonly used unary operators in Java.
Example Program for Unary Operators
package Operators;
public class Unary {
public static void main(String args[])
{
//Unary Operators in Java ++ --
int a=10;
System.out.println(a);//
//a++; //a=a+1
System.out.println(a++);//
System.out.println(a);//
System.out.println(++a);//
System.out.println(a--);//
System.out.println(a);//
System.out.println(--a);//
}
}Relational Operators in Java
Relational operators are used to compare two operands. The result of the comparison is either true or false.
Relational Operator Table
- == (Equal to): x == y
- != (Not equal): x != y
- > (Greater than): x > y
- < (Less than): x < y
- >= (Greater than or equal to): x >= y
- <= (Less than or equal to): x <= y
Example Program for Relational Operators
package Operators;
public class relational {
public static void main(String args[])
{
int a=100,b=50;
System.out.println("Equal to : "+(a==b));
System.out.println("Not Equal to : "+(a!=b));
System.out.println("Greater than : "+(a>b));
System.out.println("Less than : "+(a<b));
System.out.println("Greater than or equal to : "+(a>=b));
System.out.println("Less than or equal to : "+(a<=b));
}
}Logical Operators in Java
Logical operators are used to evaluate combinations of conditions. The result of a logical operation is always true or false.
Logical Operator Table
- && (Logical AND): Returns true if both conditions are true. Example: x < 5 && x < 10
- || (Logical OR): Returns true if one condition is true. Example: x < 5 || x < 10
- ! (Logical NOT): Reverses the result. Example: !(x < 5 && x < 10)
Example Program for Logical Operators
package Operators;
public class Logical {
public static void main(String args[])
{
int m1=25,m2=75;
System.out.println("And && : "+(m1>=35 && m2>=35));
System.out.println("Or || : "+(m1>=35 || m2>=35));
}
}Assignment Operators in Java
Assignment operators are used to assign values to variables.
Assignment Operator Table
- = : x = 5
- += : x += 3 (Same as: x = x + 3)
- -= : x -= 3 (Same as: x = x - 3)
- *= : x *= 3 (Same as: x = x * 3)
- /= : x /= 3 (Same as: x = x / 3)
- %= : x %= 3 (Same as: x = x % 3)
- &= : x &= 3 (Same as: x = x & 3)
- |= : x |= 3 (Same as: x = x | 3)
- ^= : x ^= 3 (Same as: x = x ^ 3)
- >>= : x >>= 3 (Same as: x = x >> 3)
- <<= : x <<= 3 (Same as: x = x << 3)
Example Program for Assignment Operators
package Operators;
public class Assignment
{
public static void main(String args[])
{
int a=123;
System.out.println(a);
a+=10;
System.out.println(a);
a-=10;//a=a-10
System.out.println(a);
a*=10;
System.out.println(a);
a/=10;
System.out.println(a);
a%=10;
System.out.println(a);
}
}Conditional Operator in Java
The conditional operator is also called the ternary operator. It is used for decision-making in Java programs.
The ternary operator works similarly to an if-else statement.
Syntax of Conditional Operator
Expression ? expression2 : expression3;
Example Program for Conditional Operator
package Operators;
public class Conditional {
public static void main(String args[])
{
//Conditional or Ternary Operators in Java ?:
int a=45,b=35,c;
c=a>b?a:b;
System.out.println("The Greatest Number is : "+c);
}
}Bitwise Operators in Java
Bitwise operators perform operations on data at the bit level.
Common Bitwise Operators
- & (Bitwise AND)
- | (Bitwise OR)
- ^ (Bitwise XOR)
- ~ (Bitwise NOT)
- << (Left Shift)
- >> (Right Shift)
Example Program for Bitwise Operators
package Operators;
public class Bitwise {
public static void main(String args[])
{
//Bitwise & Shift Operators in Java
int a=25,b=45;
System.out.println("Bitwise And : "+(a&b));
System.out.println("Bitwise Or : "+(a|b));
System.out.println("Bitwise Xor : "+(a^b));
System.out.println("Bitwise Not : "+(~a));
}
}Advantages of Using Operators in Java
Faster Calculations
Operators help perform calculations quickly and efficiently.
Better Decision Making
Relational and logical operators help create conditions and control program flow.
Easy Data Manipulation
Bitwise and assignment operators simplify complex operations.
FAQ
1. What are operators in Java?
Operators are symbols used to perform operations on variables and values, such as addition, comparison, and logical evaluation.
2. What is the difference between unary and binary operators?
Unary operators work with one operand, while binary operators require two operands.
3. What is the use of the ternary operator in Java?
The ternary operator is used for decision-making and works as a short form of the if-else statement.