Java Puzzlers笔记--Puzzle 3: Long Division 溢出问题

    技术2022-05-11  79

    public  class LongDivision{     public static void main(String[] args){        final long MICROS_PER_DAY = 24*60*60*1000*1000;        final long MILLIS_PER_DAY = 24*60*60*1000;        System.out.println(MICROS_PER_DAY / MILLIS_PER_DAY);     } } Solution:     显示: 5    因为 24*60*60*1000*1000的结果是int类型,再把这个结果转换成long的,但是24*60*60*1000*1000的结果已经溢出,不能用int表示。 TID:     When working with large numbers, watch out for overflow-it's a silent killer. Correctly: public  class LongDivision{     public static void main(String[] args){        final long MICROS_PER_DAY = 24 L*60*60*1000*1000;        final long MILLIS_PER_DAY = 24 L*60*60*1000;        System.out.println(MICROS_PER_DAY / MILLIS_PER_DAY);     } }

    最新回复(0)