the usage of close() method(Java Beginner)

為{幸葍}努か 提交于 2019-12-23 02:05:17

问题


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.


回答1:


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.

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




回答2:


The most effective example is file object.When you create a file object, you exit the file using the close() method,otherwise it will be kept open while you're doing some other operations.



来源:https://stackoverflow.com/questions/35419715/the-usage-of-close-methodjava-beginner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!