Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

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 the Saluton.java file, replace Line 5 with the following:

String greeting = "Saluton mondo!";

This statement tells the computer to store the text “Saluton mondo!” 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 Saluton program after the String greeting = "Saluton mondo!" 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.

  • + Share This
  • 🔖 Save To Your Account