Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it necessary to close Scanner when there is no stream underlying?

Directly from this Scanner API:

String input = "1 fish 2 fish red fish blue fish";
     Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
     System.out.println(s.nextInt());
     System.out.println(s.nextInt());
     System.out.println(s.next());
     System.out.println(s.next());
     s.close(); 
like image 256
Rollerball Avatar asked Jul 30 '13 11:07

Rollerball


2 Answers

First we make our habits; then our habits make us.

I'd say close the stream, because ingraining a habit like closing resources in finally blocks make sense.

like image 104
duffymo Avatar answered Sep 19 '22 17:09

duffymo


I don't think so, but it's best to close it anyway in case you change your code to use a stream. It's also a good habit, one that you really want to get into.

like image 33
tbodt Avatar answered Sep 17 '22 17:09

tbodt