Alright, enough talking. Let us get Java running on your machine and write some code. Trust me, the first time you compile and run a Java program, it feels good.
Installing the JDK
To write Java, you need the JDK โ Java Development Kit. This includes the compiler
(javac), the runtime (java), and a bunch of useful tools.
Here is what you need to do:
- Go to Oracle's website or Adoptium (I personally recommend Adoptium โ it is open source and works great)
- Download the installer for your operating system
- Run the installer and follow the instructions
- Open a terminal or command prompt and type
java -version
If you see something like java version "21.0.1", you are good to go.
If you get an error, make sure Java is added to your PATH โ the installer usually
does this automatically.
Your First Java Program
Create a new file called Main.java. Remember, the filename must match
the class name. Type the following:
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Now open your terminal, navigate to the folder where you saved Main.java,
and run these two commands:
javac Main.java
java Main
The first command compiles your code and creates Main.class (the bytecode).
The second command runs that bytecode on the JVM. If everything worked, you should see:
Hello, World!
I still get a little thrill every time I see that output. Silly, I know. But there is something magical about telling a computer what to do and watching it obey.
Understanding the Code
Let me quickly explain what each piece does before we move on:
public class Mainโ every Java program lives inside a class.publicmeans anyone can access it.public static void main(String[] args)โ this is the entry point. When you run your program, Java looks for this method and starts there.System.out.println()โ this prints text to the console. Thelnpart adds a new line after the text.
Do not worry about memorizing everything. You will see this pattern so many times that it will become second nature.