Está en la página 1de 10

KUVEMPU UNIVERSITY

INFORMATION TECHNOLOGY PROGRAMMES


Bachelor of Science in Information Technology - B.Sc. (IT)
Master of Science in Information Technology - M.Sc. (IT)

B.Sc.(IT) - 4th Semester

BSIT - 41
BSIT - 42

Directorate of Distrance Education

Kuvempu University
Shankaraghatta, Shimoga District, Karnataka

Algorithms
Java Programming

In
collaboration
with

Universal Education Trust


Bangalore

II

Titles in this Volume :


BSIT - 41 Algorithms
BSIT - 42 Java Programming

Prepared by UNIVERSAL EDUCATION TRUST (UET)


Bangalore

First Edition : May 2005

Copyright by UNIVERSAL EDUCATION TRUST, Bangalore


All rights reserved

No Part of this Book may be reproduced


in any form or by any means without the written
permission from Universal Education Trust, Bangalore.

All Product names and company names mentioned


herein are the property of their respective owners.

NOT FOR SALE


For personal use of Kuvempu University
IT - Programme Students only.

Corrections & Suggestions


for Improvement of Study material
are invited by Universal Education Trust, Bangalore.
E-mail : info@uetb.org

Printed at :

Mayur Print Ads


Bangalore - 20
Ph : 94480 - 42724

III

JAVA PROGRAMMING
(BSIT - 42)

: Contributing Authors :

Dr. S.N. Jagadeesha, M.E., Ph.D


Professor and Head
Department of Information Science & Engineering
J N N College of Engineering
Shimoga

B.J. Sandesh, M.Tech., (CS&E)


Lecturer
Department of Information Science & Engineering
J N N College of Engineering
Shimoga

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

Simple JAVA Program.......................................................................


JAVA Program Structure...................................................................
JAVA Tokens...................................................................................
Implementing a Java Program............................................................
Java Virtual Machine........................................................................

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..........................................................................................

También podría gustarte