Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today, we’re focusing on how to output data in Java using System.out. Can anyone tell me what System.out is?
Isn't it a way to print messages to the console?
Exactly! System.out allows us to display messages on the console. It’s crucial for interacting with users. Now, what do you think happens if I use print instead of println?
Doesn’t print just keep everything on the same line?
Right! Whereas println moves to the next line after printing. So, if I use both in a sequence, they affect how my output is formatted. Let’s look at an example.
Can you show us a code example?
"Sure! Here we go:
Let’s dive deeper into print and println. How would you differentiate between the two methods?
I think print stays on the same line, but println goes to a new line after printing.
Good observation! That’s key to using them effectively. Can someone give a concrete example of when to use one over the other?
If I want to list items, I could use println for each item to have each on a new line.
Exactly! And if you wanted to format a single line output with variables? For instance, using both methods?
I could use print for most of the statement and println to finalize it!
Spot on! You’re really getting the hang of this.
Using print:
System.out.print("Hello");
System.out.print(" World"); // Outputs: Hello WorldUsing println:
System.out.println("Hello");
System.out.println("World"); // Outputs:
Hello
WorldUnderstanding how to use these methods is fundamental for displaying information in Java applications, as they form the basis of user interaction through the console.
Print shows with no break, but println knows how to make space.
Imagine a conversational robot: if it speaks with print, it never pauses; but if it uses println, every response is a fresh start!
P for print, no newline; L for println, line divine.
P-L: Print stays in line, Print-Ln goes for space!
Term
What does System.out.print do?
Click to flip
Term
What does System.out.println do?
Click to flip
This section explains how to use System.out to display output in Java, detailing the methods for printing with and without newlines.
In this section, we learn about the System.out object in Java, focusing on its print and println methods for outputting data. The difference between these two methods is highlighted, emphasizing how println adds a newline after the output, while print does not.
In Java, outputting data to the console is commonly done using the System.out object. This section explores the two main methods provided by System.out: print and println.
System.out.print(String str): This method prints the string passed to it without adding a newline at the end. This means that subsequent output will continue on the same line.
System.out.println(String str): In contrast, this method prints the string followed by a newline. This allows any following output to start on a new line, making it easier to read multiple lines of output.
Using print:
System.out.print("Hello");
System.out.print(" World"); // Outputs: Hello WorldUsing println:
System.out.println("Hello");
System.out.println("World"); // Outputs:
Hello
WorldUnderstanding how to use these methods is fundamental for displaying information in Java applications, as they form the basis of user interaction through the console.