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:
String name = "Karel"; System.out.println("Welcome to CodeHS"); // prints the text in quotes System.out.println(name); // prints what's stored in name
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.
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:
public class Welcome { public static void main(String[] args) { String name = "Karel"; System.out.println("Welcome to CodeHS"); System.out.println(name); } }
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:
public class Welcome
The class is the container for your whole file. All of your code lives inside it, and Java runs it in the Java Virtual Machine.
public static void main(String[] args)
The main method. Every Java program starts here - Java always looks for main as the place to begin. This header is always written exactly like this.
{ your code here }
The code you want to run goes between the curly braces of main. Java runs it top to bottom.
Shortcut for your brain: "every Java program starts at main." Type the boilerplate, then write your real code inside it.
Practice: introduce yourself
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.
public class MyProgram { public static void main(String[] args) { // Print your name and where you live here: } }
public class MyProgram { public static void main(String[] args) { System.out.println("My name is Karel."); System.out.println("I live in San Francisco."); } }
My name is Karel. I live in San Francisco.
println vs print
The System class actually gives you two ways to print: println and print. The big difference is what happens after the message:
- println prints the message, then moves to a new line (that's the "ln"). The next output starts on a fresh line.
- print prints the message and stays put. The next output continues on the same line.
Watch what happens with two of each. The ⏎ marker shows where a newline lands:
Two println statements
Two print statements
When println is called twice, the output lands on two separate lines. With print, everything squishes onto the same line - Java never gets told to move down.
What about mixing them? If println comes first, the next statement starts on a new line. But if print comes first, the next statement stays glued to the same line:
public class MyProgram { public static void main(String[] args) { // println forces the next output onto a new line System.out.println("Hello world!"); System.out.print("Hello world!"); // print lets the next output stay on the same line System.out.print("Hello world!"); System.out.println("Hello world!"); } }
Hello world! Hello world!Hello world!Hello world!
Easy way to remember it: println affects the line that comes after it, not the ones before it. It prints, then hits Enter.
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?
Practice: spell your name
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.
public class MyProgram { public static void main(String[] args) { System.out.print("Karel "); System.out.print("The "); System.out.println("Dog"); } }
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.
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:
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)); } }
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:
System.out.println("Age: " + 5 + 1); // glue, then glue again System.out.println("Age: " + (5 + 1)); // parentheses add first
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.
Build a banner
Newspapers need big headlines. Type up to three words, choose a layout, and the widget writes the exact print statements you'd need - plus what the console will show. Try both layouts and compare the code!
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.