Operators In Java


Introduction

When writing Java programs, you'll often need to work with operators. Operators allow us to perform actions such as modifying variables, performing calculations, and checking conditions. They're a fundamental building block of Java programming, so understanding them is essential.

In Java, operators are grouped into several categories:

  • Arithmetic Operators:

    Perform mathematical calculations.

  • Assignment Operators:

    Assign values to variables.

  • Comparison Operators:

    Compare values or variables and return either true or false.

  • Logical Operators:

    Evaluate conditions as true or false.

Arithmetic Operators

Operator Name Description
+ Addition Adds two values
- Subtraction Subtracts one value from another
* Multiplication Multiplies two values
/ Division Divides one value by another
% Modulus Returns the remainder after division
++ Increment Increases the value of a variable by 1
-- Decrement Decreases the value of a variable by 1

Aside: Integer vs. Double Division

When dividing two numbers using the division operator in Java, the data types determine the result.

  • Integer Divison:

    This occurs when both values are integers and will discard the decimal part of the result.

  • Double Division:

    This occurs when one or both values are doubles and will include the decimal part of the result.

For examples see the below section

Arithmetic Operator Examples

// Note that the comments here are not needed but are included for explanatory purposes

// Addition Operator
int sum = 10 + 5;  // sum would store the value 15 

// Subtraction Operator 
int difference = 10 - 3;  // difference would store the value 7  

// Multiplication Operator
int product = 4 * 3;  // product would store the value 12  

// Integer Division  
int intResult = 12 / 5;  // intResult would store the value 2  

// Double Division  
double doubleResult = 12.0 / 5.0;  // doubleResult would store the value 2.4  

// Modulus Operator
int remainder = 10 % 3;  // remainder would store the value 1  

// Increment Operator
int counter = 1;
counter++;  //counter would store the value 2

// Decrement Operator
int counter = 2;
counter--;  //counter would store the value 1a

Assignment Operators

Operator Name Description
= Assignment Assigns a value to a variable
+= Add and Assign Adds a value to a variable and then assigns the result
-= Subtract and Assign Subtracts a value from a variable and then assigns the result
*= Multiply and Assign Multiplies a variable by a value and then assigns the result
/= Divide and Assign Divides a variable by a value and then assigns the result
%= Modulus and Assign Finds the remainder of a division and then assigns the result

Assignment Operator Examples

// Note that the comments here are not needed but are included for explanatory purposes
// Assume the below lines of code occur in sequence

// Assignment Operator
int baseValue = 10;  // baseValue will store the value 10

// Add and Assign Operator
baseValue += 5;  // This is equivalent to baseValue = baseValue + 5; so baseValue will store the value 15

// Subtract and Assign Operator
baseValue -= 6; // This is equivalent to baseValue = baseValue - 6; so baseValue will store the value 9

// Multiply and Assign Operator
baseValue *= 2;  // This is equivalent to baseValue = baseValue * 2; so baseValue will store the value 18

// Divide and Assign Operator
baseValue /= 3;  // This is equivalent to baseValue = baseValue / 3; so baseValue will store the value 6

// Modulus and Assign Operator
baseValue %= 5;  // This is equivalent to baseValue = baseValue % 5; so baseValue will store the value 1
Operator Name Description
== Equal To Determines if two values are equal
!= Not Equal To Determines if two values are not equal
> Greater Than Determines if one value is greater than another
< Less Than Determines if one value is less than another
>= Greater Than or Equal To Determines if one value is greater than or equal to another
<= Less Than or Equal To Determines if one value is less than or equal to another

Comparison Operators

Comparison Operator Examples

// Note that the comments here are not needed but are included for explanatory purposes
// This section will not use variables however the result of a comparison can be assigned as follows 
// boolean value = (condition);

// Equal To Operator
5 == 5  // This evaluates to true
5 == 6  // This evaluates to false

// Not Equal To Operator 
5 != 4  // This evaluates to true
5 != 5  // This evaluates to false

// Greater Than Operator 
5 > 3  // This evaluates to true
5 > 7  // This evaluates to false

// Less Than Operator
6 < 9  // This evaluates to true
6 < 2  // This evaluates to false

//Greater Than or Equal To Operator
8 >= 8  // This evaluates to true
8 >= 7  // This evaluates to false 

//Less Than or Equal To Operator
1 <= 2 // This evaluates to true
1 <= 0 // This evaluates to false
Operator Name Description
&& Logical AND Returns true if both conditions are true
|| Logical OR Returns true if at least one of the conditions are true
! Logical NOT Reverses the logical state of a condition

Logical Operators

Logical Operator Examples

// Note that the comments here are not needed but are included for explanatory purposes
// Assume the below lines of code occur in sequence

// Logical AND Operator
boolean trueCondition = true;
boolean falseCondition = false; 

boolean andTest = trueCondition && falseCondition;  // This evaluates to false

// Logical OR Operator 
boolean orTest = trueCondition || falseCondition;  // This evaluates to true

// Logical Not Operator
boolean notTest = !trueCondition;  // This evaluates to false

Conclusion

Operators are fundamental to Java programming, providing the tools necessary for everything from simple calculations to complex decision-making processes. A solid understanding of these operators is essential for developing efficient and effective code. By mastering these core concepts, you are laying the groundwork for building more sophisticated and robust programs in the future.