bsit 42 contents

12
INFORMATION TECHNOLOGY PROGRAMMES Bachelor of Science in Information Technology - B.Sc. (IT) Master of Science in Information Technology - M.Sc. (IT) In collaboration with KUVEMPU UNIVERSITY B.Sc.(IT) - 4 th Semester BSIT - 42 Java Programming Directorate of Distance Education Kuvempu University Shankaraghatta, Shimoga District, Karnataka Universal Education Trust Bangalore

Upload: warrior432

Post on 11-Apr-2015

118 views

Category:

Documents


0 download

DESCRIPTION

contents of bsit 4th sem book

TRANSCRIPT

Page 1: BSIT 42 Contents

INFORMATION TECHNOLOGY PROGRAMMESBachelor of Science in Information Technology - B.Sc. (IT)Master of Science in Information Technology - M.Sc. (IT)

Incol laboration

wi th

KUVEMPU UNIVERSITY

B.Sc.(IT) - 4th Semester

BSIT - 42 Java Programming

Directorate of Distance EducationKuvempu University

Shankaraghatta, Shimoga District, Karnataka

Universal Education TrustBangalore

Page 2: BSIT 42 Contents

II

Titles in this Volume :BSIT - 42 Java Programming

Prepared by UNIVERSAL EDUCATION TRUST (UET)Bangalore

First Edition : May 2005Second Edition : December 2010Third Edition : May 2012

Copyright © by UNIVERSAL EDUCATION TRUST, BangaloreAll rights reserved

No Part of this Book may be reproducedin any form or by any means without the writtenpermission from Universal Education Trust, Bangalore.

All Product names and company names mentionedherein are the property of their respective owners.

NOT FOR SALEFor personal use of Kuvempu UniversityIT - Programme Students only.

Corrections & Suggestionsfor Improvement of Study materialare invited by Universal Education Trust, Bangalore.

E-mail : [email protected]

Printed at :Pragathi Print CommunicationsBangalore - 20Ph : 080-23340100

Page 3: BSIT 42 Contents

III

JAVA PROGRAMMING(BSIT - 42)

: Contributing Authors :

Dr. S.N. Jagadeesha, M.E., Ph.DProfessor and Head

Department of Computer Science & EngineeringJNN College of Engineering

Shimoga

Ravindra. S, M.Tech. (Networking & Internet Engg)

LecturerDepartment of Computer Science & Engineering

Jawaharlal Nehru National College of EngineeringShimoga

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

Department of Information Science & EngineeringPES School of Engineering

Bangalore

Page 4: BSIT 42 Contents

IV

BLANK PAGE

Page 5: BSIT 42 Contents

V

a

Preface

Programming languages, paradigms and practices do not survive very long. Methods and technologiesapplied yesterday become out-of-date today. This rapid rate of change makes programming anexciting task.

Java is a fine example for the above statements. In a decade or so, it has grown from a concept intoone of the most dominant computer language. Java has gone through many revisions. It has grown into atechnology. It has many new and innovative features. Each innovation in language design has been drivenby the need to solve a fundamental problem that the preceding languages could not solve. Java is noexception.

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 withinnovative concepts required by its unique environment. Java is inherited from C and C++. Java derivesits syntax from C. Its object oriented features are influenced by C++. Development of Java technologyhas its roots in the process of refinement and adaptation that has been occurring in programming languagesfor the past three decades.

Whenever a programming language is designed, trade offs like ease-of-use versus power, safetyversus efficiency and rigidity versus extensibility are often made. C++ fitted the bill. However, the WorldWide 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 onInternet. 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. Java’s magic is the byte code,which makes it platform independent. All these features make it a compulsory subject in various coursesof engineering.

This Revised study material covers all aspects of Java programming language with large number of

Page 6: BSIT 42 Contents

VI

illustrative examples which will help the students to enhance their skills in programming. It begins withbasics including such things as data types, control statements classes and arrays. In the latter part, wediscuss Java’s exception handling mechanism, packages, multithreading and interfaces.

We are very much thankful to the authorities of Universal Education Trust, Bangalore for providingthis opportunity to prepare the study material on Java.. Finally we thank the Principal and Management ofJ.N.N College of Engineering, Shimoga for providing the facilities.

Suggestions regarding the improvement of this material are welcome.

Dr. S.N JagadeeshaRavindra. S

B.J Sandesh

Page 7: BSIT 42 Contents

VII

a

Contents

Chapter 1

THE HISTORY OF JAVA 1

1.1 Introduction ..................................................................................... 11.2 What is JAVA Technology ................................................................ 2

1.2.1 Primary Goals of Java Technology........................................ 21.3 Applets & Application ....................................................................... 21.4 The history of JAVA ......................................................................... 31.5 Basic Features in JAVA.................................................................... 51.6 How JAVA differ from C & C++...................................................... 71.7 JAVA & Internet.............................................................................. 81.8 Java Development Kit(JDK).............................................................. 81.9 Java Standard Library(JSL)............................................................... 81.10 How will JAVA Technology change my life ?...................................... 91.11 Summary.......................................................................................... 10

Chapter 2

FUNDAMENTALS OF OBJECT ORIENTED PROGRAMMING 11

2.1 Introduction....................................................................................... 112.2 Programming Approaches................................................................. 122.3 What is Object-Oriented Programming............................................... 152.4 Advantages of OOP........................................................................... 182.5 Applications ..................................................................................... 192.5 Summary.......................................................................................... 19

Page 8: BSIT 42 Contents

VIII

Chapter 3

OVERVIEW OF JAVA LANGUAGE 21

3.1 Introduction...................................................................................... 213.2 Simple JAVA Program....................................................................... 223.3 JAVA Program Structure................................................................... 243.4 Implementing a JAVA Program.......................................................... 263.5 More of JAVA.................................................................................. 273.6 JAVA virtual machine........................................................................ 283.7 JAVA Tokens................................................................................... 293.8 Summary.......................................................................................... 31

Chapter 4

CONSTANTS, VARIABLES AND DATATYPES 32

4.1 Introduction...................................................................................... 324.2 Constants......................................................................................... 324.3 Data types........................................................................................ 354.4 Variables.......................................................................................... 384.5 Scope of Variables............................................................................ 404.6 Type Casting.................................................................................... 404.7 Summary.......................................................................................... 43

Chapter 5

OPERATORS AND EXPRESSIONS 45

5.1 Introduction...................................................................................... 455.2 Arithmetic Operators......................................................................... 46

5.2.1. Integer Arithmetic............................................................... 465.2.2. Real Arithmetic................................................................... 475.2.3 Mixed-Mode Arithmetic................................................ 485.2.4 String Concatenation.................................................... 495.2.5 Multiplication and Division in Java................................. 49

5.3 Assignment Operators...................................................................... 505.4 Relational Operators......................................................................... 515.5 Logical Operators............................................................................. 545.6 Increment and Decrement Operator................................................... 555.7 Conditional Operator.......................................................................... 57

Page 9: BSIT 42 Contents

IX

5.8 Bit-wise Operator............................................................................. 585.8.1 Shift and Logical Operators................................................. 585.8.2 Dot Operator...................................................................... 615.8.3 Type-Casting...................................................................... 615.8.4 Arithmetic Expressions....................................................... 615.8.5 Precedence of Arithmetic Operators.................................... 615.8.6 Operator Precedence and Associativity................................. 63

5.9 Mathematical Functions..................................................................... 645.10 Summary.......................................................................................... 68

Chapter 6

DECISION MAKINGAND BRANCHING 71

6.1 Introduction...................................................................................... 716.2 Decision Making With If Statement...................................................... 71

6.2.1 Simple IF Statement............................................................ 726.2.2 The IF…ELSE Statement.................................................... 746.2.3 Simple IF...ELSE Statements............................................... 766.2.4 The ELSE IF Ladder........................................................... 78

6.3 Switch Statement ............................................................................. 816.4 The ?: Operator in JAVA.................................................................... 856.5 Summary.......................................................................................... 86

Chapter 7

LOOPING 88

7.1 Introduction...................................................................................... 887.2 The While Loop................................................................................ 897.3 Do Statement.................................................................................... 907.4 For Statement................................................................................... 92

7.4.1. Additional Features of the for loop........................................ 967.4.2. Nested for loops.................................................................. 97

7.5 Continue and Break Statements in JAVA............................................ 1007.6 Summary.......................................................................................... 103

Chapter 8

CLASSES OBJECTS AND METHODS 105

Page 10: BSIT 42 Contents

X

8.1 Introduction...................................................................................... 1058.2 Classes............................................................................................. 105

8.2.1 Variables and Methods........................................................ 1068.3 Adding Variables to Class.................................................................... 1098.4 Introduction to Objects........................................................................ 1168.5 Defining Methods .............................................................................. 1228.6 Inheritance: Extending Classes.......................................................... 1278.7 Advanced class Features .................................................................. 1328.8 Summary.......................................................................................... 137

Chapter 9

ARRAYS AND STRINGS 139

9.1 Introduction...................................................................................... 1399.2 One-dimensional array....................................................................... 1399.3 Creating an Array............................................................................... 140

9.3.1. Declaration of Array............................................................ 1419.3.2 Initialization of Array............................................................ 141

9.4 Primitive and reference data types...................................................... 1469.5 Two dimensional array........................................................................ 1479.6 Strings in Java................................................................................... 151

9.6.1. Constructors....................................................................... 1529.6.2. String Methods................................................................... 154

9.7 Summary.......................................................................................... 161

Chapter 10

MULTITHREADING 163

10.1 Introduction....................................................................................... 16310.2 Thread Fundamentals......................................................................... 164

10.2.1. Advantages of Multithreading over Multitasking process........ 16510.3 The Java Thread Model..................................................................... 165

10.3.1. Thread States: Life Cycle of a Thread.................................. 16610.3.2. Context Switching............................................................... 167

10.4 Main thread....................................................................................... 16710.5 Naming the threads........................................................................... 167

10.5.1 THE getName METHOD................................................... 16810.5.2 THE setName METHOD................................................... 168

Page 11: BSIT 42 Contents

XI

10.6 Creating threads............................................................................... 15410.6.1 Extending the Thread Class.................................................. 16910.6.2 Implementing the Run() Method........................................... 17010.6.3 Starting New Thread........................................................... 17010.6.4 Implementing The ‘Runnable Interface’................................ 171

10.7 Synchronization................................................................................ 17310.8 Messaging........................................................................................ 17410.9 Summary.......................................................................................... 174

Chapter 11

PACKAGES & INTERFACES 175

11.1 Introduction...................................................................................... 17511.2 Creating Packages ........................................................................... 17611.3 Procedure to create our own package................................................ 17611.4 Accessing a Package........................................................................ 17711.5 Using a Package.............................................................................. 17711.6 Access Protection............................................................................. 17811.7 Adding a class to a Package.............................................................. 17911.8 Interfaces......................................................................................... 18011.9 Summary.......................................................................................... 184

Chapter 12

EXCEPTION HANDLING 185

12.1 Introduction...................................................................................... 18512.2 Exception ......................................................................................... 18612.3 Exception class hierarchy................................................................... 18712.4 Uncaught Exceptions......................................................................... 18812.5 Exception Methods............................................................................ 189

12.5.1 Throw................................................................................ 18912.5.2 Catch ................................................................................ 18912.5.3 Try..................................................................................... 18912.5.4 Finally................................................................................. 19012.5.5 Using Try In Exception Handling.......................................... 19012.5.6 Using Catch In Exception Handling...................................... 19112.5.7 Using Finally In Exception Handling...................................... 191

12.6 Summary.......................................................................................... 194

Page 12: BSIT 42 Contents

XII

Chapter 13

MANAGING INPUT /OUTPUT 196

13.1 Introduction....................................................................................... 19613.2 Concept of Streams........................................................................... 19713.3 Stream Classes................................................................................. 19913.4 Reading Characters........................................................................... 19913.5 Reading Strings................................................................................. 20113.6 Writing Console Output...................................................................... 20413.7 Summary.......................................................................................... 205

References....................................................................................... 206