JDS Book
JDS Book
Ihe most striking feature of the language is that it is a platform-neutral language. Java is the .
programming language that is not tied to any particular hardware or operating system. Programs developed
in Java can be executed anywhere on any system. We can callJava as a revolutionary technology because
it has brought in a fundamental shift inhow we develop and use programs. Nothing like this has happened
to the software industry before.
of systems worldwide. We can download a Java applet from a remote computer onto our local system via
Intemet and execute it locally. This makes the Internet an extension of the user's basic system providing
practically unlimited number of accessible applets and applications.
Java ensures portability in two ways. First, Java compiler generates bytecode instructions that
can be implemented on any machine. Secondly, the size of the primitive data types are machine
independent.
Lobject-Oriented
Java is atrue object-oriented language. Almost everything in Java is an object. All program code and data
Teside within objects and classes. Java comes with an extensive set of classes, arranged in packages, that
we can use in our program_ by inheritance. The object model in Java is simple andeasy to extend.
Robust and Secure
Java is a robust language. It provides many safeguards to ensure reliable code. It has strict compile
time and run time checking for data types It is designed as a garbage-collected language relieving the
programmers virtually all memory management problems. Java also incorporates the concept of exception
handling which captures serious errors and eliminates any risk of crashing the system.
Security becomes an important issue for a language that is used for programming on Internet.Threat
of viruses and abuse of resources are everwhere. Java systems not only verify all memory aCceSs but
also ensure that no viruses are communicated with aD applet/The absence of pointers in Java ensures that
programs cannot gain access to memory locations without proper authorization.
Distributed
Java is designed as a distributed language for creating applications on networks. It has the ability to share
Doth data and programs. Java applications can open and access remote objects on Internet as easily as
they can do in a local system. This enables multiple programmers at multiple remote locations to collaborate
and work together on a single project.
3.1 INTRODUCTION
Lava is a general-purpose, object-oriented programming language. We can develop two types of Java
programs:
1, Standalone applications
2. Web applets
They are implemented as shown in Fig. 3.1.(Standalone applications are programs writen in Java to
cary out certain tasks on a standalone local compute In fact, Java can be used to develop programs
Java
Source
Code
Java
Compiler
Applet Application
Type Type
Output Output
Standalone programs can read and write files and perform certain operations that applets cannot do. An
application.
C+ + .");
System. out .println ("Java is better than
salient features
Program 3.1 is perhaps the simplest of all Java programs. Nevertheless, it brings out some
understand the unigue features that
of the language. Let us therefore discuss the program line by line and
constitute a Java program.
Class Declaration
The first Iine
class SampleOne
declares a class, which is an object-oriented construct As stated earlier, Java is atrue object-oriented
language and therefore, everything must be placed Tnside a classclass is a keyword and declares that
anew class/definition follows. SampleOne is a Java identiier that specifies the name of the class to be
defined.
25
Overview of Java Language
OpeningBrace
class definition in Java matching closing brace "}.
Every inthelast line in thebegina
appeaing
wit h an
example, Thia opening
s similerbreca "" class
to C++ and enda with a(Note
Construct. that a class definition
ends with a sermicolon)
inCt+
The Main Ling
Thethirdline
nublic static void maln (8Ling aqa )
asfines a method named main Concoptually, this is similar to the main) function in C/C++. Every Java
application program must include the maln( ) method, This Is the starting point for the interpreter to begin
execution of the program. A Java application can have
the any number of classes but only one of them
mustinclude a main method to initiate the execution. (Note that Java applets will not use the main method
aall)
This line contains anumber of keywords, public, static and void.
Public: The keyword public is an access specifier that declares the ma1n
method as unprotected and therefore making ít accessible to all
other classes. This is s imilar. to the C++ public modifier.
Static: Next appears the keyword static, which declares this method as one
that belongs to the entire class and not a part of any objectsS OL
the class The main must always be declared as static since the
interpreter uses this method before any objects are created. More
about static methods and variables will be discussed later
in Chapter 8.
Void: The type modifier void states that the main method does not return
any value (but simply prints some text to the screen.)
All parameters to a method are declared inside a pair of parentheses. Here, String args[] declares a
parameter named args, which contains an array of objects of the class type String. 2
3.4 AN
APPLICATION WITH TWO CLASSES
Both the examples discussed above use
will generally require multiple onlv one class that contains the main
classes. Program 3.3 illustrates a Java method. Area-ite appicatron
application with Wo classes.
Program 3.3 Aprogram with multiple classes
class Roam
float lenath:
loat breadth;
oid getdata (float a float b)
length = a;
breadth = b;
Documentation Section
The documentation section comprises a set of comment lines giving the nane of the program, the author
and other details, which the programmer would like to refer to at a later stage Comments must explain why
28 Programming with Java
and what of classes and how of algorithms. This would greatly help in maintaining the program.
to the twO styles of comments discussed earlier, Java also uses a third style of comment /** In
documentation comment, This form of comment is used for generating documentation automaticall.
addlir.
Documentation Section Suggested
Package Statement
a package
The first statement allowed in a Java file is a package statement. This statement declares
name and informs the compiler that the classes defined here belong to this package. Example:
package student;
The package statement is optional That is, our classes do not have to be part of package. More abou
packages will be discussed in Chapter 11.
Import Statements
The next thËng after apackage statement (but before apy class definitions) may be a number of import
statemeFts.This is similar to the #include statement in CExample:
import student.test;
This statement instructs the interpreter to load the test class contained in the package student/ Using
import statements, we can have access to classes that are part of other named packages. More on import
statements in Chapter 11.
(o) this sjatint nport al nput
Interface Statements
buput metti
3 An interface is like a class but includes a group of method declarations. This is also an optional
section and
feature in the program. Interface is a new
is used only when we wish to implement the multiple inheritance
10.
concept in Java and is discussed in detail in Chapter
3 Class Definitions
elements of a
A Java program may contain multiple class definitions. Classes are the primary and essential
Java program These classes are used to map the objects of real-world problems. The number of classes
used depends on the complexity of the problem.
Overview of Java Language 29
Tokens
Declaration statement
Variables
Section
Declaration statement
Tokens
Method 1
Expressions and
Statements
Tokens
Method 2 Methods
Section
The Unicode is a 16-bit character coding system and currently supports more than 34,000
characters derived from 24 languages from America, Europe, Middle East, Africa and Asia (includingdefinen
However, most of us use only the basic ASCIl characters, which include letters, digits and punch
marks, used in nomal English. We, therefore, have used only ASCIl character set (a subset of UNICo
character set) in developing the programs in this book.
4 Keywords
Keywords are an essential part of a langquage definition, They implement specitic teatures of the languan
Java language has reserved 50 words as kevwords. Table 3.1 lists these keywords. These keywor
combined with operators and separators according to a syntax, form definition of the Java language
Understanding the meanings of al these words is important for Java programmers.
Since keywords have specific meaning in Java, we cannot use them as names for variables, classe.
methods, and so on All keywords are to be written in lower-case letters. Since Java is case-sensitive. ona
can use these wOTOS as identifiers by changing one or more letters to upper cáse. However, it is a ba4
Note: VWe should also not attempt to use the boolean values true and false of null as names in our programs.
ldentifier must be meaningful, short enough to be quickly and easily typed and long enough to be
descriptive and easily read. Java developers have followed some naming conventions.