i

JAVA Complete Course

Arithmetic Operations

Let’s describe arithmetic operations very shortly with table, as it is so clear and similar like in mathematics.

 

Operator

Description

Example

+

Adds elements

int a = 5;

int b = 7;

int c = a + b;

// Result will be 7

-

Subtract elements

int a = 5;

int b = 7;

int c = a - b;

// Result will be -2

*

Multiplies

int a = 5;

int b = 7;

int c = a * b;

// Result will be 35

/

Divides (if variables are integers, divides as integers. But if variables are float or double, divides normally)

int a = 5;

int b = 7;

int c = a / b;

// Result will be 0

 

double x = 10;

double y = 4;

double z = x / y;

// Result will be 2.5

%

Module

int a = 7;

int b = 5;

int c = a % b;

// Result will be 2