|
Up to this point, we've been creating what could be called static programs, programs that don't really do much except pump out what we've put in. When we type in something like:
println("Hello World!");
the part in quotes is a static string called a literal. We could just as easily have printed out numbers such as:
println(145);
println(3.147);
These numbers, the first an integer and the second a decimal number, would be printed as is, as simple, static numbers. They are also called literals. (Note that numbers do not have to have quotation marks around them. In fact, if they do they will be interpretted as a string.)
In order for a program to be really useful, it needs to calculate stuff and pump out the results of those calculations. Whether it's a graphics applet with a bouncing ball or a mortgage calculator attached to a spreadsheet application, there's calculations to be done. And where there are calculations, there are variables.
What is a variable? Think back to high school algebra. Remember a=b+c? Well, a, b and c are variables. They are basically string representations of values. In computer programming, you can use variables to represent integers and decimal numbers, but you can also use them to represent strings and other objects. Each of these different types of data that you can represent with a variable is called a data type.
Because there are so many different data types that can be represented by variables, you must tell the computer what type of variable you are using. This is called declaring your variable, and you do that in a declaration statement. The computer then sets aside the appropriate amount of storage space in memory for that data type.
Let's take a closer look.
|