java - java中2个6位数字的乘法
问题描述
为什么下面代码的输出输出的是 1345094336 而不是 39999800000?我应该如何编辑它?我相信这与整数溢出有关。
public class testC {
public static void main(String[] args) {
long product = 199999 * 200000;
System.out.println(product);
}
}
解决方案
两个整数 199999 * 200000 的乘积是 39999800000,大于整数
storage capacity.
width minimum maximum
SIGNED
byte: 8 bit -128 +127
short: 16 bit -32 768 +32 767
int: 32 bit -2 147 483 648 +2 147 483 647
long: 64 bit -9 223 372 036 854 775 808 +9 223 372 036 854 775 807
UNSIGNED
char 16 bit 0 +65 535
因此,您需要在乘法端将至少一个数字转换为 long
(long)199999 * 200000
199999 * (long)200000
199999 * 200000L
199999L * 200000
推荐阅读
- go - Golang - [...] 接口的总和{}
- flutter - 如何使用 MethodChannel 在 Native 上显示颤振视图?
- ios - 如何检查 JSON 响应的 null 条件?
- sonarqube - SonarQube 投资组合计算失败
- pandas - 在列表类型数据框中出现字符
- angular - Angular 7 - 安装本地库(未发布) - 项目设置
- node.js - Express Gateway 禁用 TLSv1.0
- django - How can I apply filter to Django modelform and return form to view in Django?
- javascript - 比较两个不同数组的值
- javascript - 如何获取用户在手机上使用的浏览器版本?