Categories
Java Uncategorized

Java 101 – Lesson 3 – Dissecting Hello World

This article is a continuation of articles in the series of Java 101. You will need to read lesson 1 and lesson 2 to completely understand this article (or blog post, or whatever you want to call it).  In lesson 2, we executed a piece of code, which printed “Hello World” on the command prompt (also called the console). The code looked something like this :

/**
 * This is a package. You need to import this to print anything to the command prompt,
 * get information from the user on the command prompt, to read/write to a file, etc.
 * For now, just keep in mind that you need to have this line in almost all java applications
 * you write
 */
import java.io.*;

/**
 * This is a class which will contain the method you will run, to run your program.
 * As we move along in these tutorials, you will see that this is a common structure
 * in all Java applications. Everything is placed in a class. I will explain this more
 * when we learn about classes.
 *
 * Also this is a public class, which means it has to be put in a file whose name is
 * the same as its name (which will be HelloWorld.java). If you name this class
 * HelloJoe, then you will have to save this whole code in HelloJoe.java
 */
public class HelloWorld {
    /**
     * Forgive the cliche but this is a method which you will use in all Java applications
     * you will write. This method is run is the one that the Java Virtual Machine will
     * look for, when it is asked to run your application. Think of this as a starting point.
     */
    public static void main(String[] args) {
        /**
         * This is the place where you write your code. Give Java instructions on what you
         * want it to do, etc. In this case, we are just printing out a statement - "Hello World".
         * Just for fun, try changing the text "Hello World" to "I have ice cream" and run the
         * application. The statement "I have ice cream" will be printed on the command prompt
         */

        System.out.println("Hello world");
    }
}

I added some extra text in there to  make it easy to understand what each line is doing. Notice that the extra text I added is in between /* and */ . Any text placed in between /* and */ is called a comment. Comments are pieces of information that are ignored by the compiler when it compiles and runs your code. They are meant to explain what your code is supposed to do, which is very helpful when the code is 3000 lines. As we move through the lessons, I will place comments in my example code so you should have an idea of how they are generally used.

The comments in the above code should explain pretty much everything about the Hello World application (or program). What you can do is copy this same structure for any program you write. And, just to be clear this is the structure :

import java.io.*;

public class AnyClassName {
    public static void main(String[] args) {
         // This is where you will write your code. Everything else will remain the same
         // You can change the class name though (which is AnyClassName here)
    }
}

Now that we know the basic structure of a Java program we will start learning some actual programming in the next lesson.

By Moazzam

Moazzam is a software developer working in the big apple. He has over a decade of experience in development. While most of his work involves developing web applications, he does development for Android and Windows Mobile in his spare time.

2 replies on “Java 101 – Lesson 3 – Dissecting Hello World”

Thank you again! I now understand / remember the basic structure of a program. Still trying to understand some of the errors I’m getting when writing basic code. One specific says ‘{‘ expected but as far as I can tell, the { is where it should be on the line it says it should be on. Hopefully I will find the answers in more of your ramblings. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *