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.

Categories
Java Uncategorized

Java 101 – Lesson 2 – Getting familiar with Java

I’ve never been too fond of books when trying to learn a language for the first time. The reason is that they give you too many details in the beginning that you may not necessarily be interested in until later on. The approach that I’ve found to be very useful in learning or teaching is to let the user/student get an overview first and then go into how things actually work. So, in this article we are going to write a simple application/program and run it. As we move on, I will explain what is going on.

Hello World

For those who don’t know, Hello world is a very popular program in the computing world. Almost every language’s tutorial has one for beginners. All it does is print Hello world on the screen in one form or another. This is what we will do too. We will write a program that will print “Hello world” in your console window.

Create a folder in your c:\ drive and name it java_projects. This is where we will place all Java files.

Open notepad and type this in it :

import java.io.*;

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello world");
    }
}

After copying and pasting the above code in your notepad window, save the file as HelloWorld.java in c:\java_projects\ . When you save it, make sure that file type is selected to “All files” or notepad will save your file as “HelloWorld.java.txt” and not “HelloWorld.java” .

Saving Java file
Saving Java file

Oh, and you will have to save the file as HelloWorld.java (with the capital letters) or the code won’t compile. I will get to the reason in later articles.

We have written the code. Now, we need to compile and run it. We will do that by opening the command prompt (also referred to as the console) and do it from there. Click on start then on Run and type cmd in the dialog box that opens up.

Run dialog
Run dialog

A black window will appear with a blinking cursor. It is the command prompt. Type this in it :

cd c:\java_projects\

Then type :

javac HelloWorld.java

The screen will get stuck for a few seconds and then you will see a blinking cursor again. javac is used to compile java code into byte-code. If you go to c:\java_projects\ in windows explorer, you will see that there javac created a file called HelloWorld.class . This is the byte-code file that the command “java” needs to run the application. You can run your code by typing :

java HelloWorld

A line of text saying Hello world should appear in your console. Congratulations ! you just ran your first application.

Click to enlarge
Click to enlarge