Please enable JavaScript to use CodeHS

Java Tutorial

Printing in Java

Programs are only useful if people can see what they do. Learn how System.out.println sends messages to the console, and how one little "ln" decides whether your output gets a fresh new line.

java ~15 min Beginner friendly
The Big Idea

Meet System.out.println

When you create a program, you usually want to show information to the person using it - a score, a greeting, a result. In Java, the easiest way to display information is with System.out.println.

System.out.println can display all kinds of values - Strings, numbers, even variables - in the console. You just put the thing you want to display inside the parentheses:

Welcome.java
String name = "Karel";
System.out.println("Welcome to CodeHS");   // prints the text in quotes
System.out.println(name);                  // prints what's stored in name
Console
Welcome to CodeHS
Karel

Quotes matter! println("name") prints the word name. println(name) - no quotes - prints whatever value is stored in the variable called name. Here that's Karel.

Boilerplate

The main method: where Java starts

To actually run that code in a Java file, you need a few extra lines wrapped around it. Every Java program you write for a long time will start out looking like this:

Welcome.java
public class Welcome
{
    public static void main(String[] args)
    {
        String name = "Karel";
        System.out.println("Welcome to CodeHS");
        System.out.println(name);
    }
}
Console
Welcome to CodeHS
Karel

Don't worry about memorizing what every word means yet. For now, here's the friendly version of what each part does:

Shortcut for your brain: "every Java program starts at main." Type the boilerplate, then write your real code inside it.

Your Turn

Practice: introduce yourself

1

Print your name and where you live

Try printing out your name and where you live! Start from the boilerplate below and add your System.out.println statements inside main.

MyProgram.java
public class MyProgram
{
    public static void main(String[] args)
    {
        // Print your name and where you live here:

    }
}
MyProgram.java
public class MyProgram
{
    public static void main(String[] args)
    {
        System.out.println("My name is Karel.");
        System.out.println("I live in San Francisco.");
    }
}
Console
My name is Karel.
I live in San Francisco.

Interactive

The println vs print lab

Time to get your hands inky. Each row below is one print statement. Click the println / print button on any row to flip it, and watch the console re-print instantly. Can you make it say HaHaHa on one line? Ha on three lines? Two on one line and one alone?

Your Turn

Practice: spell your name

2

Print your full name

Print your full name to the console using at least 2 print statements and 1 println statement - and make the whole name land on one single line.

MyProgram.java
public class MyProgram
{
    public static void main(String[] args)
    {
        System.out.print("Karel ");
        System.out.print("The ");
        System.out.println("Dog");
    }
}
Console
Karel The Dog

Notice the spaces inside the quotes: "Karel " and "The ". print doesn't add any space for you - without them you'd get KarelTheDog.


Level Up

Gluing text together with +

Printing one thing at a time is fine, but usually you want to mix words and values in a single message. In a print statement, the + sign concatenates - a fancy word for "glues together" - Strings and variables into one String:

Concat.java
public class Concat
{
    public static void main(String[] args)
    {
        String name = "Karel";
        int age = 5;

        System.out.println("My dog is named " + name);
        System.out.println(name + " is " + age + " years old");
        System.out.println("Next year: " + (age + 1));
    }
}
Console
My dog is named Karel
Karel is 5 years old
Next year: 6

One thing to watch: when a String is involved, + means "glue", not "add". Java works left to right, so numbers that come after a String get glued on as text:

Gotcha.java
System.out.println("Age: " + 5 + 1);    // glue, then glue again
System.out.println("Age: " + (5 + 1));  // parentheses add first
Console
Age: 51
Age: 6

Age: 51?! Java saw "Age: " + 5 first and made the String "Age: 5", then glued 1 onto the end. If you want math inside a print statement, wrap it in parentheses.


Check Yourself

Quick quiz

1. What does this code print?

System.out.print("Ha");
System.out.print("Ha");
System.out.println("Ha");

2. Where does Java always start running a program?

3. What does System.out.println("Score: " + 10 + 5); print?

Keep learning Java

You can print anything to the console now - next, store values in variables to print smarter messages, or flip things around and read input typed by the user.