Printing
We have already seen how to print text, but this section will review printing and go into more detail about what you can do.
Print statements
There are two commands for printing text:
System.out.println("Hello");
System.out.print("Hello");The first command prints “Hello” to the screen, and then moves down to the next line. This way, if you print something else it will appear on the line below. The second command prints “Hello”, but stays on the same line. If we printed something else, it would appear on the same line as “Hello”.
Of course, we can substitute any string we like for “Hello”. The string we want to print must be in "" quotes, but the quotes do not actually get printed.
Printing variables
In addition to printing strings enclosed in “”, we can also print the values of variables. To do
this, we just put the variable name inside the System.out.println or System.out.print parentheses. When that line is executed, the VALUE of the variables
gets printed to the screen.
Here are some examples:
int num = 4;
char c = ‘A';
double val = 7;
boolean flag = true;
String s = "Cat";
//prints 4
System.out.print(num);
//prints A on the same line as 4
System.out.println(c);Concatenating
We can output text that has both variable values and regular strings with a single
System.out.println statement. This can be accomplished by using the concatenation (+)
operator.
Here’s an example:
String name = "Fred";
int age = 20;
System.out.println(name + " is " + age + " years old");This prints out: “Fred is 20 years old”. Notice that if you want to print the value of a
variable, just list the variable (not in quotes). If you want to print normal text, put the text in
quotes. To combine variables and text, separate them by a +. This plus concatenates the
variables and text by pushing them together to form one big string.
Formatted printing
As an alternative to the concatenation approach, we can also use the System.out.printf command to print a combination of text and variable values. This command also allows us to format our output by adding spacing or rounding doubles to some number of decimal places.
Format specifiers
Printing variables works a bit differently with the System.out.printf command. In this approach, weneed to use a format specifier to specify the kind of variable that’s going to be printed.
Here are the different format specifiers:
| Type | Format Specifier |
|---|---|
| int | %d |
| double | %f |
| char | %c |
| boolean | %b |
| String | %s |
| newline character | %n |
It’s easiest to see an example to figure out how formatted printing works. Here’s how to print the value of an integer to the terminal:
int num = 4;
System.out.printf("The value of num is %d%n", num);Notice that where we want to print a variable, we put the format specifier (%d for int). After we’ve listed the entire text we want to print, we put the corresponding variable names in a comma-separated list.
The above example will print:
"The value of num is 4" to the terminal. It will also end with a newline character, so that the next thing we printed would appear on the line below.
We can also print several variables at once:
char letter = 'A';
int val = 27;
System.out.printf("The letter is %c and the number is %d%n", letter, val);This prints:
"The letter is A and the number is 27"to the screen. Notice that the %c corresponds to the letter variable, and the %d corresponds to the val variable.
Formatting output with System.out.printf
The System.out.printf command also allows you some control over formatting your output. For example, if you want a value to take up exactly 6 spaces (padded with space characters on the left, if necessary), put a 6 between the % and the format specifier.
For example:
int num = 4;
System.out.printf("The value of num is %6d%n", num);This will print:
"The value of num is 4" to the terminal (note the padding on the left of the 4).
You can also only display a certain number of digits for doubles. For example, put a .2 in between the % and the format specifier character to only display two decimal places.
For example:
double val = 3.14159;
System.out.printf("Pi is %.2f%n", val);This will display:
"Pi is 3.14" You can specify both the width of the output (for example, six spaces) and the number of decimals to display by doing something like this:
double val = 3.14159;
System.out.printf("Pi is %6.2f%n", val);