Results 1 to 8 of 8
Thread: funtion of close() method?
- 10-10-2010, 07:34 PM #1
Member
- Join Date
- Sep 2009
- Posts
- 35
- Rep Power
- 0
funtion of close() method?
I have below codes everthing is working fine, however I do not understant the reason of using in.close(); method at the end, program works fine without of in.close(); also, but what is the purpose of using it?
Java Code:import java.util.Scanner; public class SongTime { public static void main(String[] args) { //Declartaion int seconds; double price; String name; Scanner in = new Scanner(System.in); //Prompt for input System.out.println("Enter the price , the time of the song in second,"); System.out.println(" and the name of the song, all seperated blanks: "); //Read values price = in.nextDouble(); seconds = in.nextInt(); name = in.nextLine(); //Output song name, cost, and time in minutes and seconds System.out.println(); System.out.println(name + "cost$" + price + " and is " + seconds/60 + " minute(s), and " + seconds%60 + " second(s) long."); double costPerMinute = price/((double)seconds / 60.0); System.out.println("Cost per minute is $ "+ costPerMinute); [COLOR="Red"]in.close();[/COLOR] } }
- 10-10-2010, 07:37 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,372
- Blog Entries
- 7
- Rep Power
- 17
- 10-10-2010, 08:48 PM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
That's a good question.
Actually a couple of good questions, but I'll try to address the OP's to the extent that I can. Some methods involve obtaining resources (memory for example) from the OS and these resources should be given back when they are no longer needed. In the case of I/O things like file handles are like this.
The problem is that we don't work with such OS specific details directly when using Java. So what we do is call close() when we have finished and let the implementation of the particular class do whatever it should to ensure that proper "clean up" is done.
Another circumstance that points towards this call-it-in-case approach is that Java I/O often uses wrapped classes where each class adds its own functionality (buffering etc). We may not even know the exact sort of stream that is actually being wrapped much less what clean up is required. Again we just call close() and leave it to the wrapper classes to do the right thing (often this is to tell the thing they wrap to close()).
-----
I hazarded my own explanation because I couldn't find anything in Oracle's Tutorial, or the Closeable interface API docs. Also because I wonder if attitudes like this Google Books example might be commonplace in published material.
If I have made huge mistakes I'm sure someone will chime in. But also there may be other questions raised by this: like what are the problems or possibility of Java using some automatic method of closing, like it has automatic GC? Other languages have a "with" statement that attempts this.
- 10-10-2010, 09:00 PM #4
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Java 7 - Project Coin - will give us "try-with-resources" (formerly known as Automatic Resource Management, ARM)
So you are able to write something like that:
See: http://blogs.sun.com/darcy/resource/...rojectCoin.pdfJava Code:try(InputStream in = new FileInputStream(src)){ ....}
- 10-10-2010, 09:38 PM #5
Member
- Join Date
- Sep 2009
- Posts
- 35
- Rep Power
- 0
Dear JosAH
This program is an example on my college book, I am trying to learn Java, and try to understand examples like as every Java beginner. You can not help me by questioning, for the next time if you know the answer you have right to answer or not, but not questioning with attitudee. Do not waste your time on my questions, because you are not helping, moreover giving negative energy. Because your reply is not meet the purpose of this JAVA FORUMS.
Thanks, those of you who gave information and explain the answer.Last edited by hacikho; 10-10-2010 at 09:43 PM.
- 10-10-2010, 09:45 PM #6
You'll have to excuse us for not wanting to have people steal a bunch of code from random places and ask why it works. JosAH's question was completely valid and didn't need to be answered with such a reply. A simple, "it's an example from my college textbook" would have sufficed.
- 10-10-2010, 11:44 PM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
@OP: there's no reason to be defensive - actually you and he were making somewhat related points. Just why *do* we close() things when the published API gives little or no clue as to what (if anything) might happen in some particular case? My answer was supposed to be a rationale for the call-it-anyway-just-in-case approach. A discussion like this - mediated by brief text messages - is best approached with a skin: to protect against things like questions that might sound as if they were more personally directed than is the case.
@eRaaaa Thanks for that.
- 10-11-2010, 01:08 AM #8
Member
- Join Date
- Sep 2009
- Posts
- 35
- Rep Power
- 0
You guys have more experience than I have at this forum, If you think there is no need to be defensive, and these are normal to understand the case clear, I respect all of you who have more knowledge and experience than me. I do know where to stand. However I might be little bit touchy, because I thought JosAH does not want to help. I do apologize from JosAH. pbrockway2 and Zack thank for you to make the case more clear and give information about how things going on here.
Similar Threads
-
Will the connection.close() and statement.close() ever be called???
By Stephen Douglas in forum New To JavaReplies: 13Last Post: 04-09-2010, 11:15 AM -
How to Close Combobox.
By ocean in forum New To JavaReplies: 4Last Post: 11-17-2009, 04:50 PM -
close to zero
By nokicky in forum New To JavaReplies: 6Last Post: 10-26-2009, 04:30 PM -
[SOLVED] Close Exe
By smartsubroto in forum New To JavaReplies: 11Last Post: 10-14-2008, 08:04 AM -
How to close a JFrame
By valery in forum New To JavaReplies: 1Last Post: 08-06-2007, 05:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks