In Day 2, I created my first program and ran it to print the phrase "Hello there!" to the console. Today, I’ll be sharing my understanding of that first program—breaking down who is who and what they do in our Java code.
👇 This was my first Java program:
public class MySweetProgram {
public static void main(String[] args) {
System.out.println("Hello there!");
}
}
🏗️ The Blueprint aka Class
public class MySweetProgram
Every program is wrapped inside a class. Here, MySweetProgram
is our class. It has an opening and closing curly brace, and it acts as a container for our code.
📌 Remember: The class name must match the filename. That’s why our file is named MySweetProgram.java.
❤️ The Heart of the Program
public static void main(String[] args)
The heart (or should I say 心 Xīn?) of the program! Gotta sneak in some Chinese practice while I’m at it. 😆
This is the entry point of every Java program. When you run the code, Java starts executing from the main
method. Let’s break it down:
-
public
– This means the method can be accessed from anywhere. -
static
– It belongs to the class itself, so we don’t need to create an object to run it. -
void
– This means the method doesn’t return any value. -
main
– The special method name that Java looks for when running the program. -
String[] args
– This is used to accept command-line arguments (not something we need right now, but useful for later!).
🖨️ Printing to the Console
System.out.println("Hello there!");
This line prints "Hello there!" to the console. It’s a simple statement that outputs the text passed to the println()
method. However, we need not worry about the System.out
part at this moment.
That’s it for today!
Top comments (0)