Java Introduction

Before we can begin to write programs in Java, it would be interesting to find out what really is Java, how it came into existence and how does it compare with other computer languages. Also, it is important to know what tools we are going to use for executing programs in this tutorial, from where to get them and how to install them. In this chapter, we would briefly outline these issues.

What is Java?

Java is a programming language developed at Sun Microsystems in 1995. It was designed by James Gosling. The language derives much of its syntax from C++ (and its predecessor, C), but is simpler to use than C++. Java’s reputation has spread wide and far and has captured the imagination of most software professionals. Literally thousands of applications are being built today in Java for different platforms including desktop, web and mobile. Possibly why Java seems so popular is because it is reliable, portable and easy to use. It has modern constructs that are required to represent today’s problems programmatically. Java, like C++ makes use of a principle called Object-Oriented Programming (OOP) to organize the program. This organizing principle has lots of advantages to offer. We would be discussing this in detail in Chapter 8. Let us now try to understand how Java achieves portability and reliability.  

Traditional Programming Model

When we execute a program on any computing device like PC, Laptop, Tablet or Smartphone, the instructions in it are executed by the microprocessor present in that device. However, the microprocessor cannot understand the instructions written in languages like C, C++ or Java. Hence these instructions have to be first converted into instructions that can be understood by the microprocessor. These converted instructions are in machine language. This conversion process is known as compilation .

The machine language instructions understood by a microprocessor are often called its Instruction Set. Problem is that instruction sets of different microprocessors are different. Thus instructions of an Intel microprocessor are different than an ARM microprocessor. Therefore, any program being executed on a specific microprocessor needs to be converted into machine language instructions that that microprocessor understands. Thus, for the same program, corresponding machine language instructions would be different for different microprocessors. Hence if a program is compiled for one microprocessor it may not work on another microprocessor. To make it work on another microprocessor it would have to be compiled for that microprocessor again. 

Any running program needs to make use of services of an Operating System (OS) during its execution. These include services like performing input/output, allocating memory, etc. You must be aware of the fact that on the same microprocessor, different OS can be used. For example, suppose there are two laptops having same Intel Pentium microprocessor. On one laptop one can run Windows, whereas on the other one can run Linux. But since the way these OSs offer different  services is different, during conversion to machine language these changes have to be accommodated. So for the same program, machine language instructions for Intel + Windows combination would be different than those for Intel + Linux combination. 

How is Java Different?

 Java takes a different approach than the traditional approach taken by languages like C and C++. It lets application developers follow a “compile once, run anywhere” scenario. This means that once a Java program is compiled, it can get executed on different microprocessors + OS combinations without the need to recompile the program. This makes Java programs immensely portable across different microprocessors + OS combinations. The microprocessor + OS combination is often called “Platform”. Hence Java is often called a platform-independent language or architecturally neutral language. Java programs are considered portable since they can be used on different microprocessor + OS combination without making any changes in them. Java achieves this “compile once, run anywhere” and platform independence magic through a program called Java Virtual Machine (JVM). When we compile Java programs they are not converted into machine language instructions for a specific microprocessor + OS combination. Instead, our Java program is converted into bytecode  instructions. These bytecode instructions are similar to machine code, but are intended to be interpreted by JVM. A JVM provides an environment in which Java bytecode can be executed. Different JVMs are written specifically for different host hardware and operating systems. For example, different JVMs are written for Intel + Windows combination, ARM + Linux combination, etc. 

During execution, the JVM runtime executes the bytecode by interpreting it using an Interpreter program or compiling it using a justin-time (JIT) compiler. JIT compilers are preferred as they work faster than interpreters. During interpretation or JIT compilation the bytecode instructions are converted into machine language instructions for the microprocessor + OS combination on which the program is being executed. This perfectly facilitates executing Java programs on different machines connected to the Internet. 

A Java program is typically stored in a .java file, and the bytecode is usually stored in a .class file. A complex program may consist of many .class files. For easier distribution, these multiple class files may be packaged together in a .jar file (short for Java archive). The working of a Java program discussed above is shown in.

How Java addresses Security?

Let us first understand what typically happens when we use some web application on the Internet. Through browser on our PC/Laptop/Tablet/Smartphone we use a URL to reach the application  present on some web server on the Internet. The web application sends HTML that gets rendered in our browser. However, except for the simplest of web applications, along with the HTML some executable Java program is also sent to our browser. This program is often small and is called Applet. The purpose of the applet is to make the web application more responsive. For example, if we enter a password, it should be possible to check whether it follows rules for password creation or not right there within the browser using the downloaded applet, rather than sending the password to server and get it verified. This certainly improves user experience. This is because the check is being performed on your machine rather than on the server machine. This saves around trip to the server.    

But when we download an applet there is always a possibility that the
applet contains malicious code like a Virus or Trojan horse that would
cause harm to our machine. JVM prevents this from happening by
restricting the applet code from accessing other resources of your
machine, other than what it is supposed to. This makes applets secure.
Thus JVM solves two dicey issues in one shot—portability as well as
security.

Java or C++?

After learning C, it is often a question whether one should migrate to Java or C++. Answer is both; and that too in any sequence that you want. Though both are Object Oriented programming languages, neither is an advanced version of the other. Learning one before the other would naturally help to learn the second.

It is important to note that both address different sets of problems. C++ primarily addresses complexity, whereas Java addresses portability and security. In my opinion both languages would continue to rule the hearts of programmers for many years to come.

As you start learning Java, you will find that there are many features in it that are similar to C and C++. This is not by accident, but by intent. Java designers knew that he has to provide a smooth transition path to learners of the Java language. That is why Java uses a syntax which is similar in many ways to that of C and it follows many of the object oriented features of C++, though in a refined fashion.  

The Java Environment

As we know JVM contains an Interpreter / JIT that converts bytecode into microprocessor + OS specific machine language instructions. Since instruction sets vary from microprocessor to microprocessor, there exist different JVMs for different platforms. Thus though any JVM can run any Java program, JVMs themselves are not portable.

JVM is distributed along with a set of standard class libraries that implement the Java Application Programming Interface (API). The Java APIs and JVM together form the Java Runtime Environment (JRE). If your need is only to execute Java programs on your machine, all that you need is JRE. For example, if you wish to play a Java-based game on your machine, you need to install only JRE on your machine for the game to run.

However, if you wish to also develop programs on your machine, you need Java Developer Kit (JDK). JDK contains tools needed to develop the Java programs, as well as JRE to run the programs. The tools include compiler (javac.exe), Java application launcher (java.exe), Appletviewer, etc. Compiler converts Java code into bytecode. Java application launcher opens a JRE, loads the class, and calls its main( ) method. Figure 1.4 shows all these pieces of the Java environment.  

Tools of the Trade

To create and run Java programs you need to install two software programs on your PC. These are

(a) Java Development Kit (JDK)  

(b) NetBeans 

Remember that JDK must be installed before installing NetBeans. There are multiple versions of JDK and NetBeans available for download from several websites. The latest versions are JDK 8 and NetBeans 8.2. They can be downloaded together from   
 
http://www.oracle.com/technetwork/java/javase/downloads/index.html
 
On this download page select the appropriate Java SE and NetBeans Co bundle based on the OS (Windows/Mac/Linux) and the Microprocessor (x86 or X64).
 
JDK is often also called Standard Edition Development Kit or Java SE 8 JDK. Basically JDK contains JVM, JRE, and Java compiler and debugger. A compiler is needed to convert the Java program into its equivalent bytecode. A debugger is needed to detect, analyze and eliminate bugs in the program.
 
When you are developing a Java program you need an editor to type the program. Small Java programs can be typed in one file. But more sophisticated programs may be split across multiple files. To let you type the program, manage multiple files of your program, compile it and debug it, you need a tool that can let you carry out these tasks in a visual and user-friendly manner. This tool is often called an Integrated Development Environment (IDE). One such IDE that is very popularly used for building programs in Java, is NetBeans. All programs in this book have been created using NetBeans IDE.
 
Once you download the JDK and NetBeans bundle you need to install it. This is a fairly simple job and I am sure you would be able to do this easily. You simply have to double click the downloaded installer file jdk8u141-nb-8_2-windows-i586.exe (assuming Windows and 64-bit machine configuration), and the installer will guide you through the installation process.
 
We are now on surer grounds. We now have the historical perspective of what led to creation of Java, what problems it primarily attempts to solve, and what tools we need to install to begin Java program development. It would be a good idea to attempt the exercise on the next page to help you fix these ideas, before we formally begin learning Java language from next chapter onwards.