close()方法的用法(Java初学者) [英] the usage of close() method(Java Beginner)

查看:449
本文介绍了close()方法的用法(Java初学者)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想关闭Scanner对象或任何其他对象,如果我不使用x.close()会发生什么。close()方法的用法是什么。请举例说明如果我们不知道会发生什么不要使用close方法。

What would really happen if I don't use x.close() if I want to close a Scanner object or any other object.What is the usage of close() method.Please give example of what happens if we don't use close method.

推荐答案

方法的目的实际上取决于它所属的类,但在你的例子中(扫描仪)在大多数情况下,涉及一些IO操作,如文件阅读器,流,连接等 - close()方法背后的想法是关闭连接并释放资源,这通常是消耗程序内存的东西。

The method's purpose really depends on the class it belongs to, but in your example (Scanner) and in most cases that involves some IO operations like file reader, streams, connection and such - the idea behind close() method is to close the connection and release the resource, which is usually something that consumes memory from the program.

如果你不这样做 - 好吧,你使用的是你不需要的资源,这将使cpu /内存比你需要的更难工作,所以它是建议在完成对象

If you don't do it - well, you're using a resource you don't need, which will make the cpu/memory work harder than it needs, so it is recommended to always use it when you're done with the object

这篇关于close()方法的用法(Java初学者)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆