Programming & Scripting Tutorials

Java: Arithmetic





Basic arithmetic is extremely simple in java (as they are in many languages).
I think the best way for you to learn about this is to see an example:

public class Main {


    public static void main(String[] args) {

        double Num1, Num2, Total;
        Num1 = 13.8;
        Total = Num1 + 12;
        System.out.println("Num1 + 12 = " + Total);
        Num1 = 15.5;
        Num2 = 15.1;
        Total = Num1 + Num2;
        System.out.println("Num1 + Num2 = " + Total);
        Total = 12 + 12;
        System.out.println("12 + 12 = " + Total);
    }

}

As you might have guessed this outputs:

Num1 + 12 = 25.8 Num1 + Num2 = 30.6 12 + 12 = 24.0

We can also subtract, multiply, and divide numbers in this type of way; Apart from instead of using "+" you would use one of the following:

"-" Minus
"*" Multiply
"/" Divide

This Java tutorial was written by


Back to Java

Advertisement: