Oh No, More Operators!

Yes there are many more operators, but they are all very useful. Here is a table of three more operators.




Increment operator


Decrement operator


Modulus — This is the remainder from a division calculation

The increment and decrement operators will add or subtract a value of one from a variable of a numeric data type. They can be used with both integers and floating-point variables. The increment and decrement operators are simply neater implementations of the following code:

int counter = 0;
counter = counter + 1;
int countdown = 10.0;
countdown = countdown – 1;

Instead, we can use this code:

int counter = 0;
double countdown = 10.0;

This method of using these operators is the postfix method, which means they are entered on the right-hand side of their associated operand. You can use the increment and decrement operators in a postfix or prefix form, giving different results each time. Take the following segments of code as an example:

int numberA = 10;
int numberB = numberA++;

This code assigns the current value in numberA to numberB and then increments the value of numberA after that. So the result of this code leaves numberA equal to 11 and numberB equal to 10. If we wanted the increment code to execute first and then the assignment of numberB afterwards, we would use the prefix increment operator instead. The code would now be as follows:

int numberA = 10;
int numberB = ++numberA;

As you can see, the increment (++) operator is now entered on the left-hand side of its associated operand. This code first increments the value of numberA by 1 and then assigns the new value of numberA to numberB. The result is that both variables are equal to 11. The modulus operator % is used to calculate the remainder value of a division calculation. We can see the use of the modulus operator in the following example:

public class Eggsample
 public static void main(String args[])
 { int totalEggs = 15;
 int eggsPerBox = 6;
 int filledBoxes = totalEggs / eggsPerBox;
 int remainingEggs = totalEggs % eggsPerBox;
 System.out.println("Number Of Eggs = " + totalEggs);
 System.out.println("Eggs Per Box = " + eggsPerBox);
 System.out.println("Filled Boxes = " + filledBoxes);
 System.out.println("Remaining Eggs = " + remainingEggs);

When you compile and run, you should get output similar to the following screen shot.

Java Click To expand

As you can see from the console output, there are two filled boxes of eggs, calculated using integer division, which will ignore any remainder values. The amount of remaining eggs is given using the modulus operator.