Console Output
System.out
System.out is the object that controls console output in Java.
It contains several methods that pertain to output. We will be covering
print(), println() and printf()
today.
print() — Takes one argument, which is then printed to
the console. This argument will usually be a string, but
print() will work with almost any primitive type (i.e. int,
double, char, etc.).
println() — The same as print(), but
automatically places the cursor on the next line.
printf() — Uses a custom syntax to provide advanced
formatting.
Outputting Data
The easiest way to use print() is to output a simple statement to the screen.
Example:
public static void main(String[] args)
{
System.out.print("Hello world");
}
print() can also be used to output the contents of variables.
Example:
public static void main(String[] args)
{
String outputText = "Hello world";
System.out.print(outputText);
}
Finally, print() can also combine any number of variables and
constants together and print them out as one. Note that the "\n" character
represents a newline.
Example:
public static void main(String[] args)
{
String myName = "Travis";
int myAge = 22;
double myHeight = 6.0;
System.out.println("Name: " + myName);
System.out.print("Age: " + myAge + "\n");
System.out.print("Height: ");
System.out.println(myHeight);
System.out.println("Name: " + myName + " Age: " + myAge + " Height: " + myHeight);
}
Output:
Name: Travis Age: 22 Height: 6.0 Name: Travis Age: 22 Height: 6.0
This example works because Java will automatically convert numbers and some other native (primitive) types to strings when you use the '+' operator to add them to a string. The '+' operator in Java is the concatenation operator when used on strings.
So, what happens if we say:
public static void main(String[] args)
{
int a = 4;
int b = 3;
System.out.println("Name: " + a + b);
}
And what if we slide a + b to the left hand side:
System.out.println(a + b + "Name: ");
print() vs println()
println() is useful because it automatically inserts a
line break at the end of your output, but sometimes print()
is a better choice. Times when you might want to use print()
over println() are:
- Prompting for user input
- Combining several print statements that should all be on one line
- Splitting large print statements up to make code more readable
printf()
Since version 1.5, Java includes a printf() function that
behaves similarly to the C equivalent.
Here's an example of formatted output using printf:
public static void main(String[] args)
{
int i = 7;
float f = 962.5274f;
double d = 12345.6789;
System.out.printf("There are %d dogs. \n", i);
System.out.printf("They can smell %2.1f times better than we can.\n", f);
System.out.printf("Actually, %10.1f is a made-up number. So is %f\n", f, d);
System.out.printf("Maybe in %e years, we will be able to smell as well as dogs.", d);
}
Which should output:
There are 7 dogs. They can smell 962.5 times better than we can. Actually, 962.5 is a made-up number. So is 12345.678900 Maybe in 1.234568e+04 years, we will be able to smell as well as dogs.
These are just a few of the many formatting flags that exist, but the ones shown here include:
%dfor decimal (as in base 10) numbers-
%ffor floating point numbers. You can further format a floating point number by specifying a minimum character length and number of digits after the decimal point. For example, %10.1f would print a number with a minimum of 10 characters and only 1 digit after the decimal point. -
%efor scientific notation.
More information on printf and formatted printing can be found here.