2.1 INTRODUCTION: WHAT IS JAVA?
Java is an easy to learn, object-oriented programming (OOP) language developed by Sun Microsystems. There are different types of programming languages, but Java is unique among them. For example, a Visual Basic program executes correctly on Microsoft Windows, but it does not run on Apple Mac OS. With Java, a developer can write an application and run that on different operating systems (including Windows, Mac OS, Unix and Mainframes) without any modification in the code.
Typically, when developing an application in a programming language, the source code is passed through a compiler (another application) that transforms the code into a set of native instructions (for target operating system). For example, when developing applications that run on Windows operating systems, they usually run on Intel processors (like Pentium chipset). The compiler converts the source code into instructions which the processor can understand and execute.
When writing Java applications, the developer does not need to directly call Windows (or other operating systems) library functions. The Java compiler does not write native instructions; instead it generates bytecodes for a virtual machine called Java Virtual Machine (JVM) .
2.2 EVOLUTION OF JAVA
1. In 1990, James Gosling was given a task of creating programs to control consumer electronics. Gosling and his team at Sun Microsystems started designing their software using C++ because of its object-oriented nature. Gosling, however, quickly found that C++ was not suitable for the projects they had in mind. They faced problems due to program bugs like memory leak, dangling pointer and multiple inheritance.
2. Gosling soon decided that he would develop his own, simplified computer language to avoid all the problems faced in C++.
3. In designing a new language, Gosling kept the basic syntax and object-oriented features of the C++ language.
4. When Gosling completed his language design project, he had a new programming language that he named 'Oak'.
5. Oak was first used in the Green project, wherein the development team attempted to design a control system for use in the home. This control system would enable the user to manipulate a list of devices, including TVs, VCRs, lights and telephones, all from a hand-held computer called *7 (star seven).
6. By the time Sun discovered that the name 'Oak'
was already claimed, they changed the name to Java.
7. In 1993, after the world wide web (www) had transformed the text-based internet into a graphics-rich environment, the Java team realized that the language they had developed would be perfect for web programming. The team came up with the concept of web applets, small programs that could be included in web pages, and even went so far as to create a complete web browser (now called Hot Java) that demonstrated the language's power.
8. In the second quarter of 1995, Sun Microsystems officially announced Java. The 'new' language was quickly embraced as a powerful tool for developing internet applications. Support for Java was added in the Netscape Navigator (Web browser on UNIX) and in the Internet Explorer.
2.3 MAIN BENEFITS OF USING JAVA
1. Java programming language is very simple and object oriented. It is easy to learn and taught in many colleges and universities.
2. Java applications run inside JVM , and now all major operating systems are able to run Java including Windows, Mac and UNIX.
3. Write once, run anywhere: a Java application runs on all Java platforms.
4. Java technologies have been improved by community involvement. It is suitable for most types of applications, especially complex systems that are used widely in network and distributed computing.
5. Java is very secure. Only Java applications that have permission can access the resources of the main computer. Thus, the main computer is protected from virus attackers and hackers.
2.4 KEY FEATURES OF JAVA
1. Java is simple : Java was developed by taking the best points from other programming languages, particularly C and C++. Java, therefore, utilizes algorithms and methodologies that are already proven. Error prone tasks, such as pointers and memory management, have either been eliminated or are handled by the Java environment automatically rather than by the programmer. Since Java is primarily a derivative of C++, which most programmers are conversant with, it has a familiar feel which makes it easy to use.
2. Java is object oriented : Even though Java has the look and feel of C++, it is a wholly independent language which has been designed to be object oriented from the ground up. In OOP, the data is treated as objects to which methods are applied. Java's basic execution unit is the class. Java is considered as a pure OOP language as no coding outside of the class definitions, including main()
, is allowed. Java contains an extensive class library available in the core programming packages. The advantages of OOP include reusability of code, extensibility and dynamic applications.
3. Java is distributed : Commonly used Internet protocols such as HTTP and FTP as well as calls for network access are built into Java. Internet programming can call on the functions through the supplied libraries and access files on the Internet, as easily as writing to a local file system. Applets can be created and distributed over the communication links, which can be run on any machine supporting JVM.
4. Java is compiled and interpreted : When Java code is compiled, the compiler outputs the Java bytecode which is executable for the JVM. The JVM does not exist physically, but it is the specification for a hypothetical processor that can run Java code. The bytecode is then run through a Java interpreter on any given platform that has the interpreter ported to it. The interpreter converts the code to the target hardware and executes it. This provides portability to any machine for which a virtual machine has been written. The two steps of compilation and interpretation allow for extensive code checking and improved security.
5. Java is robust : Java compels the programmer to be thorough. It carries out type checking at both compile and run time and makes sure that every data structure has been clearly defined and typed. Java manages memory automatically by using an automatic garbage collector. The garbage collector runs as a low priority thread in the background by keeping track of all objects and references to those objects in a Java program. When an object has no more references, the garbage collector tags it for removal and removes the object when there is an immediate need for more memory. Exception handling built-in, strong type checking (i.e. all data must be declared as explicit type), initializing local variables are the key points which make Java robust.
6. Java is secure : The Java language has built-in capabilities to ensure that violations of security do not occur. First and foremost, at compile time, pointers and memory allocation are removed thereby eliminating the tools that a system breaker could use to gain access to system resources. Memory allocation is deferred until run time. Even though the Java compiler produces only correct Java code, there is still the possibility of the code being tampered with between compilation and run time. Java guards against this by using the bytecode verifier to check the bytecode for language compliance when the code first enters the interpreter, before it even gets the chance to run.
The bytecode verifier ensures that the code does not do any of the following:
a. False pointers
b. Violate access restrictions
c. Incorrectly access classes
d. Use illegal data conversions
At run time, the security manger determines what resources a class can access such as reading and writing to the local disk. Sun Microsystems are currently working on a public-key encryption system to allow Java applications to be stored and transmitted over the Internet in a secure encrypted form.
7. Java is architecturally neutral : The term 'architectural neutral' means that Java program does not depend upon the specific architecture of the machine on which they run. The Java compiler source code to a stage which is intermediate between source and native machine code. This intermediate stage is known as the bytecode or ".class" file which is neutral. The bytecode conforms to the specification of a hypothetical machine called JVM and can be efficiently converted into native code for a particular processor. Therefore, Java does not support the concept of executable files as they are machine dependent.
8. Java is portable : The Java language follows the principle of 'write once, run anywhere' which means Java application/applet once written can be run on many platforms. By porting an interpreter for the JVM to any computer/operating system, one is assured that all code compiled for it will run on that system. This forms the basis for Java's portability. Another feature, which Java employs in order to guarantee portability, is by creating single standard for data sizes irrespective of processor or operating system platforms, i.e., all primitive types in Java are machine independent. For example, the size of the integer is 2 bytes under DOS and 4 bytes in Windows, but for Java the size of integer will remain fixed irrespective of OS, whether it is DOS, Windows, Linux or UNIX.
9. Java is high performance : The Java language supports many high-performance features such as multithreading, just-in-time compiling, and native code usage. Multithreading is the ability of an application to execute more than one task (i.e., thread) at the same time. For example, a word processor can carry out spell check in one document and print a second document at the same time. Java has employed multithreading to help overcome the performance problems suffered by interpreted code as compared to native code. Since an executing program hardly ever uses CPU cycles all the time, Java uses the idle time to perform the necessary garbage cleanup and general system maintenance that renders traditional interpreters slow in executing applications.
Since the bytecode produced by the Java compiler from the corresponding source code is very close to machine code, it can be interpreted very efficiently on any platform. In cases where even greater performance is necessary than the interpreter can provide, just-in-time compilation can be employed whereby the code is compiled at run time to native code before execution.
10. Java is dynamic : The linker of data and methods to where they are located is done at run time. New classes can be loaded while program is running. Linking is done on the fly . Even if libraries are recompiled, there is no need to recompile code that uses classes in those libraries. This differs from C++ which uses static binding. This can result in fragile classes for the cases where linked code is changed and memory pointers then point to the wrong addresses.
2.5 JAVA CHARACTER SET
A Java program is a collection of number of instructions written in a meaningful order. Further instructions are made up of keywords, variables, functions, objects, etc., which uses the Java character set defined by Java. It is a collection of various characters, digits and symbols which can be used in a Java program (Table 2.1). It comprises the followings:
2.5.1 Java Tokens
The smallest individual unit in a Java program is called a Java token. Six types of tokens are defined in Java (Figure 2.1)
1. Keywords : Keywords are those words whose meaning has already been known to the compiler. The meaning of each keyword is fixed. One can simply use the keyword for its intended meaning. The meaning of keywords cannot be changed. Also one cannot use the keywords as names for variables, function, array, etc. All keywords are written in small case letters.
There are 48 keywords in Java. They are mentioned in Table 2.2.
2. Identifier: Identifiers are names given to various program elements like variables, array, functions, structures, etc.
Rules for writing identifiers
Rule 1: The first letter must be an alphabet or an underscore.
Rule 2: From the second character onwards, any combination of digits, alphabets or underscore is allowed.
Rule 3: Only digits, alphabets, underscore are allowed. No other symbol is allowed.
Rule 4: Keywords cannot be used as identifiers.
Examples of valid and invalid identifiers (on the basis of above given rules):
3. Constants or literal: Constants in Java refer to fixed values that do not change during the execution of a program. They are also termed as literals. A name for a constant value is called a literal. A literal is what you have to type in a program to represent a value. 'A
' and '*
' are literals of type char, representing the character value A
and *
.
There are various types of constants in Java as shown in Figure 2.2.
They are classified into the following categories as given below:
(a) Integer constants : Ordinary integers, such as 17456 and - 32, are literals of type byte, short
or int
, depending on their size. One can make a literal of type long
by adding 'L'
as a suffix. For example: 17L
or 728476874368L
.
They are of three types:
(i) Decimal constants: They are sequence of digits from 0 to 9 without fractional part. It may be negative, positive or zero. For example: 12, 455, -546, 0, etc.
(ii) Octal constant: They have sequence of numbers from 0 to 7 and first digit must be 0. For example: 034, 0, 0564, 0123, etc.
(iii) Hex constant: They have sequence of digits from 0 to 9 and A to F (represents 10 to 15). They start with 0x
or 0X
. For example: 0x34, 0xab3, oX3E, etc.
(b) Real constants: They are the number with fractional part. They are also known as floating point constants. Any numerical literal that contains a decimal point or exponential is a literal of type double. To make a literal of type float
, one have to append an 'F
' or 'f
' to the end of the number. For example, '1.2F
' stands for 1.2 considered as a value of type float
. You need to know this because the rules of Java say that a value of type double
cannot be assigned to a variable of type float
. So, a error message may appear if you try to do something like "float" x=1.2;."
You have to say "float x = 1.2 F".'/
So, the advice is to stick to type double
for real numbers.
Real constant can also be represented in exponential or scientific notation which consists of two parts. For example, the number 212.345
can be represented as 2.12345e+2
where e+2
mean 10
to the power 2
. The portion before the e, i.e. 2.12345
, is known as mantissa
and +2
is the exponent
. Exponent is always an integer number which can be written in either lower or upper case. There may be many more representation of the above given number.
For example: 34.56, 0.67, 1.23f
, 2.45F
.
(c) Single character constants: They are enclosed in single quote. They consist of single character or digit. For example:'4', 'A', ' ,' etc.
(d) Boolean constants: For the type boolean
, there are precisely two literals: true and false. These literals are typed without quotes. But they represent values and not variables. Boolean values occur most often as the values of conditional expressions. For example, size > 30 is a Boolean
-valued expression that evaluates to true if the value of the variable size is greater than 30, and false if the value of size is not greater than 30. Boolean
-valued expressions are used extensively in control structures. Of course, Boolean
values can also be assigned to variables of type Boolean
.
(e) String constants: They are sequence of characters, digits or any symbol enclosed in double quote. For example: "hello", "23twenty three", "&^ABC". "2.456" etc.
(f) Backslash constants: Java defines several backslash constants which are used for special purpose. They are called so because each backslash constant starts with a backslash (). They are represented with two characters whose general form is char
but treated as a single character. They are also called escape sequence . Given below is the list of backslash (escape sequence) character constants (Table 2.3):
BCC | Meaning | ASCII |
Backspace | 08 | |
f | Form feed | 12 |
New line | 10 | |
Carriage return | 13 | |
" | Double quote | 34 |
' | Single quote | 39 |
? | Question mark | 63 |
a | Alert | 07 |
Horizontal tab | 09 | |
v | Vertical tab | 11 |