Operators in Java Programming Language

Operators in Java Programming language

Operators in Java are symbols or characters used to perform operations on one or more operands (variables, constants, literals) and produce a result. Java has various types of operators, including

  • arithmetic operators
  • comparison operators
  • logical operators
  • assignment operators and
  • bitwise operators.

Arithmetic Operators

Arithmetic operators are used to performing basic mathematical operations, such as addition, subtraction, multiplication, and division.

Addition (+)

The addition operator is used to add two values together. Here’s an example:

int x = 5;
int y = 7;
int sum = x + y;
System.out.println("The sum is: " + sum);

Output: The sum is: 12

Subtraction (-)

The subtraction operator is used to subtract one value from another. Here’s an example:

int x = 10;
int y = 7;
int difference = x - y;
System.out.println("The difference is: " + difference);

Output: The difference is: 3

Multiplication (*)

The multiplication operator is used to multiply two values together. Here’s an example:

int x = 4;
int y = 6;
int product = x * y;
System.out.println("The product is: " + product);

Output: The product is: 24

Division (/)

The division operator is used to divide one value by another. Here’s an example:

int x = 15;
int y = 3;
int quotient = x / y;
System.out.println("The quotient is: " + quotient);

Output: The quotient is: 5

Modulus (%)

The modulus operator finds the remainder of dividing one value by another. Here’s an example:

int x = 15;
int y = 4;
int remainder = x % y;
System.out.println("The remainder is: " + remainder);

Output: The remainder is: 3

Assignment Operators in Java

Assignment operators are used to assigning values to variables.

Simple Assignment (=)

The simple assignment operator is used to assign a value to a variable. Here’s an example:

int x = 5;
System.out.println("The value of x is: " + x);

Output: The value of x is: 5

Compound Assignment (+=, -=, *=, /=, %=)

The compound assignment operators are used to perform an arithmetic operation and then assign the result to a variable. Here are some examples:

int x = 10;
x += 5; // equivalent to x = x + 5
System.out.println("The value of x is: " + x);

Output: The value of x is: 15

int x = 10;
x -= 3; // equivalent to x = x - 3
System.out.println("The value of x is: " + x);

Output: The value of x is: 7

int x = 10;
x *= 2; // equivalent to x = x * 2
System.out.println("The value of x is: " + x);

Output: The value of x is: 20

int x = 10;
x /= 3; // equivalent to x = x / 3
System.out.println("The value of x is: " + x);

Output: The value of x is: 3

Comparison Operators in Java

Comparison operators are used to comparing two values and return a boolean value (true or false).

Equal To (==)

The equal to the operator is used to check if two values are equal. Here’s an example:

int x = 5;
int y = 7;
boolean result = (x == y);
System.out.println("Are x and y equal? " + result);

Output: Are x and y equal? false

Not Equal To (!=)

The not equal to the operator is used to check if two values are not equal. Here’s an example:

int x = 5;
int y = 7;
boolean result = (x != y);
System.out.println("Are x and y not equal? " + result);

Output: Are x and y not equal? true

Greater Than (>)

The greater than operator is used to check if one value is greater than another. Here’s an example:

int x = 5;
int y = 7;
boolean result = (y > x);
System.out.println("Is y greater than x? " + result);

Output: Is y greater than x? true

Less Than (<)

The less than operator is used to check if one value is less than another. Here’s an example:

int x = 5;
int y = 7;
boolean result = (x < y);
System.out.println("Is x less than y? " + result);

Output: Is x less than y? true

Greater Than or Equal To (>=)

The greater than or equal to the operator is used to check if one value is greater than or equal to another. Here’s an example:

int x = 5;
int y = 7;
boolean result = (y >= x);
System.out.println("Is y greater than or equal to x? " + result);

Output: Is y greater than or equal to x? true

Less Than or Equal To (<=)

The less than or equal to the operator is used to check if one value is less than or equal to another. Here’s an example:

int x = 5;
int y = 7;
boolean result = (x <= y);
System.out.println("Is x less than or equal to y? " + result);

Output: Is x less than or equal to y? true

Logical Operators

Logical operators are used to combining two or more conditions and return a boolean value (true or false).

Logical AND (&&)

The logical AND operator is used to check if both conditions are true. Here’s an example:

int x = 5;
int y = 7;
boolean result = (x < y && y < 10);
System.out.println("Is x less than y AND y less than 10? " + result);

Output: Is x less than y AND y less than 10? true

Logical OR (||)

The logical OR operator is used to check if at least one of the conditions is true. Here’s an example:

int x = 5;
int y = 7;
boolean result = (x < y || y > 10);
System.out.println("Is x less than y OR y greater than 10? " + result

Output: Is x less than y OR y greater than 10? true

Logical NOT (!)

The logical NOT operator is used to invert the value of a boolean expression. Here’s an example:

int x = 5;
int y = 7;
boolean result = !(x < y);
System.out.println("Is x NOT less than y? " + result);

Output: Is x NOT less than y? false

Assignment Operators in Java

Assignment operators are used to assigning values to variables.

Simple Assignment (=)

The simple assignment operator is used to assign a value to a variable. Here’s an example:

int x = 5;
System.out.println("The value of x is: " + x);

x = 7;
System.out.println("The value of x is now: " + x);

Output:

The value of x is: 5
The value of x is now: 7

Compound Assignment (+=, -=, *=, /=, %=)

Compound assignment operators are used to perform an operation and assign the result to a variable in one step. Here are some examples:

int x = 5;
x += 3; // equivalent to x = x + 3
System.out.println("The value of x is: " + x);

x -= 2; // equivalent to x = x - 2
System.out.println("The value of x is now: " + x);

x *= 4; // equivalent to x = x * 4
System.out.println("The value of x is now: " + x);

x /= 2; // equivalent to x = x / 2
System.out.println("The value of x is now: " + x);

x %= 3; // equivalent to x = x % 3
System.out.println("The value of x is now: " + x);

Output:

The value of x is: 8
The value of x is now: 6
The value of x is now: 24
The value of x is now: 12
The value of x is now: 0

Bitwise Operators

Bitwise operators are used to performing operations on binary numbers.

Bitwise AND (&)

The bitwise AND operator compares the corresponding bits of two numbers and returns 1 if both bits are 1. Here’s an example:

int x = 5; // 101 in binary
int y = 3; // 011 in binary
int result = x & y; // 001 in binary
System.out.println("The result of x & y is: " + result);

Output: The result of x & y is: 1

Bitwise OR (|)

The bitwise OR operator compares the corresponding bits of two numbers and returns 1 if at least one bit is 1. Here’s an example:

int x = 5; // 101 in binary
int y = 3; // 011 in binary
int result = x | y; // 111 in binary
System.out.println("The result of x | y is: " + result);

Output: The result of x | y is: 7

Bitwise XOR (^)

The bitwise XOR operator compares the corresponding bits of two numbers and returns 1 if the bits are different. Here’s an example:

int x = 5; // 101 in binary
int y = 3; // 011 in binary
int result = x ^ y; // 110 in
System.out.println("The result of x ^ y is: " + result);

Output: The result of x ^ y is: 6

Bitwise Complement (~)

The bitwise complement operator inverts all the bits of a number. Here’s an example:

int x = 5; // 101 in binary
int result = ~x; // 010 in binary (because of two's complement representation)
System.out.println("The result of ~x is: " + result);

Output: The result of ~x is: -6

Note that the result is negative because of the two’s complement representation.

Left Shift (<<)

The left shift operator shifts the bits of a number to the left by a specified number of positions. Here’s an example:

int x = 5; // 101 in binary
int result = x << 2; // 10100 in binary
System.out.println("The result of x << 2 is: " + result);

Output: The result of x << 2 is: 20

Right Shift (>>)

The right shift operator shifts the bits of a number to the right by a specified number of positions. Here’s an example:

int x = 20; // 10100 in binary
int result = x >> 2; // 101 in binary
System.out.println("The result of x >> 2 is: " + result);

Output: The result of x >> 2 is: 5

Unsigned Right Shift (>>>)

The unsigned right shift operator shifts the bits of a number to the right by a specified number of positions, filling the leftmost positions with zeros. Here’s an example:

int x = -20; // 11111111111111111111111111101100 in binary (because of two's complement representation)
int result = x >>> 2; // 00111111111111111111111111111011 in binary
System.out.println("The result of x >>> 2 is: " + result);

Output: The result of x >>> 2 is: 1073741821

Understanding these operators is essential for writing effective Java programs.

FAQ’s

What is the difference between the = operator and the == operator in Java?

The = operator is the simple assignment operator used to assign a value to a variable, while the == operator is the equality operator used to compare two values or variables for equality.

What is the order of precedence for operators in Java?

The order of precedence for operators in Java determines the order in which operators are evaluated in an expression. The highest precedence is for postfix operators such as increment and decrement, followed by unary operators such as logical NOT and bitwise complement, then binary operators such as multiplication and division, and finally, assignment operators.

What is the purpose of the modulus operator (%) in Java?

The modulus operator (%) in Java returns the remainder of a division operation. It is often used to determine if a number is even or odd or to cycle through a set of values.

What is the difference between the & operator and the && operator in Java?

The & operator is the bitwise AND operator, which performs a bitwise AND operation on two integer values. The && operator is the logical AND operator, which performs a logical AND operation on two boolean values. The main difference is that the && operator evaluates the second operand only if the first operand is true, while the & operator always evaluates both operands.

What is the difference between the left shift (<<) and right shift (>>) operators in Java?

The left shift (<<) operator in Java shifts the bits of a number to the left by a specified number of positions, effectively multiplying the number by a power of 2. The right shift (>>) operator in Java shifts the bits of a number to the right by a specified number of positions, effectively dividing the number by a power of 2.