Oct 262012
 
Article Java

In this post we will go through the first steps to be followed to start programming in the Java language

Installation of the development environment.

There are many good quality integrated development environment (IDE) to develop applications in the Java language. These environments offer the basic development tools: Text editor with code highlighting, debugger, etc. Among them, Eclipse is by far the most widely used, and we will cover this IDE in another post. But first, we will explain how to write small java applications using only our text editor of choice (Windows Notepad, Linux vim,…).

To run a Java application, the JRE (Java Runtime Environment) must be installed in our computer. We issue “java -version” command in a command window to check if the JRE is already in place. For instance, in a Windows machine:

But, if we want to develop applications, we must install also the JDK (Java Development Kit). One of the main tools of the JDK is the java compiler (javac). The compiler transforms the java source code into bytecodes that can be executed inside the Java Virtual Machine (JVM). This compilation step is an important difference betweeen Java and languages such as PHP and Perl. These languages are interpreted, that is, the compilation happens at the time the script is run, and is transparent to the user.

To check if the JDK is installed in our computer, issue the “javac -version” command to detect the presence of the compiler:

We can see from the output of the “javac -version” command that the compiler is not found, and therefore we must proceed to install the JDK. To do this, go in your browser to the java downloads page http://www.oracle.com/technetwork/java/javase/downloads/index.html and download the Java SE package that matches the operating system (Windows/Linux/…) and hardware platform (32/64 bits)of your computer. We downloaded for this post the 90.3 MB file “jdk-7u9-windows-x64.exe” for Windows 64bit .

The downloaded file is a “Java SE” (standard edition) package, suited for the development of applications running on servers, desktops and laptops. There are also specialized packages, namely  “Java EE” (Enterprise Edition) for the development of web applications,  “Java ME” (Micro Edition) for the development of apps for mobile devices, “Java Embedded”, etc. which are outside the scope of this post.

Once downloaded, execute the package to perform the installation. The iinstallation of the JDK also performs by default the installation of the JRE. We can uncheck the corresponding checkbox in the installation dialog window if for some reason we don’t need it.

Once done with the installation, the JDK tools (including the javac compiler) can be found in our Windows laptop under the directory:

And the JRE tools, including the ‘java’ JVM, are located under the directory:

These directories can be added to the PATH environment variable to make it possible to invoke the tools without having to type the full paths:

“Hello World” in Java

We are finally ready to write our first Java program! using any text editor, create the file “Hello.java” with the following contents:

Then, compile it:

The compiler creates a file “Hello.class” in the same directory, containing the compiled bytecodes:

Finally, run the program in the JVM:

This basic Hello World program has allowed us to verify that the JDK is correctly installed and ready to use.

Moreover, in the source code of this small program we can identify some key elements of the Java language:

Classes and objects.

The code is enclosed in a “class Hello { … }” block. A Java program is structured in classes. A class is a set of “attributes” (data in the form of variables holding values) and “methods” (functions/subroutines). In the simple Hello World program, the Hello class has no attributes, and has a single method named “main”.

Variables.

The “main” method in our sample program receives an argument named “args” of type “String args[]”; this is an array (identified by the “[]” characters), holding values of type “String”. The Java language is “strong typed”, meaning that a variable can hold only data of a give data type. Complex data types can be constructed using structures of type Array, Hashtable, etc.

Libraries

Our sample program prints a line “Hello World!” using a call to “System.out.println”, that is, a call to the “println” method in the “System.out” class. Normally, a Java program makes heavy use of libraries of classes with methods implementing the required functionality, be it accessing a database, generating graphs, or any other kind of processing of the information being handled.

In coming posts in this series we will delve deeper into these and other concepts of the Java language.

 Posted by at 7:44 am

 Leave a Reply

(required)

(required)