10.4: Printing Variables
- Page ID
- 15217
You can display the value of a variable using print
or println
. The following statements declare a variable named firstLine
, assign it the value "Hello, again!"
, and display that value.
String firstLine = "Hello, again!"; System.out.println(firstLine);
When we talk about displaying a variable, we generally mean the value of the variable. To display the name of a variable, you have to put it in quotes.
System.out.print("The value of firstLine is "); System.out.println(firstLine);
For this example, the output is:
The value of firstLine is Hello, again!
Conveniently, the syntax for displaying a variable is the same regardless of its type. For example:
int hour = 11; int minute = 59; System.out.print("The current time is "); System.out.print(hour); System.out.print(":"); System.out.print(minute); System.out.println(".");
The output of this program is:
The current time is 11:59.
To output multiple values on the same line, it’s common to use several print
statements followed by println
at the end. But don’t forget the println
! On many computers, the output from print
is stored without being displayed until println
is run; then the entire line is displayed at once. If you omit the println
, the program might display the stored output at unexpected times or even terminate without displaying anything.