System.out Methods
Part of the Fundamentals section of Coddy's Java journey — lesson 33 of 73.
In Java, the System.out object provides various methods for printing output to the console. Here are some of the most commonly used System.out methods:
print(String s): Prints a string to the console. It does not add a newline character at the end, so subsequent output will continue on the same line.
println(String s): Prints a string to the console followed by a newline character. This means the next output will start on a new line.
Here's how you can use these methods:
String name = "Alice";
int age = 30;
System.out.print("Name: ");
System.out.print(name);
System.out.println(" is " + age + " years old.");
System.out.println("Hello, " + name + "!");This code will produce the following output:
Name: Alice is 30 years old.
Hello, Alice!Challenge
BeginnerWrite a Java program that uses System.out methods to output the following:
- Print
"Hello, "and"Coddy!"on the same line. - Print
"Java is fun!"on a new line usingprintln.
Cheat sheet
Java provides System.out methods for console output:
print(): Prints without adding a newline (output continues on same line)println(): Prints and adds a newline (next output starts on new line)
System.out.print("Hello, ");
System.out.print("World");
System.out.println(" - same line");
System.out.println("New line");Output:
Hello, World - same line
New lineTry it yourself
public class Main {
public static void main(String[] args) {
System.out.print("Hello, ");
// Write your code below
}
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorIncrement/DecrementPost Increment/DecrementArithmetic ShortcutsComparison OperatorsString Comparison5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 43Variables Part 2
ConstantsNaming ConventionsRecap - Initialize VariablesType Casting Part 1Type Casting Part 26Decision Making
If StatementIf - ElseSwitch StatementTernary OperatorRecap - If ElseNested If - Else