int类型数字特别大
package com.company;
public class Demo06 {
? ? public static void main(String[] args) {
? ? ? ? //操作比較大數(shù)時(shí)注意溢出問題
? ? ? ? //JDK7新特性,數(shù)字之間可以用下劃線分隔
? ? ? ? int money=10_0000_0000;
? ? ? ? System.out.println(money);
? ? ? ? int years=20;
? ? ? ? int total=money*years;
? ? ? ? System.out.println(total);//-1474836480計(jì)算時(shí)溢出了
? ? ? ? long total2=money*years;
? ? ? ? System.out.println(total2);//-1474836480默認(rèn)是int,轉(zhuǎn)換之前已經(jīng)存在問題了
? ? ? ? long total3=money*((long)years);//先把一個(gè)數(shù)轉(zhuǎn)換為long
? ? ? ? System.out.println(total3);//20000000000正確
? ? }
}
1000000000
-1474836480
-1474836480
20000000000
加qq群422464063學(xué)習(xí)
總結(jié)
以上是生活随笔為你收集整理的int类型数字特别大的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python int函数 - Pytho
- 下一篇: C语言 数组定义和使用 - C语言零基础