Now that you’re all setup, let’s begin.In this lesson, we’re going to take it slowly and just set up our program that we’re going to use for this tutorial series and learn how to write a basic Java program that outputs a greeting just so you can get used to using Eclipse and the language itself.
Open up Eclipse and create a new Java Project (File > New > Java Project). I’m going to use one project for all the files used in this series just to make things easier. Give your name a project and leave the defaults as they are. We don’t really need to customize anything here so we should be fine. Smash the Finish button (not literally) and your project should be ready to go!
Next up let’s create a class to write our code in. Right-click your ‘src‘ folder and select New > Class. Name this class ‘helloworld‘ and click finish.
Now that you have a class, type the following code in the class:
Done that? Great! You just wrote your first Java program. But what’s going on? Let’s examine it line by line
public class helloworld – This is the name of our class. It’s a public class (We’ll discuss scope later on in the series) and it’s named helloworld.
public static void main(String args) – This is the main method that’s built into Java. When our program runs, this method will be the one that’s executed.
System.out.println(“Hello Java!”); – This line of code just prints out “Hello Java!” to the console. To put it quite simply, we are telling the System to output a string “Hello Java!”
So when we run our program (Ctrl+F11 on Windows) our output looks like this:
- Make sure that your class name matches the filename. That includes any capitalization that you might have. As you can see, helloworld is all in lower case in both the filename and class name.
- Get used to indenting your code. Different companies have different conventions, but I usually hit the tab key whenever I need to indent code.
Congratulations! In this lesson, you created your first Java program. It’s not exciting I admit, but it’s a first small step into a much larger world! In the next lesson, we’ll discuss Data types to store interesting things within our program and how we can comment our code to make our programs much easier to understand.