Java Operators: making math harder than it needs to be.

Ritu Sitlani
Javarevisited
Published in
6 min readMar 7, 2024

What are Operators in Java ?

Operators are symbols that manipulate data by performing specific operations on operands.

Types of Operators :

Arithmetic Operators :

As name suggests, they perform simple arithmetic operations on primitive data types. Arithmetic operators include —
addition (+),
subtraction (-),
multiplication (*),
division (/) and modulus (%).
They manipulate numerical values and are fundamental for mathematical calculations within Java programs.

Below is a Java code example demonstrating the use of arithmetic operators:


public class operators {
public static void main(String[] args) {
//operands
int operand1 = 15;
int operand2 = 10;

//operators
int sum = operand1 + operand2;
System.out.println("Sum : "+sum);

// Subtraction
int difference = operand1 - operand2;
System.out.println("Difference: " + difference);

// Multiplication
int product = operand1 * operand2;
System.out.println("Product: " + product);

// Division
int quotient = operand1 / operand2;
System.out.println("Quotient: " + quotient);

// Modulus (remainder)
int remainder = operand1 % operand2;
System.out.println("Remainder: " + remainder);

}
}

Output:

Sum : 25
Difference: 5
Product: 150
Quotient: 1
Remainder: 5

Unary Operators :

Operators that operate on a single operand are known as Unary Operators. Unary Operators include :

1. Unary minus (-) Operator : Converts a positive value to a negative one


public class operators {
public static void main(String[] args) {
//operands
int operand1 = 15;
int operand2 = 10;

//unary operators
operand1 = -operand1;
System.out.println("Result :" +operand1);
}}

Output :

Result :-15

2. Logical NOT (!) Operator: Inverts the logical state of its operand. Basically converts True to false and vice versa.


public class operators {
public static void main(String[] args) {
boolean istalkingFun = true;

// Using logical NOT (!) operator to negate the value
boolean istalkingNotFun = !istalkingFun;

System.out.println("Is talking not fun? " + istalkingNotFun);

}}

Output :

Is talking  not fun? false

3. Increment Operators (++): Used to increment the value of the integers by 1. Further classified as pre increment and post increment.


public class operators {
public static void main(String[] args) {
int x = 5;

System.out.println(" Orignal Value of x : " + x);
// Post-increment: The value of x is incremented after its current value is used

x++; //x = x+1

System.out.println("Value of x post-increment: " + x);

// pre increment
++x;
System.out.println("Value of x pre-increment: " + x);
}}

Output:

 Orignal Value of x : 5
Value of x post-increment: 6
Value of x pre-increment: 7

4. Decrement Operators (- -) : Used to decrement the value of the integers by 1. Further classified as pre decrement and post decrement.


public class operators {
public static void main(String[] args) {
int x = 5;

System.out.println(" Orignal Value of x : " + x);
// Post-decrement: The value of x is incremented after its current value is used

x--; //x = x+1

System.out.println("Value of x post-decrement: " + x);

// pre decrement
--x;
System.out.println("Value of x pre-decrement: " + x);
}}

5. Unary plus (+) Operator : it is used to represent positive values. Usually, we do not write the operator before the operand. Hence, it is optional.


public class operators {
public static void main(String[] args) {
int x = 10;
int y = +x; // Unary plus operator

// Output the original and updated values
System.out.println("Original value of x: " + x);
System.out.println("Value of y after unary plus operator: " + y);
}}

Output :

Original value of x: 10
Value of y after unary plus operator: 10

6. Bitwise complement operator (~) : In Java, numbers are stored in memory in binary form. It flips all the bits of the operand, changing each 0 to 1 and each 1 to 0.


public class operators {
public static void main(String[] args) {
int x = 5; // Binary: 0000 0101

int complementX = ~x; // Bitwise complement of 5: 1111 1010
System.out.println("Bitwise complement of " + x + " is: " + complementX);

int y = -5; // Binary: 1111 1011 (two's complement form of -5)
int complementY = ~y; // Bitwise complement of -5: 0000 0100
System.out.println("Bitwise complement of " + y + " is: " + complementY);


}}

Output:

Bitwise complement of 5 is: -6
Bitwise complement of -5 is: 4

Assignment Operators :

Assignment operators are used to assign values to variables. Further classified into :
Simple Assignment Operator (=),
Addition Assignment
(+=),
Subtraction Assignment (-=),
Multiplication Assignment (*=),
Division Assignment (/=) and
Modulus Assignment (%=)


public class operators {
public static void main(String[] args) {
int a = 5;
int b = 3;

// Simple Assignment
int c = a + b; // c = 5 + 3 = 8

// Addition Assignment
a += b; // Equivalent to: a = a + b; => a = 5 + 3 = 8

// Subtraction Assignment
b -= a; // Equivalent to: b = b - a; => b = 3 - 8 = -5

// Multiplication Assignment
c *= a; // Equivalent to: c = c * a; => c = 8 * 8 = 64

// Division Assignment
a /= 2; // Equivalent to: a = a / 2; => a = 8 / 2 = 4

// Modulus Assignment
b %= 3; // Equivalent to: b = b % 3; => b = -5 % 3 = -2

System.out.println("a: " + a); //
System.out.println("b: " + b); //
System.out.println("c: " + c); //
}}

Output:

a: 4
b: -2
c: 64

Relational Operators:

Relational operators are used to compare values and return a boolean result.

Equal to (==),
Not equal to (!=),
Greater than (>),
Less than (<),
Greater than or equal to (>=) and
Less than or equal to (<=)


public class operators {
public static void main(String[] args) {
int a = 10;
int b = 5;
// Equal to (==) operator
boolean isEqual = (a == b);
System.out.println("Is a equal to b? " + isEqual);

// Not equal to (!=) operator
boolean isNotEqual = (a != b);
System.out.println("Is a not equal to b? " + isNotEqual);

// Greater than (>) operator
boolean isGreater = (a > b);
System.out.println("Is a greater than b? " + isGreater);

// Less than (<) operator
boolean isLess = (a < b);
System.out.println("Is a less than b? " + isLess);

// Greater than or equal to (>=) operator
boolean isGreaterOrEqual = (a >= b);
System.out.println("Is a greater than or equal to b? " + isGreaterOrEqual);

// Less than or equal to (<=) operator
boolean isLessOrEqual = (a <= b);
System.out.println("Is a less than or equal to b? " + isLessOrEqual);
}
}

Output:

Is a equal to b? false
Is a not equal to b? true
Is a greater than b? true
Is a less than b? false
Is a greater than or equal to b? true
Is a less than or equal to b? false

Logical Operators :

In Java, logical operators are used to perform logical operations on boolean expressions. They evaluate conditions and return boolean values based on the result of the evaluation.

Logical AND (&&):

  • true && true returns true
  • true && false returns false
  • false && true returns false
  • false && false returns false

Logical OR (||):

  • true || true returns true
  • true || false returns true
  • false || true returns true
  • false || false returns false

Logical NOT (!):

!true returns false

!false returns true

Bitwise Operators :

Bitwise operators in Java are used to perform operations on individual bits of integer operands. Java provides several bitwise operators:

  • & (AND)
  • | (OR)
  • ^ (XOR)
  • ~ (Complement)
  • << (Left Shift)
  • >> (Right Shift)
  • >>> (Unsigned Right Shift)

These operators manipulate the bits of integer values at the binary level. They’re used in various scenarios such as bit manipulation, setting/clearing specific bits, or performing efficient arithmetic on binary data.


public class operators {
public static void main(String[] args) {
int a = 15; // 101 in binary
int b = 10; // 011 in binary

// Bitwise AND
int resultAnd = a & b; // 101 & 011 = 001
System.out.println("Bitwise AND result: " + resultAnd); //

// Bitwise OR
int resultOr = a | b; // 101 | 011 = 111
System.out.println("Bitwise OR result: " + resultOr); //

// Bitwise XOR
int resultXor = a ^ b; // 101 ^ 011 = 110
System.out.println("Bitwise XOR result: " + resultXor); //

// Bitwise NOT (Complement)
int resultComplementA = ~a; // ~101 = 11111111 11111111 11111111 11111010 (in 32-bit representation)
System.out.println("Bitwise Complement of a: " + resultComplementA); //

int resultComplementB = ~b; // ~011 = 11111111 11111111 11111111 11111000 (in 32-bit representation)
System.out.println("Bitwise Complement of b: " + resultComplementB); //
}}

Output :

Bitwise AND result: 10
Bitwise OR result: 15
Bitwise XOR result: 5
Bitwise Complement of a: -16
Bitwise Complement of b: -11

Proficiency with these operators is crucial for writing efficient Java code. Each operator type serves different purposes and plays important role in addressing diverse programming challenges and scenarios.

Exploring Further :

java-basics-exploring-overflow-and-underflow-concepts
java-basics-primitive-data-types-and-wrapper-classes-made-simple
introduction-to-java-variables

Thankyou !

--

--

Ritu Sitlani
Javarevisited

Hi, I'm an Engineer sharing insights on software testing and Java - Learning along the way!