Key points

  • The rounding rule of int division: int division keeps only the whole-number part and simply drops the decimal part: 29 / 10 prints 2, not 2.9 and not 3.
  • Integer division with negative numbers: 11 / 3 prints 3, and both -11 / 3 and 11 / -3 print -3.
  • How operand types affect the result: when both operands are int, the decimal part is dropped, even if avg is a double (9 / 2 stored in the double avg prints 4.0).
  • How to get a decimal result: change the kmPer or the meters from int to double
  • What happens when an integer is divided by 0: answer E: it compiles, and at run time it throws java.lang.ArithmeticException (/ by zero); the program ends right there.

Code and examples

1
2
3
4
5
6
7
public class Main {
public static void main(String[] args) {
int total = 29;
int days = 10;
System.out.println(total / days);
}
}
1
2
3
4
5
6
7
8
public class Main {
public static void main(String[] args) {
int a = 11, b = 3;
System.out.println(a / b);
System.out.println(-a / b);
System.out.println(a / -b);
}
}
1
2
int completed = 7;
double ratio = completed / 4.0;
1
2
3
4
5
6
7
8
public class Main {
public static void main(String[] args) {
int meters = 2500;
int kmPer = 1000;
double km = meters / kmPer;
System.out.println(km);
}
}
1
2
3
4
5
6
7
public class Main {
public static void main(String[] args) {
int totalRows = 40;
int rowsPerPage = 0;
System.out.println("pages=" + totalRows / rowsPerPage);
}
}

Going further

  • Remainder % and integer division / go together: -7 / 3 is -2 and -7 % 3 is -1; the remainder takes the sign of the dividend.
  • Math.floorDiv(-7, 3) gives -3 and Math.floorMod(-7, 3) gives 2. They round toward negative infinity, unlike -7 / 3, which truncates toward zero.
  • Only integer division by 0 throws an exception: 5.0 / 0 prints Infinity and 0.0 / 0 prints NaN.

要点

  • 能说出 int 除法结果的取整规则:int 整除只保留整数部分,自动忽略小数部分:29 / 10 打印 2,既不是 2.9 也不是 3。
  • 能判断含负数的整数除法表达式的值:11 / 3 打印 3,-11 / 3 和 11 / -3 都打印 -3。
  • 能解释操作数类型如何影响除法结果:两个操作数都是 int 时,小数部分就被忽略了,即便 avg 是 double 类型(9 / 2 存进 double avg 打印 4.0)。
  • 能说出怎样让除法得到小数结果:change the kmPer or the meters from int to double
  • 能判断整数除数为 0 时会发生什么:选 E:It compiles, and at run time it throws java.lang.ArithmeticException (/ by zero); the program ends right there。

代码与例句

1
2
3
4
5
6
7
public class Main {
public static void main(String[] args) {
int total = 29;
int days = 10;
System.out.println(total / days);
}
}
1
2
3
4
5
6
7
8
public class Main {
public static void main(String[] args) {
int a = 11, b = 3;
System.out.println(a / b);
System.out.println(-a / b);
System.out.println(a / -b);
}
}
1
2
int completed = 7;
double ratio = completed / 4.0;
1
2
3
4
5
6
7
8
public class Main {
public static void main(String[] args) {
int meters = 2500;
int kmPer = 1000;
double km = meters / kmPer;
System.out.println(km);
}
}
1
2
3
4
5
6
7
public class Main {
public static void main(String[] args) {
int totalRows = 40;
int rowsPerPage = 0;
System.out.println("pages=" + totalRows / rowsPerPage);
}
}

延伸

  • 取余 % 和整除 / 是一对:-7 / 3 是 -2,-7 % 3 是 -1,余数的符号跟被除数一致。
  • Math.floorDiv(-7, 3) 得 -3,Math.floorMod(-7, 3) 得 2,它们向负无穷取整,和 -7 / 3 的朝零截断不同。
  • 除数 0 只有整数才抛异常:5.0 / 0 打印 Infinity,0.0 / 0 打印 NaN。

相关