DEV Community

Cover image for Java Newbie to Pro? Day 3 – Who is Who and What They Do in my First Java Program
Nabil Mahmud
Nabil Mahmud

Posted on

Java Newbie to Pro? Day 3 – Who is Who and What They Do in my First Java Program

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!");
   }
}
Enter fullscreen mode Exit fullscreen mode

🏗️ The Blueprint aka Class

public class MySweetProgram
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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!");
Enter fullscreen mode Exit fullscreen mode

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!


Image description

Top comments (0)