In the last exercise, you learned how to pause the program and allow the human to type in something. But what happened to what was typed? When you typed in the answer “Paris” for the first question, where did that answer go? Well, it was thrown away right after it was typed because we didn’t put any instructions to tell the Scanner object where to store it. So that is the topic of today’s lesson.
Just like the last exercise, when you first run this your program will only display the first question and then pause, waiting for a response:
Notice that the first printing statement on line 11 is
print() rather than
println(). Because of this, the question is printed on the screen, but
the cursor is left alone after printing. Thus the cursor is left blinking
at the end of the line the question is on.
If you had used
println(), the cursor would have been moved to the
next line before the Scanner’s
next() method had a chance to run,
and so the cursor would be blinking on the beginning of the line after
the question instead.
At the top of the program we declared four variables: one String variable called name, one integer variable called age, and two floating-point variables named weight and income.
On line 12 we see the
keyboard.next() that we know from the previous
exercise will pause the program and let the human type in something it will
package up in a String. So now where does the String they type go? In
this case, we are storing that value into the String variable named “name”.
The String value gets stored into a String variable. Nice.
So, assuming you type
Brick for your name, the String value
stored into the variable name on line 12. This means that on line 14, we can
display that value on the screen! That’s pretty cool, if you ask me.
On line 15 we ask the Scanner object to let the human type in something which it will try to format as an integer, and then that value will be stored into the integer variable named age. We display that value on the screen on line 17.
Line 19 reads in a double value and stores it into weight, and line 23 reads in another double value and stores it into income.
This is a really powerful thing. With some variables and with the help of the Scanner object, we can now let the human type in information, and we can remember it in a variable to use later in the program!
Before I wrap up, notice for example that the variable income is declared all the way up on line 7 (we choose its name and type), but it is undefined (it doesn’t have a value) until line 23. On line 23 income is finally initialized (given its first value of the program). If you had attempted to print the value of income on the screen prior to line 23, the program would not have compiled.
Anyway, play with typing in different answers to the questions and see if you can get the program to blow up after each question.
Java is a statically-typed programming language. That means that the
type of a variable (
String, etc) is static: it can’t
be changed. Almost all older programming languages are statically-typed,
and most languages with static typing also have what is called “manifest
typing”; these languages make you declare what types your variables are
going to be before you can use them. Java, C, and C# are all languages
with manifest typing.
Once you’ve told the Java compiler that, say, age is going to hold an integer, it makes sure that you don’t change your mind later and try to put a String in there.
This might seem a bit annoying, but when you start writing much longer programs, the type checker can help prevent certain kinds of errors. And that’s nice. But don’t feel too sorry for yourself! One of the oldest programming languages ever (Plankalkül) wasn’t so nice. You had to write the type of a variable every time you used it. This would be very annoying. I have written some code to show what Java might be like if you had to add the type of a variable every time you used it. Don’t bother typing it up; it won’t compile since Java (thankfully) doesn’t work like that.
Whew! Aren’t you glad you don’t have to do that every time you use a variable?!?
On the other hand, you are allowed to feel a little bit sorry for yourself that you even have to declare the types of variables at all. Many modern programming languages feature “type inference”, where the compiler can infer the type of a variable by how you use it in the code. Python and Swift are some programming languages that can infer types. Java does not have type inference, but I have written up some code that shows what this exercise might look like if it did. Again, don’t bother typing this one up; it won’t compile.
For example, on line 6 a compiler for such a language would be able to infer
that name is a variable and that it must for holding
Strings since that’s
what you’re putting into it.
Not that any of that matters, because you’re learning Java right now and not any of those other fine programming languages. So you’re stuck with static typing and manifest typing and you’ll get used to it soon enough. Is it too much to ask to just specify the type of a variable before you start putting values in there? I think you’ll find it isn’t too bad.
Maybe you should scroll back up to the top to read the code again for this exercise before attempting the Study Drills, if you didn’t do them already.
“Learn Java the Hard Way” is ©2013–2016 Graham Mitchell