Lesson 1: Hello, Java! (And No, It’s Not Just Coffee)
Welcome to your first step into the world of Java! No, we’re not brewing actual coffee beans, though you might want to have some nearby for those inevitable late-night coding sessions. Today, we’re diving into Java – the programming language that powers everything from your favorite apps to those mysterious enterprise systems nobody quite understands.
What is Java, Anyway?
Java is the programming language that’s been around longer than most memes and some of your coworkers. It’s powerful, versatile, and has more frameworks than you have coffee mugs. Best of all, it’s object-oriented, meaning you’ll get to think in terms of objects and classes instead of just endless lines of code. Think of it like building a city out of LEGO blocks, only fewer sharp corners to step on.
And hey, if you’ve used Python or JavaScript before, you’ll notice Java has a more structured approach. Think of it like Python’s laid-back hoodie compared to Java’s suit and tie – both stylish, but one makes you feel extra sharp.
The “Hello, World!” Classic
Let’s get started with the programming world’s equivalent of a warm handshake – the legendary “Hello, World!” program. It’s the universal way to say, “Hey, I’m here and I’m ready to code!” Plus, once you see those magical words on your screen, you’ll know you’ve officially joined the club.
Step 1: Install OpenJDK
Before we write any code, we need the right tools. Java isn’t just going to appear out of nowhere (though that would be magical), so let’s install the OpenJDK (Java Development Kit) – your all-in-one toolbox for writing and running Java code.
Here’s how to install OpenJDK on your Linux distribution:
On Ubuntu:
sudo apt update
sudo apt install openjdk-17-jdk
On Fedora:
sudo dnf install java-17-openjdk
On Arch Linux:
sudo pacman -S jdk17-openjdk
Once installed, you can verify the installation by running:
java -version
If Java responds with its version number, congratulations! You’ve successfully equipped yourself with a coder’s most essential tool. You’re practically a wizard now – or at least a Java apprentice.
Step 2: Setting Up Your Project with Spring Tool Suite (STS)
Now that you’ve got OpenJDK installed, it’s time to get down to business. And by business, I mean creating your first project using Spring Tool Suite (STS) – an IDE that’s like having a coding assistant, but without the need to pay or feed it.
How to Install Spring Tool Suite (STS)
- Download the latest version of STS from the Spring official website.
- Extract the downloaded archive:
tar -xvzf spring-tool-suite-*.tar.gz
- Navigate to the extracted folder and run STS:
./STS
Now that your IDE is ready, let’s move on to setting up your project. Get ready – things are about to get serious (but not too serious, we promise).
Step 3: Creating a New Java Project in STS
- Open STS and go to File > New > Java Project.
- Name your project
JavaPractice
and click Finish. (It’s a great name – you’re starting small, but soon you’ll be coding circles around everyone.) - STS will automatically create the basic structure for your project, which is like having a personal assistant organize all your stuff before you even know you need it.
Step 4: Creating a Package
Now, we need a package – not the kind Amazon delivers, but a way to neatly organize your code. Think of packages as folders for your Java classes, like filing cabinets in a digital library.
- Right-click on the src folder in the Project Explorer and choose New > Package.
- Name the package
com.example.helloworld
and click Finish. Now your code has a home, a cozy little folder where it can live its best life.
Step 5: Creating the HelloWorld Class
Let’s bring this project to life. Time to create the infamous HelloWorld
class!
- Right-click on the
com.example.helloworld
package in the Project Explorer and choose New > Class. - Name the class
HelloWorld
and check the box that says public static void main(String[] args). (The main method is like the engine of a car – without it, nothing moves.)
Step 6: Writing Your First Java Program
Inside the new HelloWorld.java
class, type this code:
package com.example.helloworld;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Boom! You’ve just written your first Java program. Take a moment to admire the beauty of that code. Feels good, doesn’t it?
Step 7: Running Your Program
Now it’s time to see your creation in action. Here’s what you do:
- Right-click on the
HelloWorld.java
file in the Project Explorer. - Choose Run As > Java Application.
If everything goes according to plan (and why wouldn’t it?), you’ll see “Hello, World!” printed in the console. You just told the computer to greet the world, and it listened! You’re practically a digital maestro.
Summary of the Folder Structure
Your project folder is starting to take shape. Here’s what it should look like now:
JavaPractice/
└── src/
└── com/
└── example/
└── helloworld/
└── HelloWorld.java
Look at that organization! It’s like having a Marie Kondo-ed project folder – everything is neat, tidy, and where it belongs.
What Did We Just Do?
You’ve just written and run your first Java program – and all without breaking a sweat. Here’s a quick breakdown:
package com.example.helloworld
: This is your code’s address – the package keeps everything organized like a digital filing system.public class HelloWorld
: This is your Java class, the star of the show.public static void main(String[] args)
: This method is where Java starts running the show. Without it, your code wouldn’t even get off the ground.System.out.println("Hello, World!");
: This is your code’s first message to the world. It’s like sending a postcard, but with fewer stamps and more semicolons.
Wrapping Up
Congrats! You’ve just written your first Java program, and the world didn’t implode. You’re on your way to becoming a Java master! Soon, you’ll be building applications that remind you to take breaks, stretch, and of course, grab another cup of coffee.
In the next lesson, we’ll dive into variables – because even Java likes to hoard information like a squirrel with nuts. Get ready to start storing data like a pro!
Ready to continue? Head over to Lesson 2 and let’s keep the coding magic alive!
See also
- Lesson 2 – Loops and Conditionals: Making Decisions and Repeating Yourself (Without Going Crazy)
- Unlock the Power of JavaScript – A Beginner’s Journey (No Cape Required)
- Lesson 2: Variables and Data Types – The Building Blocks of Your Java Adventure
- Ready to Master Java? (And No, We're Not Talking About Coffee Beans!)
- Tabs vs. Spaces: The Great Divide in Programming