I was solving a competitive problem and in problem, I was taking user input using scanner.
These are 2 code segments, one closing scanner and one without closing scanner.
Closing scanner
import java.util.Scanner;
public class JImSelection {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = Integer.valueOf(scanner.nextLine());
while (n-- > 0) {
double number = (Math.log(Long.valueOf(scanner.nextLine())) / Math.log(2));
System.out.println((int) number - number == 0 ? "Yes" : "No");
}
scanner.close();
}
}
Not closing scanner
import java.util.Scanner;
public class JImSelection {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = Integer.valueOf(scanner.nextLine());
while (n-- > 0) {
double number = (Math.log(Long.valueOf(scanner.nextLine())) / Math.log(2));
System.out.println((int) number - number == 0 ? "Yes" : "No");
}
}
}
The first one (closing scanner) give me score 14.47
,
and second one (not closing scanner) gives 15.22
.
I think compiler is freeing the resource when i use scanner.close();
and that is why there is a difference in the scores.
This is the score judge formula.
It is assigned a score of 100. Suppose, you submit a solution of n characters, then your score is (56/n)*100.