Java #2 – Using an IDE and understanding Hello World

In this article we will be setting up an IDE to make programming in Java a lot easier and we will be breaking down the Hello, World! program we wrote last time.

1. Setting up Eclipse

As I said last time, in this tutorial series I will be using Eclipse. I recommend you do the same to avoid any confusion.

Once you’ve installed and run Eclipse, you should be met with a screen that looks like this :

The workspace is where all of your projects and files will be saved. You can just leave this as the default, but if you change it make sure you remember where you are saving your projects, as we will need to access it later.
You can also select the Use this as the default and do not ask again box to avoid having this window pop up every time you launch Eclipse.

Once you’re done selecting where you want your workspace to be, you can click launch.

Once Eclipse launches, you will be met with a screen that looks like this :

Close the Welcome tab by clicking on the x as if you were closing a tab in your web browser. Your screen should now look like this :

I’ll explain what each section is one by one.

  1. The package explorer : This is the leftmost section, and it is basically a file explorer. This is where you will create new projects or files, and it is where you can open files from.
  2. The terminal or console : This is the section at the bottom. You will notice that there is currently no console or terminal there, but that is where it will be when you launch a program.
  3. The task list and outline : These are the rightmost sections. We won’t be using these, so you can just close them.
  4. The editor : This is the central section. There is currently no file open, so it is blank, but this is where you will be writing code.

You can turn on dark mode, which, trust me, is way better than leaving light mode on. Go to Window > Preferences > General > Appearance > Theme and set it to Dark. Then hit apply and close.

Once you have Eclipse set up, we can start using it.

2. Recreating Hello, World! in Eclipse

Recreating Hello, World! in an IDE is a lot easier than making it without one.

To start off, click on Create a Java project in the package explorer. Set the name to Hello, World! and click finish. Expand the created folder in the package explorer by clicking on the arrow next to the project folder.

Next, right-click on the src folder and select New > Class. Everything in Java is contained in a class, so this is what you will be using to create new files. Set the name to HelloWorld and select the public static void main(String[] args) box in the Which method stubs would you like to create? section. Then click finish.

You will notice that this already looks a lot like what we created last time. In fact, to recreate exactly what we had last time all we need to do is replace // TODO Auto-generated method stub with System.out.println("Hello, World!");.

Eclipse actually gives us a couple of handy tools to help type out your code faster, namely autocomplete. If you type System., you will see a bunch of options pop up. These are all of the things you can find in System. The same thing will happen when you type System.out.. Once again, these are all of the things you can use System.out for.

You can trigger autocomplete manually by pressing ctrl and space at the same time. For instance, if I were to type Sys and then press ctrl and space, System would be suggested.

In fact, we can do even better than that. Since there are some lines of code that you will be using a lot, Eclipse gives us shortcuts to them. So in this case, if you were to type sysout and then press ctrl and space at the same time, Eclipse will automatically write System.out.println(); for you. You then just need type what you want in the parenthesis, in our case "Hello, World!".

Running your project is a lot easier too. To run a project, just click in the menu bar. You should see the console pop up and, in it, Hello, World! should appear.

If you get any errors, make sure the code you have is the same as last time. If you still get any errors, ask on the discord for help.

3. Understanding Hello, World!

It’s all good to have a program running, but to learn you need to understand how it works. I’ll break the program down into each of its parts.

public class HelloWorld {

}

This is the class that contains our code. Everything in Java is part of a class, so you will always find this in a Java file.

public static void main(String[] args) {

}

This is the main method. All the java code you write will be inside a method, and each method is inside a class. The main method is special though, as it is the one which is run when the program starts. I’ll break it down a bit more :

public static void

These are the modifers that this method has. They each mean something specific :

  • public: This means that this method is accessible from everywhere in your code, it is publically available.
  • static: This one is a bit more complicated, so I’ll explain it when we learn about instances.
  • void: This is the return type of the method. The return type of a method is what kind of data it sends back to the code that executed the method. In this case, void means it returns nothing.
main

This is the name of the method.

(String[] args)

These are the arguments that this method has. The arguments are just bits of data that are given to a method, in this case an array of String.

{

}

Anything between these curly braces is the code that you want to run when this method is executed. It is the body of the method.

System.out.println();

This is the line of code that is run when the method is executed. I’ll also break this one down.

System

This is a class. If you remember, classes have methods inside of them. However, they can also have another type of data : fields. A field is just a piece of data which you can access. We’ll learn more about these later.

out

This is the out field in the System class. It allows you to interact with the console.

println()

This on the other hand, is a method in the PrintStream class. Now, you might be thinking, “We never mentioned anything called PrintStream!”. And you would be… kind of right. You see, the field out of the System class is actually a PrintStream in disguise.
You can think of it like this : If I say “My best friend is Bob”, then you can assume that Bob knows how to breathe, so he could have a method breathe(), like any other human. But did I ever say the word “Human”? I didn’t. You know Bob by his name, but you know he is a human. It’s the same here. You know out by it’s name, but its actually a PrintStream.

"Hello, World!"

This is the argument that you give to the println() method. In this case, the quotes (" ") tell java that you are creating a String. A String is just text, and when you give it to println(), it just outputs that text to the console.

;

You may think I’m joking, but I’m not. The semicolon is very important as it tells Java when one instruction is done and it has to move on to the next. Without it, the code simply wouldn’t run.

And that’s it for this time!

Previous tutorial

Next tutorial