Friday, 12 May 2017

Tutorial 1: Hello World Java Program



In this tutorial, I will show you how to write a Java program that prints out “Hello World!” or anything else you want. If you are watching this, there is a high likelihood that this is your first time learning Java and that is cool. Remember that the first step is the most important one; then persist to the end.
Okay. So you should have Java set up on your system and an IDE such as Eclipse, NetBeans, IntelliJ, etc. I will use Eclipse for this demonstration. So first, open your IDE.

Go to File -> New -> Java Project.

Give your project a name. I’m going to name mine “Fundamental Java” 



Figure 1: Creating a new Java project in Eclipse.

Then click Finish

Now you should see your project on the left pane of the project window.


 Figure 2: How Eclipse looks after creating your project.


Right-click your project name and on the options that appear, select New -> Java Class.

 Figure 3: Creating a new Java class.


A window appears. Give your project any name. However, make sure that it starts with a capital letter (uppercase letter) and that if you use two names, that there is no space between them. Your class name should not have any punctuation and must always begin with a letter. Do not use an integer as the starting character of your class name.
For example, you can name it HelloWorld, Hello_World, Jack, Jill, TomTheCat etc.

You cannot name your class 1HelloWorld or :HelloWorld or +HelloWorld. As you learn Java, you will understand why these conventions are important.

After giving your project a name, click Finish and your class should open up in the editor. It looks as shown below:

 Figure 4: The project window with the Java class opened.


The image below shows the main parts of the window that should be of interest to you.

 Figure 5: Parts of the IDE that should interest you.

So now we can write our program: I know that some things may not make sense to you, but just write them anyway. You will know them by heart within no time. Just like when you’re learning a new language, you need to practice and soon enough, it becomes second nature.
So, your program should look like this:

public class HelloWorld {

         public static void main (String[] args) {

                        System.out.println(“Hello World!”);

            }

}

Let me explain a few things:

public static void main (String[] args)… Notice the word main in this line of code. This is where the program will start executing. Here’s why this is important. If you have children who are old enough to do household chores, you can give them instructions like this: I want you guys to sweep the house, dust the surfaces, wash the dishes and clear the dining table. But start by dusting the surfaces

In future tutorials, I will show you how to write complex programs with many classes, and you will see why this is important. Your program may have many instructions, but you need to tell it where to start executing those instructions.
System.out.println(“Hello World!”); … This just tells the program to output whatever text is between the brackets onto the console. So in this instance, we are telling the program to write the text “Hello World” to the console.

So after writing your program, click the “Run” button and the words “Hello World!” should appear on the console.


 Figure 6: Running the program.

You might want to practice writing this program several times a day, just to ensure you understand everything. Practice it until you can write it without referring to this tutorial.

If you have any questions or comments, please leave them below and I will address them as soon as possible.

No comments:

Post a Comment