00 - Printing
Printing to Screen
What is it?
Printing means sending text output to the console (terminal). In Java, you do this with System.out.println().
Why does it exist?
Every program needs a way to communicate results to the user. Printing is the most basic form of output - you’ll use it constantly for debugging and displaying results.
How does it work?
System.out.println("Hello World");
System→ a built-in Java class that gives access to system-level toolsout→ the output stream (i.e., the console)println→ the method that prints text and then moves to a new line"Hello World"→ the parameter (the value you’re passing into the method)
println vs print
| Method | Behaviour |
|---|---|
System.out.println("hi") |
Prints “hi” and moves to a new line |
System.out.print("hi") |
Prints “hi” and stays on the same line |
System.out.print("Hello ");
System.out.print("World");
// Output: Hello World (on ONE line)
System.out.println("Hello");
System.out.println("World");
// Output:
// Hello
// World
The Boilerplate (Minimum Java Program)
Every single Java program needs this shell to run. Without it, Java won’t even start executing:
public class Example {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Breaking it down line by line:
| Part | Meaning |
|---|---|
public class Example |
Declares a class named Example. The filename must match: Example.java |
public static void main(String[] args) |
The entry point - Java starts executing from here |
{ ... } |
Curly braces define the boundaries of blocks |
System.out.println(...) |
The actual code you want to run |
Important The class name in the code must exactly match the filename.
Example→Example.java. Case-sensitive.
Note
public,static,void,classare keywords - reserved words with special meaning in Java. You’ll understand each one as you progress. For now, just use the boilerplate as-is.
How Commands Execute
Commands run one line at a time, top to bottom. Java reads your code like you read a book - left to right, line by line.
System.out.println("First");
System.out.println("Second");
System.out.println("Third");
// Output:
// First
// Second
// Third
Gotchas
- Forgetting the semicolon
;at the end of each statement → compilation error - Mismatching class name and filename → Java won’t compile
Systemwith a capitalS- it’s case-sensitive;systemwon’t work- String content must be in double quotes
"...", not single quotes'...'