Documentos de Académico
Documentos de Profesional
Documentos de Cultura
BSIT - 41
BSIT - 42
Kuvempu University
Shankaraghatta, Shimoga District, Karnataka
Algorithms
Java Programming
In
collaboration
with
II
Printed at :
III
JAVA PROGRAMMING
(BSIT - 42)
: Contributing Authors :
IV
V
a
Preface
rogramming languages, paradigms and practices do not survive very long. Methods and technologies
applied yesterday become out-of-date today. This rapid rate of change makes programming an
exciting task.
Java is a fine example for the above statements. In a decade or so, it has grown from a concept into
one of the most dominant computer language. Java has gone through many revisions. It has grown into a
technology. It has many new and innovative features. Each innovation in language design has been driven
by the need to solve a fundamental problem that the preceding languages could not solve. Java is no
exception.
When the history of computer languages is written, it will be said that B led to C, C evolved into C++
and C++ set the stage to Java. Java is a blend of best elements of its rich heritage combined with
innovative concepts required by its unique environment. Java is inherited from C and C++. Java derives
its syntax from C. Its object oriented features are influenced by C++. Development of Java technology
has its roots in the process of refinement and adaptation that has been occurring in programming languages
for the past three decades.
Whenever a programming language is designed, trade offs like ease-of-use versus power, safety
versus efficiency and rigidity versus extensibility are often made. C++ fitted the bill. However, the World
Wide Web (WWW) and the Internet drove the language evolution forward and Java arrived.
The Internet helped catapult Java to the forefront programming and Java had a profound effect
on Internet. The reason is that Java expands the universe of objects that can move about freely in
cyberspace. Java addresses serious problem of security and portability in Internet. Javas magic is the
byte code, which makes it platform independent. All these features make it a compulsory subject in
various courses of engineering.
This study material covers all aspects of Java programming language. It begins with basics including
such things as data types, control statements classes and arrays. In the latter part, we discuss Javas
VI
exception handling mechanism, packages, multithreading and interfaces.
We are highly grateful to the authors mentioned in the references. These books have been referred
widely while preparing this study material.
We are very much thankful to the authorities of Universal Education Trust, Bangalore for providing
this opportunity to prepare the study material on Java. We acknowledge the help rendered by Mrs. Vidya
and Mr. Mahesh, Technical Staff, CS and E department, JNNCE, Shimoga. Finally we thank the principal
and management of J.N.N College of Engineering, Shimoga for providing the facilities.
Suggestions regarding the improvement of this material are welcome.
Dr.S.N Jagadeesha
Sandesh B.J
VII
a
Contents
Chapter 1
THE HISTORY OF JAVA
1.0
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
1
2
2
3
4
7
7
7
8
8
Introduction .....................................................................................
What is JAVA Technology ................................................................
Applets & Application ......................................................................
The history of JAVA .........................................................................
Basic Features in JAVA....................................................................
How JAVA differ from C & C++......................................................
JAVA & Internet..............................................................................
Java Development Kit(JDK)..............................................................
Java Standard Library(JSL)...............................................................
Summary..........................................................................................
Chapter 2
FUNDAMENTALS OF OBJECT ORIENTED PROGRAMMING
10
2.0
2.1
2.2
2.3
2.4
2.5
10
10
16
16
17
17
Introduction.......................................................................................
Programming Approaches.................................................................
What is Object-Oriented Programming...............................................
Advantages of OOP..........................................................................
Applications .....................................................................................
Summary..........................................................................................
Chapter 3
OVERVIEW OF JAVA LANGUAGE
19
3.0
19
Introduction......................................................................................
VIII
3.1
3.2
3.3
3.4
3.5
20
25
27
28
29
Chapter 4
CONSTANTS, VARIABLES AND DATA TYPES
31
4.0
4.1
4.2
4.3
4.4
31
31
36
38
39
Introduction......................................................................................
Constants.........................................................................................
Variables..........................................................................................
Scope of Variables............................................................................
Type Casting....................................................................................
Chapter 5
OPERATORS AND EXPRESSIONS
41
5.0
5.1
5.2
5.3
5.4
5.5
5.6
5.7
5.8
41
42
46
47
49
50
53
53
60
Introduction......................................................................................
Arithmetic Operators.........................................................................
Assignment Operators......................................................................
Relational Operators.........................................................................
Logical Operators.............................................................................
Increment and Decrement Operator...................................................
Conditional Operator..........................................................................
Bit-wise Operator.............................................................................
Mathematical Functions.....................................................................
Chapter 6
DECISION MAKING AND BRANCHING
63
6.0
6.1
6.2
6.3
63
63
73
77
Introduction......................................................................................
Decision Making With If Statement....................................................
Switch Statement .............................................................................
The ?: Operator in JAVA..................................................................
Chapter 7
LOOPING
80
7.0
7.1
7.2
7.3
7.4
80
81
82
84
89
Introduction......................................................................................
The While Loop................................................................................
Do Statement....................................................................................
For Statement...................................................................................
Continue and Break Statements.........................................................
IX
Chapter 8
CLASSES OBJECTS AND METHODS
94
8.0
8.1
8.2
8.3
8.4
8.5
8.6
94
94
97
103
110
114
119
Introduction......................................................................................
Classes............................................................................................
Adding Variables to Class..................................................................
Introduction to Objects......................................................................
Defining Methods ............................................................................
Inheritance: Extending Classes..........................................................
Advanced class Features ..................................................................
Chapter 9
ARRAYS AND STRINGS
125
9.0
9.1
9.2
9.3
9.4
9.5
125
125
126
132
133
138
Introduction......................................................................................
One-dimensional array.......................................................................
Creating an Array.............................................................................
Primitive and reference data types.....................................................
Two dimensional array.......................................................................
Strings in Java..................................................................................
Chapter 10
MULTITHREADING
148
10.0
10.1
10.2
10.3
10.4
10.5
10.6
10.7
148
149
150
151
151
154
157
157
Introduction.....................................................................................
Thread Fundamentals.......................................................................
The Java Thread Model....................................................................
Main thread......................................................................................
Naming the threads...........................................................................
Creating threads...............................................................................
Synchronization................................................................................
Messaging........................................................................................
Chapter 11
PACKAGES & INTERFACES
159
11.0
11.1
11.2
11.3
11.4
11.5
11.6
11.7
159
160
160
161
161
162
163
164
Introduction......................................................................................
Creating Packages ...........................................................................
Procedure to create our own package................................................
Accessing a Package........................................................................
Using a Package..............................................................................
Access Protection.............................................................................
Adding a class to a Package..............................................................
Interfaces.........................................................................................
X
Chapter 12
EXCEPTION HANDLING
169
12.0
12.1
12.2
12.3
12.4
12.5
169
170
171
172
173
178
Introduction......................................................................................
Exception .........................................................................................
Exception class hierarchy...................................................................
Uncaught Exceptions.........................................................................
Exception Methods............................................................................
Summary..........................................................................................
Chapter 13
MANAGING INPUT /OUTPUT
179
13.0
13.1
13.2
13.3
13.4
13.5
13.6
179
180
182
182
184
186
187
Introduction.......................................................................................
Concept of Streams...........................................................................
Stream Classes.................................................................................
Reading Characters...........................................................................
Reading Strings.................................................................................
Writing Console Output......................................................................
Summary..........................................................................................