为什么第一个和第一个1的结果不同?我猜它与Long类型的限制有关.
long seconds = System.currentTimeMillis();
long first = (seconds / (1000*60*60*24))/365;
long first1 = seconds / (1000*60*60*24*365);
System.out.println(first);
System.out.println(first1);
Run Code Online (Sandbox Code Playgroud)
谢谢!
第二个分母溢出了这个int
类型.
如果你这样做没有区别 - 全程使用:
public class Overflow
{
public static void main(String[] args)
{
long seconds = System.currentTimeMillis();
long first = (seconds / (1000L * 60L * 60L * 24L)) / 365L;
long i = 1000L * 60L * 60L * 24L * 365L;
long first1;
first1 = seconds / i;
System.out.println(i);
System.out.println(Integer.MAX_VALUE);
System.out.println(first);
System.out.println(first1);
System.out.println(first1/first);
}
}
Run Code Online (Sandbox Code Playgroud)