Java Operators and Assignments
Java is a versatile and widely-used programming language that provides a rich set of operators to perform various operations on variables and values. Operators in Java are special symbols or keywords that perform specific operations, such as arithmetic, logical, relational, or bitwise operations. Assignments, on the other hand, are used to assign values to variables. Together, operators and assignments form the foundation of Java's ability to manipulate data and control program flow.
Types of Operators in Java
Java operators can be broadly categorized into the following types:
- Arithmetic Operators
- Relational (Comparison) Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Unary Operators
- Ternary Operator
- Instanceof Operator
- Shift Operators
1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations. These include addition, subtraction, multiplication, division, and modulus.
Operator | Description | Example |
|---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus (remainder) | a % b |
Example:
int a = 10, b = 3;
System.out.println(a + b); // Output: 13
System.out.println(a % b); // Output: 1
2. Relational (Comparison) Operators
Relational operators are used to compare two values. They return a boolean result (true or false).
Operator | Description | Example |
|---|---|---|
== | Equal to | a == b |
!= | Not Equal to | a != b |
> | Greater than | a > b |
< | Less than | a < b |
>= | Greater than or equal to | a >= b |
<= | Less than or equal to | a <= b |
Example:
int a = 5, b = 10;
System.out.println(a > b); // Output: false
System.out.println(a != b); // Output: true
3. Logical Operators
Logical operators are used to combine multiple boolean expressions or values.
Operator | Description | Example |
|---|---|---|
&& | Logical AND | a && b |
|| | Logical OR | a || b |
! | Logical NOT | a != b |
4. Bitwise Operators
Bitwise operators operate on bits and perform bit-level operations.
Operator | Description | Example |
|---|---|---|
& | Bitwise AND | a & b |
| | Bitwise OR | a | b |
^ | Bitwise XOR | a ^ b |
~ | Bitwise Complement | ~a |
Example:
int a = 5, b = 3; // Binary: a = 0101, b = 0011
System.out.println(a & b); // Output: 1 (0001)
System.out.println(a | b); // Output: 7 (0111)
5. Assignment Operators
Assignment operators are used to assign values to variables. The most common assignment operator is =. Other assignment operators combine arithmetic or bitwise operations with assignment.
Operator | Description | Example |
|---|---|---|
= | Assign | a = b |
+= | Add and assign | a += b |
-= | Subtract and assign | a -= b |
*= | Multiply and assign | a *= b |
/= | Divide and assign | a /= b |
%= | Modulus and assign | a %= b |
Example:
int a = 10;
a += 5; // Equivalent to a = a + 5
System.out.println(a); // Output: 15
6. Unary Operators
Unary operators operate on a single operand. They include increment, decrement, and negation operators.
Operator | Description | Example |
|---|---|---|
+ | Unary plus | +a |
- | Unary minus | -a |
++ | Increment | ++a or a++ |
-- | Decrement | --a or a-- |
! | Logical NOT | !a |
Example:
int a = 5;
System.out.println(++a); // Pre-increment: Output 6
System.out.println(a--); // Post-decrement: Output 6, then a becomes 5
7. Ternary Operator
The ternary operator is a shorthand for an if-else statement. It takes three operands.
condition ? value_if_true : value_if_false;
Example:
int a = 10, b = 20;
int max = (a > b) ? a : b;
System.out.println(max); // Output: 20
8. Instanceof Operator
The instanceof operator is used to test whether an object is an instance of a specific class or subclass.
Example:
String str = "Hello";
System.out.println(str instanceof String); // Output: true
9. Shift Operators
Shift operators are used to shift bits to the left or right.
Operator | Description | Example |
|---|---|---|
<< | Left shift | a << 2 |
>> | Right Shift | a >> 2 |
>>> | Unsigned right shift | a >>> 2 |
Example:
int a = 8; // Binary: 1000
System.out.println(a << 1); // Output: 16 (Binary: 10000)
Precedence and Associativity of Operators
Operator precedence determines the order in which operators are evaluated in an expression. For example, multiplication (*) has higher precedence than addition (+).
Example:
int result = 10 + 5 * 2; // Multiplication is evaluated first
System.out.println(result); // Output: 20
Assignments in Java
Assignments in Java involve assigning values to variables using the assignment operator (=). Java supports both simple and compound assignments.
Simple Assignment
A simple assignment assigns a value to a variable.
int a = 10;
Compound Assignment
Compound assignments combine an operation with assignment.
int a = 10;
a += 5; // Equivalent to a = a + 5
System.out.println(a); // Output: 15
Java operators and assignments are essential tools for performing operations and managing data in a program
