About Basic Arithmetic Operators In Java :
Basic Arithmetic Operators are sub group of
Java Arithmetic Operators which include only addition, subtraction, multiplication and division.
Basic Arithmetic Operators In Java With Example Program.
class BasicAlgebra
{
public static void main(String arg[])
{
int a = 3 + 4;
int b = a * 4;
int c = b - a;
int d = b / 2;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
}
}
OUTPUTa = 7
b = 28
c = 21
d = 14
DESCRIPTIONIn this example, 3 + 4
is assigned to a
which will be 7
,
a * 4
is assigned to b
which will be 7 * 4 = 28
,
b - a
is assigned to c
which will be 28 - 7 = 21
,
b / 2
is assigned to d
which will be 28 / 2 = 14
As we can see here, the operands could be literals (e.g., 3, 4), variables (e.g., a, b) or expressions.
THINGS TO TRY
- Create a new
int
variable e
and assign it an expression ( a + b ) - ( c * d )
and see the output by printing e
.
- Exchange the literal values and the operators used and see the output in those cases.