我一直在看代码,我已经看过尝试资源.之前我使用过标准的try-catch语句,看起来它们做同样的事情.所以我的问题是Try With Resources vs Try-Catch 它们之间有什么区别,哪个更好.
这是资源的尝试:
objects jar = new objects("brand");
objects can= new objects("brand");
try (FileOutputStream outStream = new FileOutputStream("people.bin")){
ObjectOutputStream stream = new ObjectOutputStream(outStream);
stream.writeObject(jar);
stream.writeObject(can);
stream.close();
} catch(FileNotFoundException e) {
System.out.println("sorry it didn't work out");
} catch(IOException f) {
System.out.println("sorry it didn't work out");
}
Run Code Online (Sandbox Code Playgroud) 我什么时候应该定义一个变量?在if语句中使用原始值与变量一次使用会更快吗?例如,原始值:
if(variable == 503) {
//run code
}
Run Code Online (Sandbox Code Playgroud)
VS这个:
if(variable == anotherVariable) {
//run some code
}
Run Code Online (Sandbox Code Playgroud)
我正在寻找哪一个更快更安全(在if语句和一般情况下).