Scanner scanner = new Scanner(System.in);
System.out.println("enter number :");
int number = scanner.nextInt();
System.out.println("Number is " + number);
let me go through step by step to explain my understanding,
Scanner scanner = new Scanner(System.in);
The Scanner is one of the java In-built classes and it is one of the ways we can use it to read user input.
scanner is the identifier of the object we created.
Using
new
, we create an instance of the object.System.in
helps us to take the inputs from the console(keyboard)System.out.println("enter number :");
prints the " enter number : ".
int number = scanner.nextInt();
Reads the input given to the system.in from console(keyboard) and parse(convert) the input to an int and stores it in the variable number.
System.out.println("Number is " + number);
prints the statement "Number is " + number(input stored in the variable number));
this is my vague understanding I think and I am not certain of my knowledge.
my questions are;
Scanner class is used to read user input, then System.in also is used to read input? isn't it? it will be a help to explain the clear distinction between these two terms?
when we type(enter) input from the console, where is it going? to the System.in?
does System.in storing the values?
- nextInt() method is also used to read inputs? how it works? is it read input that is already been read by the System.in and then parses to an int?
The scanner reads, System.in reads, nextInt() reads, everything reads? I can't able to distinguish the workings of these?