Monday, January 25, 2016

Hello World Program

Now we will begin creation of the simplest program, namely "Hello World" program. This program is a program that usually first made by programmers in learning a new programming language. Here's a guide to make the program "Hello World":
  1. Create "javacode" directory in D: drive, so you will have D:\javacode directory. We will use this directory to place our Java source code.

  2. Run text editor. In this example, we use Notepad. For those of you who know Integrated Development Environment (IDE) such as Eclipse or NetBeans, we have not used the IDE at this time, because by using Notepad we will understand the basic techniques of Java programming.

  3. Write the following code in it:
    /*
       This is Java code.
       File of this code should be saved with the name HelloWorld.java.
    */
    public class HelloWorld {
       //Execution of the program will be started by calling main().
       public static void main(String[] args) {
          //Print Hello World to the console.
          System.out.println("Hello World");
       }
    }
    
  4. Save with the name HelloWorld.java
In most other programming languages, the name of the file that containing the program code can be saved with any name. However, in Java programming, the file name must exactly match with the name of the class that has the main() method. Thus, for example in the program above, we must save the file with the name HelloWorld.java. The following image display our Java code in Notepad:



Now, let's see more about the program code above. Although it looks a little (only consists of a few lines of code), the above program has several key features that common and is applied to all Java programs. First, the above program begins with the following code:
/*
   This is Java code.
   File of this code should be saved with the name HelloWorld.java.
*/

The above code is a program comment. Program comment is part of a program that will not be translated during the compilation process. The presence of the program comment is only used to provide the required information and will not affect the course of the program. Programmers usually use comment to write down the information about the code: when created, when modified, whom the author, and the most common is to write a logical process (algorithm) of the code created. Java provides three ways to write program comment: using sign /*..*/, //, and /**..*/. Comment that preceded by /* and end in */ may consist of a few lines, while starting with // will only apply to a single line. In code above we can see comment that has only one line, that is:
//Execution of the program will be started by calling main().
and
//Print Hello World to the console.

The third type of comment (started with /** and end with */) are used to create documentation of using javadoc program.

The next line is as follows:
public class HelloWorld {

Line of code above using the class keyword to define a new class and followed by the name of the class. In Java, all programs are classes. Opening curly brackets, {, is used to initiate a program block, while the closing curly brackets, }, used to end a program block. Program block can be any class, method, selection structure, repetition structure, and so on. In this time, we do not discuss about the presence of public keyword contained in the code line. We will review it later in the next chapter that talks about package and interface.

Next, the HelloWorld class contained the following line of code:
public static void main(String[] args) {

The above line would start main() method. The main() method is the primary method to be executed first time when the program starts. If you ever learn C or C++ language, main() method contained in Java will be the same as the function of main() contained in C or C ++.

The next line of the code above is as follows:
System.out.println("Hello World");

The code used to display the "Hello World" text to the console.

Lastly, the above code is closed with closing curly brackets sign, }. The sign is used as the final part of the HelloWorld class definition.


Compile and Run


To compile and run a program that we made, we need to use command line via Command Prompt. The steps are as follows:
  1. Open Command Prompt, and go to our Java code directory in D:\javacode.

  2. Perform compile a file using the command javac HelloWorld.java. If the compilation process runs smoothly without any errors, then system will not display anything in the console, but in that directory system will create a new file with the name HelloWorld.class. This file is called a bytecode and this file is the end file of program that we created.




  3. To run or execute the program, type command java HelloWorld. System will display "Hello World" text to the console, as shown as follows:


Thursday, January 21, 2016

Installing Java

Oracle provides Java Platform in two types: Java Runtime Environment (JRE) and Java Development Kit (JDK). The JRE consists of the Java Virtual Machine (JVM) and other components needed to run program written using Java language. While JDK shall be composed of the JRE itself plus development tools, compilers, and also debuggers necessary for creating programs written using Java language.

The Official statement from Oracle: "The JDK is a development environment for building applications, applets, and components using the Java programming language. The JDK includes tools useful for developing and testing programs written in the Java programming language and running on the Java platform".

Because we require JDK for create Java programs, we will install the JDK, which will automatically install the JRE as well. Please download JDK below for Windows platform. There are two options, for Windows 32 bit and 64 bit version.

jdk-8u71 means Java Development Kit version 8 update 71. To download other JDK version or update, please visit http://www.oracle.com/technetwork/java/javase/downloads/index.html.

To check the Windows version you are using whether 32 bit or 64 bit, go to Control Panel - System and Security - System.



After downloading JDK, you can install it like other installer in Windows, just select default value and click Next. After JDK installed successfully, Java will be installed in C:\Program Files\Java\ directory by default, in this case is C:\Program Files\Java\jdk1.8.0_71.

To ensure your JDK installed successfully, open Command Prompt and type java -version and press Enter. This will show Java version you already installed.




Setting CLASSPATH


CLASSPATH is a system variable that is used to tell the program which is written in Java (including the tools included in the JDK) the location where the classes to be used. By setting the value of the CLASSPATH variable, then we can do the compilation and execution of the program that we created in any directory.

If you open Command Prompt, then type javac and press Enter, the result will look like this:



The above message appears because the CLASSPATH has not been set. But if you go to JDK bin directory then type javac and press Enter,  the result will look like this:



javac is a command to compile Java source code. javac.exe resides in JDK bin directory. This is just for sample, If CLASSPATH has not been set, you just can compile and execute Java program under JDK directory. Now we will set the CLASSPATH, so we can compile and execute Java program in any directory. The steps are as follow:

  1. Go to Control Panel - System and Security - System, click Advanced system settings.



  2. System Properties will appear. Click Environment Variables...


  3. Environment Variables will appear. In System variables section, Scroll down and click Path in Variable column, then click Edit...



  4. Edit System Variable will appear. Add JDK bin directory string: ";C:\Program Files\Java\jdk1.8.0_71\bin" (without quotation marks) to Variable value then click OK. Important! add Variable value, not replace.



  5. Back to Environment Variables, then click OK.

  6. Back to System Properties, then click OK.

  7. Now your CLASSPATH has been set. You can compile and execute Java program in any directory. To check your CLASSPATH has been set successfully, open Command Prompt and type javac and press Enter in any directory, your console will print all parameters can be used for javac, as shown in the following image.