Results 1 to 8 of 8
  1. #1
    hacikho is offline Member
    Join Date
    Sep 2009
    Posts
    35
    Rep Power
    0

    Default 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]
    	} 
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by hacikho View Post
    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?
    My question to you: if you don't know what a close() method does, why did you type it in the first place()?

    kind regards,

    Jos

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Quote Originally Posted by pbrockway2 View Post
    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?
    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:
    Java Code:
    try(InputStream  in  = new FileInputStream(src)){ ....}
    See: http://blogs.sun.com/darcy/resource/...rojectCoin.pdf

  5. #5
    hacikho is offline Member
    Join Date
    Sep 2009
    Posts
    35
    Rep Power
    0

    Default

    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.

  6. #6
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Quote Originally Posted by hacikho View Post
    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 attitue. 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.
    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.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    @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.

  8. #8
    hacikho is offline Member
    Join Date
    Sep 2009
    Posts
    35
    Rep Power
    0

    Default

    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

  1. Replies: 13
    Last Post: 04-09-2010, 11:15 AM
  2. How to Close Combobox.
    By ocean in forum New To Java
    Replies: 4
    Last Post: 11-17-2009, 04:50 PM
  3. close to zero
    By nokicky in forum New To Java
    Replies: 6
    Last Post: 10-26-2009, 04:30 PM
  4. [SOLVED] Close Exe
    By smartsubroto in forum New To Java
    Replies: 11
    Last Post: 10-14-2008, 08:04 AM
  5. How to close a JFrame
    By valery in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 05:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •