CHAPTER 1

INTRODUCTION

Welcome to the world of Java programming. In this book, we will start from the most basic concepts and try to reach a reasonably higher level. We hope you will enjoy the tour of Java language with us. We do not assume knowledge of any particular programming language. However, it is expected that many of our readers have a prior introduction to the object-oriented language C++. You will find the comparison of C++ and Java useful. Let us start our study in a more formal manner.

1.1 A Brief History of Java

Java was developed at Sun Microsystems in 1991, by a team comprising James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan as its members. The language was initially called Oak. It was later termed as Java. Java was launched on 23 May, 1995. The Java software was released as a development kit. The first two versions were named JDK 1.0 and JDK 1.1. In 1998, while releasing the next version, Sun Microsystems changed the nomenclature from Java Development Kit (JDK) to Software Development Kit (SDK). Also, it added “2” to the name. The released version of Java was called Java 2 SDK 1.2.

With every version, Java became stronger and stronger. At the time of writing of the book, Java had come up with its version 6, known as JDK 6. Java has already released the beta of its next version. In our text, we will be describing version 6.

The following table illustrates the various versions released till date:

Version Release date Major additions
JDK 1.0 January 1996 Initial release
JDK 1.1 February 1997 Inner classes, JavaBeans, JDBC, RMI
J2SE 1.2 December 1998 Swing and Collections Framework
J2SE 1.3 May 2000 HotSpot JVM, RMI, JavaSound
J2SE 1.4 February 2002 Regular expressions, Java Web Start
J2SE 5.0 September 2004 Generics, autoboxing, enumeration
Java SE 6 December 2006 Database manager and many new facilities
Java SE 7 To be released

1.2 Why Is Java Popular for the Internet?

The main reason why Java is popular on the Internet is that Java offers applets. Applets are tiny programs which run inside the browser. Before the introduction of applets, the web pages were mostly static. After the use of this application, they have become dynamic, making browsing richer. When we will study chapter on applets (Chapter 20), you will get the proof of this statement.

Applets are very safe. They cannot write to the local hard disk. Hence, there is no threat of viruses while surfing the Internet. This has made applets (and thereby Java) highly popular on the Internet.

However, this is only one aspect. Because applets are Java programs, they inherit strengths of Java. They also support graphical user interface (GUI). Hence, surfing the Internet can be quite enjoying when web pages use applets. Please note that applets are only one aspect of Java.

Java is a complete programming language. We can write applications in different walks of life. As we move along the text, we will be discussing them thoroughly.

1.3 Features of Java

The features of Java, which make Java a powerful, popular language, can be stated briefly as

  • Simple
  • Portable
  • Robust
  • Architecture neutral
  • Distributed
  • Secure
  • Object oriented
  • Multi-threaded
  • Interpreted

Let us discuss these features one by one.

1.3.1 Simple

As compared to C++, Java is simpler because of many reasons. The most important thing is the absence of pointers. Many unnecessary features of C++, like overloading of operators, are removed from Java.

1.3.2 Secure

Java language becomes secure because of the following properties/components:

  • No pointers
  • Bytecode verifier
  • Class loader
  • Security manager

1.3.3 Portable

The Java code is highly portable. Write Once Run Anywhere (WORA) is the basic Java slogan. Every platform will always have its own Java runtime machine. What you need is the Java bytecode. You are free to write the Java source code on any platform and compile to bytecode there. The bytecode so generated is platform independent. It will run on any platform.

1.3.4 Object oriented

Java is object oriented. During the last many years, every new language introduced is object oriented. We will be discussing the advantages of “object-oriented” programming language in Chapter 2.

1.3.5 Robust

Java is a robust language. It does not crash the computer, even when it encounters minor mistakes in a program. It has the ability to withstand the threats. This is considered as a great advantage for a programming language.

1.3.6 Multi-threaded

With the introduction of high-speed microprocessors a few years ago, users wanted to perform many tasks at a time. This is possible on a single-chip processor only by the use of multi-threading. Multi-threading means ability of a program to run multiple (more than one) pieces of program code simultaneously. This is possible by time slicing in a single processor system. Every thread is assigned a small time slice to run. It creates a feeling that all the tasks are running simultaneously in time.

1.3.7 Interpreted

Java is an interpreted language. When we write a program, it is compiled into a class file. The interpreter executes this class file. However, the interpreters 30 years ago were interpreting the statement in textual form. It was a very slow process. Java interprets bytecode; hence, it is considerably fast. Actually, Java gets all the advantages of interpretation, without suffering from major disadvantages.

1.3.8 Architecture neutral

The Java programming language is dependent neither on any particular microprocessor family, nor on any particular architecture. Any standard computer or even a microcomputer can run Java programs. The mobile phones also have software based on Java.

1.3.9 Distributed

Java has many in-built faculties which make it easy to use language in distributed systems.

1.4 How Java Programs Run

Earlier we had said that Java is an interpreted language. Some of us may be familiar with languages C and C++, which are not interpreted languages. Let us try to understand the difference.

While developing a program in non-interpreted languages (like C/C++), the following steps occur. A program is written in higher level language (HLL). It is called the source code, typically a .c or .cpp file.

Figure 1.1 Compilation in Other Languages

Figure 1.1 Compilation in Other Languages

Next, this program is compiled completely, resulting in an executable file, as shown in Figure 1.1. It typically has an .exe extension. This file contains a machine language program (sometimes called machine code). This file can be executed on machine (computer) with the help of an operating system.

To understand how Java programs run, we must understand two new terms. These are bytecode and virtual machine (VM).

  • Bytecode: This code is actually platform independent. We can consider it as a machine code for a hypothetical computer. When you compile an anyName.java file successfully, you get this code. It is in the form of anyName.class file.
  • Virtual machine: The name virtual suggests that this is not an actual machine. It is just a program which simulates a machine on which Java programs are supposed to run. Actually, you can suppose that VM is a hardware which runs programs in bytecode (as its own machine language).
Figure 1.2 Compilation in Java

Figure 1.2 Compilation in Java

Now we can understand how Java programs are executed. First, a program is written in Java. It is called the source code. The file has .java extension. Next, this program is compiled by a Java compiler. It produces a bytecode. The file extension is .class. This process is shown in Figure 1.2.

Figure 1.3 Running a Java Program

Figure 1.3 Running a Java Program

The computer does not directly execute this bytecode. Instead, a program known as Java Virtual Machine (JVM) runs on a computer. We pass this bytecode as an input to this program. This program interprets and executes this bytecode. This fact is explained in Figure 1.3.

1.5 Advantages and Disadvantages of an Interpreted Language

In the early days of computing, the compilation of interpreted languages was quite different. The source code was executed line by line by a program called interpreter. As the interpreter had to interpret (understand) and then execute every line, the speed of execution was very poor.

Although Java is an interpreted language, it uses a slightly different mechanism. A program is written as a source code, typically a .java file. When it is compiled, it results in another file that has .class extension. This file contains a code known as the bytecode. It is a code for a hypothetical machine. This file is executed by another program called interpreter. Since the interpreter interprets the bytecode (and not the text lines), the speed of execution is much higher as compared to the old interpreted languages.

Still today, Java programs run at lesser speed as compared to other language programs which are completely compiled.

Third-party softwares have already started appearing which compile and produce executable code from bytecode. Because of this and other developments that will occur in future, Java programs are expected to run as fast as any other language programs.

However, the major advantage of Java being an interpreted language is that it is machine (hardware platform) independent. If there is any computer in the world of any specific type (make/hardware) and if a JVM exists for that machine, then the Java bytecode runs on that machine in exactly the same way as being run on any other machine. In today’s world of the Internet, where heterogeneous collection of hardware and software platforms exists, this machine independence is considered as a great advantage.

1.6 Java Applets and Applications

We can use Java for writing two types of programs, namely applets and applications. Applets are executed in (Java-enabled) browsers. Applet is like a small Java program. It can be dynamically downloaded from the Internet.

Applications are programs similar to the ones that we find in any other languages. Since they do not need a browser, they are sometimes referred to as stand-alone applications.

The only difference between these two is that applet cannot write to local disk whereas an application can.

1.7 Advantages of Java Technology

Out of the available technologies, Java promises many advantages, which are described in a famous book Java Tutorial under the heading “How Will Java Technology Change My Life?” They are reproduced here for the readers benefit.

How Will Java Technology Change My Life?

  • Get started quickly: Although the Java programming language is a powerful object-oriented language, it’s easy to learn, especially for programmers already familiar with C or C++.
  • Write less code: Comparisons of program metrics (class counts, method counts, and so on) suggest that a program written in the Java programming language can be four times smaller than the same program in C++.
  • Write better code: The Java programming language encourages good coding practices, and its garbage collection helps you avoid memory leaks. Its object orientation, its Java Beans component architecture, and its wide-ranging, easily extendible API let you reuse other people’s tested code and introduce fewer bugs.
  • Develop programs faster: Your development time may be as much as twice as fast versus writing the same program in C++. Why? You write fewer lines of code and it is a simpler programming language than C++.
  • Avoid platform dependencies with 100% Pure Java: You can keep your program portable by following the purity tips mentioned throughout this tutorial and avoiding the use of libraries written in other languages.
  • Write once, run anywhere: Because 100% Pure Java programs are compiled into machine-independent bytecodes, they run consistently on any Java platform.
  • Distribute software more easily: You can upgrade applets easily from a central server. Applets take advantage of the feature of allowing new classes to be loaded “on the fly,” without recompiling the entire program.

1.8 Java Platform

The Java platform consists of the following.

  • A language for writing programs
  • A set of APIs
  • Class libraries
  • Other programs used in developing, compiling, and error-checking
  • A VM which loads and executes the class files

The Java language is the one which we are going to study in detail in this book. Language consists of syntax (rules) and semantics. Syntax means rules of grammar. Semantics means the description of what will happen when statements (instructions) that follow syntax rules are executed.

API as defined by Java is “the specification of how a programmer writing an application accesses the behaviour and state of classes and objects”. This is rather a terse definition. While writing Java programs we need many classes which are designed and developed by Java language creators. Programs simply would not run without them. The library of such classes is nothing but API.

To develop Java programs, we need a compiler, and other programs and tools. They are also part and parcel of the Java platform.

A program named JVM is needed to execute the Java programs. This is the most important component of the platform.

1.9 Java Development Kit

The JDK is a development environment for building applications, applets, and components using the Java programming language. In more technical terms, the current version is called Java™ Platform, Standard Edition 6 JDK. For simplicity, let us refer it as JDK 6. It is possible that you have heard the term SDK. You might wonder what is the difference between JDK and SDK. Actually, they mean the same thing. During the development of Java, different terms were more popular at different times.

The JDK 6 includes tools useful for developing and testing programs written in Java. They help us running them on the Java platform. These tools are designed to be used from the command line. They do not provide a GUI. The only exception is the “applet viewer”. Let us describe the contents of JDK 6 in detail.

  1. Development tools: Tools and utilities that will help one to develop, execute, debug, and document programs written in the Java programming language are provided in the “bin” sub-directory. These tools are the foundation of the JDK 6. The basic tools are described in the following table.
    Tool Description
    javac The compiler for the Java programming language
    java The launcher for Java applications. In this release, a single launcher is used for both development and deployment
    javadoc API documentation generator
    appletviewer Run and debug applets without a web browser
    jar Manage Java Archive (JAR) files
    jdb The Java Debugger
    javah C header and stub generator. Used to write native methods
    javap Class file disassembler
    extcheck Utility to detect JAR conflicts
  2. Runtime environment: An implementation of the Java 2 runtime environment for use by the SDK. The runtime environment includes a JVM, class libraries, and other files that support the execution of programs written in the Java programming language. These are present in the “jre” sub-directory.
  3. Additional libraries: Additional class libraries and support files required by the development tools are present in the “lib” sub-directory.
  4. Demo applets and applications: Examples, with source code, (of programming for the Java platform) are present in the “demo” sub-directory. These include examples that use Swing and other Java Foundation Classes, and the Java Platform Debugger Architecture.
  5. JDK documentation: The documentation is available online. It contains API specifications, feature description, developer guides, reference pages for SDK tools and utilities, demos, and links to related information. We can download this documentation and install on our machine.

1.10 How to Run Simple Java Programs

Java is a machine-independent language. Java programs can be run on any system. However, Windows operating system is quite popular. Hence, let us assume that we are working on any Pentium class machine with Windows XP as the operating system.

A good number of integrated development environments (IDEs) are available for writing and running Java programs. They provide lot of useful features to the users. However, most of them are complex. First-time users many a times become uncomfortable using them. Hence, experts feel that beginners should run Java from basic technique, that is, from “command prompt”. Only at the later stage, they should start using an IDE.

In this section, we will describe how to run our Java programs from command prompt.

1.10.1 How to organize our computer for command prompt operation

Before we start using Java, we have to organize our computer (directory structure) for it. We must first install Java in our computer. We can use any available hard drive (C: or D: drive) for this purpose. We have to install it in a directory. Shakespeare said, “What is in the name”. But it will be a good idea to name this directory “JDK 6”. In my computer, I have this directory in the C: drive. (When you install the kit using default directory, it is normally loaded in “program files” directory. Since this name contains a blank character, some experts feel that it should be avoided.) For keeping programs, we should have a separate directory. In my computer, I have a directory named Jprog for this purpose.

Also, we should have one plain text editor in our Java directory. Normally we have a program Notepad in our machine. We should copy it in the Java directory.

1.10.2 How to run a simple program in command prompt environment

The first step in running any program is to write (code) it. To write a program, we have to use a text editor. If we are using Windows, we may select Notepad. We are free to use any available “Text Editor”. Using any of these text editors, we must create a file. This file should have the extension .java. Let us say we have created a file first.java in Jprog directory.

The second step is to compile this file. We must execute the command javac as follows:

C:Jprog> javac first.java

If there are any errors in our program, they will be displayed on the screen. We have to note them and correct our file accordingly. If the compilation is successful, a file first.class will be created. Once compilation is successful, we should be ready for the next step.

In the third step, we can run our program by executing the command java as

C:Jprog> java first

Please note that while compiling we have to give full file name (including extension .java). While running we have to use only the class name (without the extension .class).

The tips for working from command prompt environment are given in Box 1.1.

1.10.3 How to run programs in integrated development environments

Earlier we had seen that to develop a program first, we have to edit the code using an editor. Next, we have to run the compiler. If there are any errors, we have to edit the code again. Then, we have to run “JVM”. To facilitate the handling of all these tasks, special programs are written. Since they provide us an environment which integrates various activities, they are called IDEs.

Like other programming languages, there are many programs (IDEs) available from different vendors. The most common are

  • Jbuilder from Borland
  • Sun ONE studio
  • Eclipse (available from www.eclipse.org)

BOX 1.1 Tips For Working In Command Prompt Environment

Please note that for convenience we have to keep all our programs in a particular directory. It is a nice idea to give it name “Jprog”.

In Windows there is an environment variable “PATH”. We can set it by using the set command. When we issue any command (like javac), Windows searches it in the path. If our javac program is in the directory (folder) specified by the path, then we can simply invoke it (even without a fully qualified path name).

Once you enter the DOS command prompt, you should change the (working) directory to Jprog and start working.

In our computer, we have created a batch file named cmdjdk6.bat as

set PATH=%path%;C:JDK6in;C:Jprog

set classpath=.;C:Jprog;

cd C:Jprog

command

When we run this batch file, it sets the PATH for us, and also sets classpath and takes us to our normal working folder. After completing our work, we should type exit to come out of command mode.

To run programs in these environments, you have to become familiar with them. After that, they offer you facilities to edit, compile, and run programs. Their text editors are normally user-friendly. They take you to appropriate locations where the compiler has indicated errors.

Keywords

Applet, appletviewer, application, bytecode, integrated development environment (IDE), the Internet, interpreter, javac, java command, Java Virtual Machine (JVM), Jbuilder, Sun Microsystems, virus

RuleBook

Bytecode   The type of code generated by a Java compiler

Review Questions

  1. Explain “write once and run anywhere” nature of Java.
  2. Write a short note on Java Virtual Machine.
  3. Why is Java popular on the Internet?
  4. Describe the various features of Java.
  5. List at least three development tools from JDK.
  6. What are the advantages that IDE provides to a programmer?
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.44.143