Division Assignment
In this step, you will learn how to perform a division using the division assignment operator /=
, which is a shorthand notation for x = x / y
.
public class IntegerDivision {
public static void main(String[] args) {
int a = 10;
int b = 3;
double c = 2.0;
c /= b;
System.out.println("2.0 / " + b + " = " + c);
}
}
We define an int
variable a
, an int
variable b
, and a double
variable c
. We initialize c
to 2.0
. Then we use the division assignment operator to divide c
by b
. Finally, we print the result.
To run the code, use the same command as in step 2:
javac IntegerDivision.java && java IntegerDivision
The output should be:
2.0 / 3 = 0.6666666666666666