"Hello World!" program

"Hello World!" program is the first lesson which people learn in programming code. Before we start, make sure we already JDK on our PC.  If you don't have please download it.

Download JDK

After download the JDK, install the software.
JDK will be used to compile the source code (.java) file into bytecodes (.class) file.
[How it work?]
I would recommend any newbie like me to read the link above before continues.

Here the source code for "Hello World!" Program.
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }//End of main
}//End of HelloWorld Class

  1. Open a notepad, copy the source code into the notepad then save as HelloWorld.java (case sensitive).
  2. Now you will need to use the JDK to compile the source code (.java) file into bytecode (.class) file. On your windows, go to DOS or command prompt  (click start > run > type cmd]
  3. On DOS, Compile the file from DOS prompt by typing javac HelloWorld.java. Successful Compilation, results in creation of .class containing byte code
  4. Execute the file by typing java HelloWorld

 In short, the procedure of saving, compiling and running java work in this way
Step 1:Save the program With .java Extension.
Step 2:Compile the file from DOS prompt by typing javac <filename>.
Step 3:Successful Compilation, results in creation of .class containing byte code
Step 4:Execute the file by typing java <filename without extension>

In case, you faced any error when entering command in DOS, here are the troubleshoot.

1. ‘javac’ is not recognized as an internal or external command, operable program or batch file
When you get this error, you should conclude that your operating system cannot find the compiler (javac). To solve this error you need to set the PATH variable.
How to set the PATH Variable?

Firstly the PATH variable is set so that we can compile and execute programs from any directory without having to type the full path of the command. To set the PATH of jdk on your system (Windows XP), add the full path of the jdk<version>\bin directory to the PATH variable. Set the PATH as follows on a Windows machine:

a. Click Start > Right Click “My Computer” and click on “Properties”
b. Click Advanced > Environment Variables.
c. Add the location of bin folder of JDK installation for PATH in User Variables and System Variables. A typical value for PATH is:

C:\jdk<version>\bin (jdk<version is nothing but the name of the directory where jdk is installed)

If there are already some entries in the PATH variable then you must add a semicolon and then add the above value (Version being replaced with the version of JDK). The new path takes effect in each new command prompt window you open after setting the PATH variable.

2. Exception in thread “main” java.lang.NoClassDefFoundError: HelloWorld
If you receive this error, java cannot find your compiled byte code file, HelloWorld.class.If both your class files and source code are in the same working directory and if you try running your program from the current working directory than, your program must get executed without any problems as, java tries to find your .class file is your current directory. If your class files are present in some other directory other than that of the java files we must set the CLASSPATH pointing to the directory that contain your compiled class files.CLASSPATH can be set as follows on a Windows machine:

a. Click Start > Right Click “My Computer” and click on “Properties”
b. Click Advanced > Environment Variables.

Add the location of classes’ folder containing all your java classes in User Variables.

If there are already some entries in the CLASSPATH variable then you must add a semicolon and then add the new value . The new class path takes effect in each new command prompt window you open after setting the CLASSPATH variable.


The Basic - How Java work...

Before I start, I would like to share with you a nice video which greatly enhanced my understanding about Java.

It's ok if you still don't get the whole picture. Now read these....

"Java was designed with a concept of ‘write once and run everywhere’. Java Virtual Machine (JVM) plays the central role in this concept. The JVM is the environment in which Java programs execute. It is a software that is implemented on top of real hardware and operating system.

When the source code (.java files) is compiled, it is translated into byte codes and then placed into (.class) files. The JVM executes these bytecodes. So Java byte codes can be thought of as the machine language of the JVM." - source javabeginner.com

Now watch the video again to understand better. Follow "Hello World!" program and try how Java work for the first time.

Object Oriented Programming (OOP) Language

Java is a Object-Oriented Programming language (OOP). So what is OOP language?  The characteristic of OOP language is the sum of the parts of a program make up the whole. I use building a car model for example. You build the engine first. It can stand alone. It is an engine and everyone can see it's an engine. Next you build the body. It can also stand alone. Finally, you build the interior including the seats, steering wheel, and whatnot. Each, by itself is a object. But it is not a fully functioning car until all the pieces are put together. The sum of the objects (parts) make up the whole.

The point is that in these languages, you build objects out of classes of commands to create the whole. That's the terminology of OOP language. That all I know and learn about OOP language today.


Java or JavaScript?

Before i decided to learn Java, I'm confused by the term JavaScript and Java. Aren't they are the same? So I search through google and finally it answered my question. As you can see in the picture above, JavaScript are not equal to Java. They shared some similarity but they are two different things.

Let's start with their similarity
  • Both Java and JavaScript are object oriented programming languages (What is OOP language?)
  • Both work within HTML documents, but in different ways (will explain later)
  • Both were created by big computer companies about 15 years ago
    • Java created by James Gosling of Sun Microsystems.
    • JavaScript was created by the fine people at Netscape.

What differences between them?
  • They work with HTML in different way. Java can stand on its own while JavaScript must be placed inside an HTML. 
    • JavaScript program is actually part of the HTML document, with the language written directly into the code of the document.
    • Java programs are standalone and the HTML document simply serves as a home for the program. 
  • They also different in  how the language is presented to the end user
    • Java must be compiled into what is known as a "machine language" before it can be run on the Web. Once the Java is compiled, it is set. If you need to edit the code, you need to go back to the original text and alter it, but then you need to compile again.
    • JavaScript is text-based. You write it to an HTML document and it is run through a browser. If you need to do some editing, simply go into the HTML document and change some code.

Both of these two codes can create great website with interactive features. But the reason I choose to learn Java instead of JavaScript is because Java offer more than JavaScript. JavaScript are limited by constraints of the HTML document in which it is embedded. On the other hand, Java offer more possibility and all sort of amazing application can be done by it. Besides, it is a more challenging language, I like challenges. : )


The journey begin...

Hello world! I am a Java beginner, just started to learn java today. I'm not a programmer, computer engineer, technician, or anyone who know Java at all. I'm just an average Joe who felt interested with this Java programming. I saw what Java is capable of and I'm astonished. From internet applet, game to small smart phone applications, all of them involved Java. I'm impressed by it's possibility. I want to master it!

I don't know if I am able to master this programming language, but I'm ready to put hard work on it. I don't have strong background knowledge about Java. All I  know are some basic html code and visual basic code.

The reason I write this blog is to record and summarized what I have learn about Java everyday. It can be serve as note for me to refresh my memory every time I see it. As for you who are reading, if you are also interested about Java, I hope we can share experiences and knowledge here. Feel free to drop a comment!
Related Posts Plugin for WordPress, Blogger...