New to Java, could you explain something to me?
Hi, I am new to java, and I have been following some youtube video tutorials and I'm not clear on something, so could you please explain it.
Here is the code:
Code :
import java.util.Scanner;
public class GetUserInput {
public static void main(String args[]){
Scanner number = new Scanner(System.in);
System.out.println(number.nextLine());
}
}
On the line
Code :
Scanner number = new Scanner(System.in);
I am creating a variable called number of the Scanner class, and then I am assigning it something, this is what I'm confused with. I know the new keyword creates an instance of a class but I dont know what is being assigned to my number variable?
Thanks.
Re: New to Java, could you explain something to me?
Re: New to Java, could you explain something to me?
new Scanner(System.in) is a call to the constructor for a Scanner object. What you're doing is creating a new Scanner object then assigning that object to the variable number (for a more verbose answer, the variable number "references" or "points to" that Scanner object that was created).
The System.in is a parameter of the Scanner constructor. It's the stdin stream (i.e. the stream that console input is read from). It's a static field found in the class System, and the field's name is in.
Re: New to Java, could you explain something to me?
Thanks for replying. I have a few questions:
1. So, in
Code :
Scanner number = new Scanner(System.in);
I am assigning number the reference to a Scanner object?
2. With that being said, that means when I refer to the number variable I am using the scanner object?
3. And then we are giving the Scanner object a parameter of our keyboard input, so when it comes to the line
Code :
System.out.println(number.nextLine());
it displays the values I type it, althought it pauses and waits until I type in values because none have yet been entered?
Re: New to Java, could you explain something to me?
If I'm right could you put just a simple yes :D
Re: New to Java, could you explain something to me?
You are correct. Congrats.
Re: New to Java, could you explain something to me?