The Java Environment - JAVA: Easy Java Programming for Beginners, Your Step-By-Step Guide to Learning Java Programming (2015)

JAVA: Easy Java Programming for Beginners, Your Step-By-Step Guide to Learning Java Programming (2015)

Chapter 2. The Java Environment

Getting a closer look on Java programming, this chapter will start its discussion on how you can use and deploy the software in various environments. It will also present a step-by-step instruction on how to install, not only the Java program itself but also other required tools on your computer.

With the unstoppable growth of the World Wide Web, developers have found ways on how to incorporate Java programs on every web page. The following is a summary on how the software language is used in different environments:

· Applet - A type of networked Java program embedded in a Web page that is automatically executed by another Java-compatible browser when transmitted over the Internet. Usually this is a small program that can display data from the server, manage user input or even perform simple functions like a calculator. These functionalities are done locally on the client’s computer instead of connecting to the server, which means when you click an applet link it will be automatically downloaded and run in the browser.

· Servlet - A type of small Java program that supports a browser but runs on a different computer or web server to extend its functionality. The introduction of the servlet has upgraded the client/server connection.

· JavaServer Page (JSP) - A Web page that consists of fragments of a Java program (as opposed to an applet that has the complete program).

· Micro Edition (ME) Java Application - A Java program running on a resource device with a limited amount of memory, such as a mobile phone or a television set-top box.

· Standard Edition (SE) Java Application - A Java program that runs on a standard computer, such as a desktop or a laptop.

· JavaFX - A Java program that is integrated with multimedia platforms, such as Flash players.

The Initial Java Setup

Before you sit in front your computer and start coding, you need to make sure that you and your machine are equipped for writing Java codes. It is strongly advised that you check out the following websites and follow the instructions in downloading the required software that is usually for free:

For Java software download and installation follow the following steps:

1. Go to the website http://java.com by typing this in the address bar of your web browser. It will take you to the screen below:

2. Click on the FREE JAVA DOWNLOAD button. It will take you to this screen:

3. Click on the AGREE AND START FREE DOWNLOAD button. When your Java download is complete, you have to close all your browsers and reload them to enable the Java installation.

4. Run the downloaded file and you will get the screen below. Just click the INSTALL button.

· www.oracle.com/technetwork/java/javase/downloads - Download and install the required Java SE documentation (also known as the Javadoc pages or Java SE API Docs)

Next:

1. Go to the website www.oracle.com/technetwork/java/javase/downloads , It will take you to the screen below:

2. Click on this option

3. The next page will ask you to choose the version of Java SE Development Kit you want to download. Make sure you know exactly what operating system you have in your computer or laptop before you start downloading.

4. To check the version of your operating system, you can go to the SYSTEMS or PC INFO settings of your computer/laptop.

5. Open the downloaded program to complete the installation of your Jave SE Development Kit. Ensure that you read and follow the steps carefully.

· http://eclipse.org/downloads - Download and install the Eclipse program you need

Then;

1. Go to the website http://eclipse.org/downloads by typing this in the address bar of your web browser. It will take you to the screen below:

2. Choose the appropriate version of Eclipse based on your computer’s operating system. For a 64-bit Windows operating system (OS), you will get the screen below:

3. After downloading, open the program and you will get the screen below:

4. Just choose ECLIPSE IDE FOR JAVA DEVELOPERS option and then click the INSTALL button. Make sure you accept the ECLIPSE FOUNDATION SOFTWARE USER AGREEMENT.

After downloading the programs, make sure you test your installed software. You can follow these steps to test Eclipse:

· Launch Eclipse. You should be able to see an ECLIPSE JAVA MARS icon on the desktop. It will then ask you to SELECT A WORKSPACE. Just leave what is inside the box and then click the OK button.

· Create a new Java project by going to the menu on top: FILE > NEW > JAVA PROJECT

· Create a new class named Displayer within the Java project.

· Modify the new Displayer.java file by entering the following lines of code in the Editor pane.

public class Displayer {

public static void main(String args[]) {

System.out.println("Hello Java!");

}

}

· Execute Displayer.java by clicking on this button located at the top and check to make sure that the output reads Hello Java! by checking the figure below.

In the field of computer programming, software developers require existing programs to create new programs. For example, you want to develop a Java program that will handle your company’s employee records. In order to accomplish this, you might use an existing program as an error-checking tool for your Java codes.

Required Java Tools

Below are the different tools needed in Java programming that can be downloaded online for free:

· Compiler

A compiler turns the Java code you write into something that can be understood and run on your computer. Computers do not comprehend instructions same like humans. For example, check the program code below:

Looking for an available rental car:

// This is part of a Java program

// (not a complete Java program).

rentalcarNum = 1;

while (rentalcarNum < 100) {

if (client[rentalcarNum] == 0) {

out.println("Car " + rentalcarNum

+ " is available.");

exit(0);

} else {

rentalcarNum ++;

}

}

out.println("All cars or vehicles are rented out");

This basic Java code searches for any available rental car or vehicle for a certain client, assuming that the company has 99 cars in total. You cannot run this code by just typing as it is. However, ignoring the strange punctuations and typing format, human beings are able to grasp what it wants the computer to do:

Set the rental car number to 1.

As long as the rental car number is less than 100,

Check the number of clients that rented the car.

If the number of clients that rented the car is 0, then

report that the car is available,

and stop.

Otherwise,

prepare to check the next car by

adding 1 to rental car number.

If you get to the non-existent rental car number 100, then

report that all cars or vehicles were rented out.

As for computers, they do not follow English-translated instructions to do a specific task. Instead, they understand cryptic Java bytecode commands. This is where the compiler comes in. After writing your Java source code, it is being translated and written by the compiler into Java bytecodes that the computer can execute. Check bytecode counterpart below for the same Java code:

aload_0

iconst_1

putfield Company/ rentalcarNum I

goto 32

aload_0

getfield Company/client [I

aload_0

getfield Company/rentalcarNum I

iaload

ifne 26

getstatic java/lang/System/out Ljava/io/PrintStream;

new java/lang/StringBuilder

dup

ldc " Car "

invokespecial java/lang/StringBuilder/<init>(Ljava/lang/String;)V

aload_0

getfield Company/rentalcarNum I

invokevirtual java/lang/StringBuilder/append(I)Ljava/lang/StringBuilder;

ldc " is available."

invokevirtual

java/lang/StringBuilder/append(Ljava/lang/String;)Ljava/lang/StringBuilder;

invokevirtual java/lang/StringBuilder/toString()Ljava/lang/String;

invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V

iconst_0

invokestatic java/lang/System/exit(I)V

goto 32

aload_0

dup

getfield Company/rentalcarNum I

iconst_1

iadd

putfield Company/rentalcarNum I

aload_0

getfield Company/rentalcarNum I

bipush 100

if_icmplt 5

getstatic java/lang/System/out Ljava/io/PrintStream;

ldc " All cars or vehicles are rented out"

invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V

return

The source code can be saved in a file named Company.java and the compiler probably puts the Java bytecode in another file named Company.class. You even need a tool to display a text-like version of a Java bytecode file (you can use Ando Saabas’s Java Bytecode Editor). If you try open the code of the Company.class file using Notepad or even Microsoft Word, you will only see gibberish characters like dots, squiggles and more.

Java Virtual Machine (JVM)

If the compiler is responsible for writing the Java bytecode, then the Java Virtual Machine is in-charge of deciphering it. In reality, each kind of computer processor has its own set of executable instructions that are interpreted by each operating system in a slightly different way. Let’s take a look at the two programs below for two different processors but will display the same output on the computer screen – “Hello world!”.

Simple Program- Pentium Processor

.data

msg:

.ascii "Hello, world!\n"

len = . - msg

.text

.global _start

_start:

movl $len,%edx

movl $msg,%ecx

movl $1,%ebx

movl $4,%eax

int $0x80

movl $0,%ebx

movl $1,%eax

int $0x80

Simple Program- Powerpc Processor

.data

msg:

.string "Hello, world!\n"

len = . - msg

.text

.global _start

_start:

li 0,4

li 3,1

lis 4,msg@ha

addi 4,4,msg@l

li 5,len

sc

li 0,1

li 3,1

sc

These two programs ought to display the same results but they cannot be interchanged, for the instructions will mean nothing when they are run by the wrong processor. You will either get notification or error messages like “Not a valid Win32 application” or “Windows can’t open this file.” That is why, with the help of Java Virtual Machine, these Java bytecodes create order in this chaotic world of programming! JVM acts like an interpreter that translates those bytecodes for any computer system to comprehend. Thus, you do not have to worry because whether it is your computer or your friend’s one, both will be able to run the bytecode. Thus, Java language has solved the issue of portability and versatility through JVM.

Integrated Development Environment

The early days of Java programming involved opening several windows – one for typing the code, another for running the program, and maybe a third one to keep track of all the codes you have written. What a messy situation! With the emergence of the integrated development environment (IDE) all of the functionalities were seamlessly combined into a single well-organized application.

Below are some of the most popular Java IDEs that can be downloaded online for free (their features may vary but still the language remains exactly the same):

1. Eclipse (www.eclipse.org)

Regarded as one of the most popular and best-looking interfaces because of its interactive design and navigation features, Eclipse is characterized as a user-friendly and open source software.

2. NetBeans (https://netbeans.org)

Just like the previous program, this open source IDE is one of the most recommended Java programming environments for beginners. It is also fast and powerful that can support all Java platforms from Standard Edition (SE) to FX.

3. BlueJ (http://bluej.org/)

BlueJ is described to have the simplest IDE interface and specifically designed to help a novice user understand the fundamental concepts of Java programming.

4. DrJava (www.drjava.org)

DrJava was also designed for amateur programmers who want a simple interface. Despite its simplicity, it is still powerful enough for proficient Java programmers.

5. Jave Development Kit (JDK)

The Java Development Kit is a tool that acts as both a compiler and an interpreter. The latest JDK version can be downloaded from Oracle’s website - www.oracle.com/technetwork/java/javase/downloads/index.html. Ensure that you download the appropriate version for your computer. If you are using a 32-bit operating system, then download the matching file name affixed with x86. Otherwise, if it is a 64-bit operating system then download the one with x64.

Now, we have learned that Java can be deployed in a multitude of ways. This chapter also gave you a clear set of instructions on how to install the software and all other required tools for the program to run properly. In the next chapter, you will experience how to start coding in a Java environment.