Learn

Let’s start by breaking down what each element of the statements System.out.print(), System.out.println(), and System.out.printf()represent.

  • System is a class that has already been built into Java for your use.
  • out is an instance, or method, that has been provided in the System class. It is of the type PrintStream, which is what allows us to show output in Java programs.

Now let’s take a look at the functions of the out instance through the examples provided below.

System.out.print() prints into the same line when a program is running. The example below shows us how two System.out.print() statements called in the main method, print in the same line.

System.out.print("Hello "); System.out.print("World!");

This code would show the following output in your program:

Hello World!

You may also choose to concatenate the string you wish to print with other strings or variables. To concatenate simply means to join multiple strings together, and in Java, we use the ‘+’ symbol to achieve this:

String world = "World"; int day = 1; System.out.print("Hello " + world + "! Today is Day: " + day + " of your Intermediate Java course!");

This code would show the following output in your program:

Hello World! Today is Day: 1 of your Intermediate Java course!

System.out.println() prints output to a new line:

System.out.println("Hello"); System.out.println("World!");

This code would show the following output in your program:

Hello World!

Concatenation using System.out.println() works similar to System.out.print(), using the ‘+’ symbol to combine strings.

System.out.printf() allows us to output strings that are formatted in the code using format specifiers. Format specifiers begin with the ‘%’ sign, followed by the type of variable we want to print. Some examples include %s for a string,%c for a character, and %d for an integer.

Let’s look at a modified version of the Java program that uses System.out.printf(). Notice that variables in our print statement are stated in the order that the format specifiers appear.

String world = "World"; int day = 1; System.out.printf("Hello %s! Today is Day: %d of your Intermediate Java course!", world, day);

This code would show the following output in your program:

Hello World! Today is Day: 1 of your Intermediate Java course!

Instructions

1.

Before we can print our variables, we need variables to print! In the code editor create the following variables:

  • a String name that’s set to your name
  • a String hobbies that contains a list of your hobbies
  • an int age that contains your age

Then import the Java IO class.

2.

Use System.out.println() to print your name in the following format:

My name is ___
3.

Use System.out.printf() to print your age in the following format:

"I am __ years old"
4.

Use System.out.print() to print your hobbies using the following format:

My hobbies are ______.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?