java – () or Scanner?


I noticed in many books it is used: is to get something from the keyboard. Why isn't the Scanner class used there? Because is faster than creating an instance of the Scanner class and then creating such a construction:

Scanner scanner = new Scanner(;
String name = scanner.nextLine();


Scanner scanner = new Scanner(;
int x = scanner.nextInt();

Searched Stackoverflow and found no difference between and Scanner class.


There is a difference, and, moreover, a significant one. can only read 1 character as char .
At the same time, Scanner is a versatile piece that allows you to read in the format you need.
If you need to count by characters, then you should not bother, but use . In other cases, these are, in particular, files, we use Scanner .

Scroll to Top