[Java] Hello World!

You’ve probably heard of the famous Hello World program in the past. Why is Hello World so famous? Well, it is usually the first thing that each programmer write in a programming language. It demonstrate the simplest ability to display text.

So what are we waiting for? Let’s dive in!

If you installed Netbeans, and follow the last lesson, you should have a HelloWorld class created for you with a template. See all those grayed out text? Those are comments, they don’t get compiled, they are just ignored by the compiler. Delete all of them for now, you should have something like below.


public class HelloWorld {

    public static void main(String[] args) {

    }

}

Now, let’s try to understand this a little bit. My class name is HelloWorld, as you can see in line #1 above. Because of that, my file name must be HelloWorld.java, or else there will be an error. If you wish to change the class name, you must also change the file name as well. To do that, you should use Netbeans’ Refactor function to ensure your codes don’t get screw up.

Let’s try this now, right click on your file under source package  in the Project panel, Refactor | Rename, put in MyName for New Name, and click Refactor. See how Netbeans now is changing everything for you automatically?

Anyway, back to the program.

public class MyName {

    public static void main(String[] args) {

    }

}

Well, now that we totally screwed up the HelloWorld program, I guess we just have to do something else.

So line #3, that’s pretty much the standard way to start a program out. This is called the main method, commands you put inside (between { and } ) are the stuffs that your software will run.

Okay, let’s get to the fun part already! Type, don’t copy and paste, the following in between line # 3 and line # 5.

//This is a comment, the compiler will ignore this entire line.
//Btw, you can put your own name instead of mine.
System.out.println("Hello World, my name is Billwaa!");

If you actually typed the codes into Netbeans instead of copy and paste, you will see why I love Netbeans. Yes, it automatically complete writing the method for you and it show you how to use it!

Alright, now you should have something like this:


public class MyName {

    public static void main(String[] args) {

        //This is a comment, the compiler will ignore this entire line.
        //Btw, you can put your own name instead of mine.
        System.out.println("Hello World, my name is Billwaa!");

    }

}

Click the Big Green Triangle Button, or Run | Run Main Project in the menu, to run the program, and hey, what did you see display on the bottom of the screen? 😀

Alright time to mess around on your own. Try output multiple line messages, try using  System.out.print(“Put your message here”); instead, what you see? Try combining the two different commands and see what happen.

You will probably get a bunch of errors messing around. Don’t worry, you are not going to kill anything. Put your mouse over the underlined error command, and read what Netbeans tell you is wrong with it, try to fix it.

*Hints* – All methods called must end with a semicolon ; 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s