为什么我需要添加"L"字母才能获得正确的长值?另一个价值是什么?
long oneYearWithL = 1000*60*60*24*365L;
long oneYearWithoutL = 1000*60*60*24*365;
System.out.println(oneYearWithL);//gives correct calculation result : 31536000000
System.out.println(oneYearWithoutL)//gives incorrect calculation result: 1471228928
Run Code Online (Sandbox Code Playgroud) 我有一个关于在java中从int转换为long的问题.为什么浮标没有问题:
float f = (float)45.45;//compiles no issue.
float f = 45.45f; //compiles no issue.
Run Code Online (Sandbox Code Playgroud)
但是对于长型,它似乎是一个问题:
long l = (long)12213213213;
//with L or l at the end it will work though.
long l = (long)12213213213L;
Run Code Online (Sandbox Code Playgroud)
似乎一旦编译器因超出范围的问题而通知错误,它就会在那里阻塞而不检查程序员可能已经计划的任何可能的转换.
你对它有什么看法?为什么有任何特殊原因?
提前致谢.