我是一个java新手.我有一个关于如何在使用try catch finally块时组织java代码的问题.假设我必须阅读一些文本文件并对存储的文件内容进行一些计算.我的代码应该怎么样?
例如
代码1看起来像:
public static void main(String[] args){
try{
//open files using BufferedReader, read and store the file contents.
}catch(IOException e){
e.printStackTrace();
}
finally{
//close the files
}
// do computations on the data
}
Run Code Online (Sandbox Code Playgroud)
代码2看起来像:
public static void main(String[] args){
try{
//open files using BufferedReader, read and store the file contents.
// do computations on the data
}catch(IOException e){
e.printStackTrace();
}
finally{
//close the files
}
}
Run Code Online (Sandbox Code Playgroud)
哪两个是更好的编码实践?也应该在try catch之后最终阻止放置,或者它可以放在最后.
今天我遇到了Math.pow()的特殊行为.我无法理解以下java代码的输出:
long N1 = 999999999999999999L;
System.out.println("N1 : " + N1);
long N2 = (long) Math.pow(N1, 1);
System.out.println("N2 : " + N2);
Run Code Online (Sandbox Code Playgroud)
我得到以下输出:
N1 : 999999999999999999
N2 : 1000000000000000000
Run Code Online (Sandbox Code Playgroud)
我一直以为Math.pow()会产生精确的结果,只要传递给它的参数是整数或长整数,前提是没有溢出(在这种情况下也是如此).