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.
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 | — |
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.
The features of Java, which make Java a powerful, popular language, can be stated briefly as
Let us discuss these features one by one.
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.
Java language becomes secure because of the following properties/components:
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.
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.
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.
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.
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.
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.
Java has many in-built faculties which make it easy to use language in distributed systems.
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
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).
anyName.java
file successfully, you get this code. It is in the form of anyName.class
file.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
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.
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.
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.
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.
The Java platform consists of the following.
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.
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.
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.
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.
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.
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
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.
Applet, appletviewer
, application, bytecode, integrated development environment (IDE), the Internet, interpreter, javac, java
command, Java Virtual Machine (JVM), Jbuilder, Sun Microsystems, virus
Bytecode The type of code generated by a Java compiler
18.216.44.143