Reputation: 17
Whenever I use Scanner class for input it shows a warning regarding memory leak, ".in" not closed. While using Buffered Reader it does not show any warning. Why So?
class Demo
{
public static void main(String[] arg){
System.out.println("Enter a String");
Scanner sc = new Scanner(System.in);
String[] str = new String[1];
str[0] = sc.next();
System.out.println(str[0]);
}
Upvotes: 1
Views: 1148
Reputation: 83557
In either case, you should close the Scanner
or BufferedReader
to avoid resource leaks. The compiler apparently doesn't recognize the problem with BufferedReader
and doesn't issue the warning.
Upvotes: 2