如果我们没有为它创建对象,如何关闭扫描器类以避免警告消息指出unassigned closable value, resource leak
?
因为我只需要从用户那里获得一次输入,所以我没有为对象 Scanner class
创建引用变量。
我获取输入的声明
int num = new Scanner(System.in).nextInt();
最佳答案
i did not created object for Scanner class
是的,你做到了:new Scanner(System.in)
以防万一,您可以使用 try-with-resource:
try(Scanner sc=new Scanner(System.in)){
int num = sc.nextInt();
/* TO DO */
}
或者,只需使用 .close():
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
sc.close();
关于java - 如果我们没有为对象创建引用变量,如何关闭扫描器类,那么如何避免资源泄漏警告消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52050299/