Storing Information in a Variable
In the programs you write, you need a place to store information for a brief period of time. You can do this by using a variable, a storage place that can hold information such as integers, floating-point numbers, true-false values, characters, and lines of text. The information stored in a variable can change, which is how it gets the name variable.
In Splash.java file, replace Line 5 with the following:
String greeting =
“Blue warrior shot the food!”;
This statement tells the computer to store the text Blue warrior shot the food! in a variable called greeting.
In a Java program, you must tell the computer what type of information a variable will hold. In this program, greeting is a string—a line of text that can include letters, numbers, punctuation, and other characters. Putting String in the statement sets up the variable to hold string values.
When you enter this statement into the program, a semicolon must be included at the end of the line. Semicolons end each statement in a Java program. They’re like the period at the end of a sentence. The computer uses them to determine when one statement ends and the next one begins.
Putting only one statement on each line makes a program more understandable (for us humans).
Displaying the Contents of a Variable
If you run the program at this point, it still seems like nothing happens. The command to store text in the greeting variable occurs behind the scenes. To make the computer show that it is doing something, you can display the contents of that variable.
Insert another blank line in the Splash program after the String greeting = “Blue warrior shot the food!” statement. Use that empty space to enter the following statement:
This statement tells the computer to display the value stored in the greeting variable. The System.out.println statement makes the computer display information on the system output device—your monitor.
Now you’re getting somewhere.