KnowledgeBoat Logo

Operators

Basic Arithmetic Operators

ICSE Computer Applications



We use Arithmetic operators to perform common mathematical operations. Operands of Arithmetic operators must be of numeric type. This implies that Arithmetic operators can operate on byte, short, int, long, float, double and char. But they cannot operate on boolean.

This table lists all the different Arithmetic operators in Java:

OperatorNameDescriptionExample
Unary +Unary PlusReturns value of the operand+x
Unary -Unary minusReturns negated value of the operand-x
+AdditionAdds together two valuesx + y
-SubtractionSubtract one value from anotherx - y
*MultiplicationMultiplies two valuesx * y
/DivisionDivides one value from anotherx / y
%ModulusReturns the division remainderx % y
++IncrementIncreases the value of a variable by 1++x or x++
--DecrementDecreases the value of a variable by 1--x or x--

The first six - Unary Plus, Unary Minus, Addition, Subtraction, Multiplication and Division are the same as in algebra.

Let’s look at a BlueJ program to see these operators in action:

public class BasicArithmeticOperators
{
    public void demoBasicArithmeticOps() {

        /*
         * Addition operator adds both 
         * of its operands 10 and 10 
         * to give the result as 20
         */ 
        int a = 10 + 10;
        
        /*
         * Multiplication operator 
         * multiplies its first 
         * operand which in this 
         * case is the variable a 
         * with its second operand 
         * which is the integer 
         * literal 3 to give the 
         * result as 60
         */
         int b = a * 3;

         /*
         * Division operator divides 
         * its first operand with 
         * second so b which has a
         * value of 60 is divided by  
         * 4 to give the result as 15
         */
        int c = b / 4;

        /*
         * Subtraction operator subtracts
         * the second operand from the
         * first so a is subtracted from 
         * c to give the result as -5
         */
        int d = c - a;
        
        /*
         * Unary Minus operator negates  
         * the value of its operand. 
         * d has the value of -5.
         * On negation it becomes 5
         * and gets assigned to e
         */
        int e = -d;
        
        System.out.println("a = " + a);
        System.out.println("b = " + b);
        System.out.println("c = " + c);
        System.out.println("d = " + d);
        System.out.println("e = " + e);

    }
}

Let’s run the program and see the output.

Difference Between Integer and Floating-Point Division

What will be the output of the below program?

public class IntegerDivision
{
    public void demoIntegerDivision() {
        int a = 18;
        double b = a / 4;
        System.out.println("b = " + b);
    } 
}

Did you say 4.5? Let’s run the program and check if you are correct.

The result is 4.0 and not 4.5. When both operands are of integer type, the division operator does not include the fractional component in the result.

To get the fractional component in the result, one operand needs to be of floating-point type like this:

public class FpDivision
{
    public void demoFpDivision() {
        int a = 18;
        /*
         * Here, the denominator 
         * is written as a
         * floating-point literal
         * 4.0 instead of integer
         * literal 4
         */
        double b = a / 4.0;
        System.out.println("b = " + b);
    } 
}

Now the result is 4.5 because we have written the denominator of the division operator as a floating-point literal 4.0 instead of an integer literal 4.

For floating-point division, either the numerator or the denominator or both should be of floating-point type.

PrevNext