Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java Notes PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 75

Programming with Java

CHAPTER 1
INTRODUCTION

 Introduction
 One characteristic that is constant in software today is “change”.
 New tools and new approaches are announced almost every day.
 To build today’s complex software it is just not enough to put together a sequence of programming
statements and sets of procedures and modules.
 Object-Oriented Programming (OOP) is an approach to program organization and development, which
attempts to eliminate some of pitfalls of conventional programming methods by incorporating the best of
structured programming features with several new concepts.
 It is a new way of organizing and developing programs and has nothing to do with and particular language.
 Languages that support OOP features include C++, Java, Smalltalk.

 Object-Oriented Paradigm
 The major objective of object-oriented approach is to eliminate some of the flaws encountered in procedural
approach.
 OOP treats data as critical element in program development and does not allow it to flow freely around
system.
 Object-oriented programming is an approach that provides a way of modularizing programs by creating
partitioned memory area for both data and functions that can be used as templates for creating copies of
such modules on demand.
 Some of the features of object-oriented paradigm are:
o Emphasis is on data rather than procedure
o Program are divided into what are known as objects
o Data structures are designed such that they characterize the objects.
o Methods that operate on data of an object are tied together in data structure.
o Data is hidden and cannot be accesses by external functions.
o Objects may communicate with each other through methods.
o New data and methods can be easily added whenever necessary.
o Follows bottom-up approach in program design.’

 Basic concepts of object-oriented programming


 The general concepts of OOP which form the heart of Java language are as follows:
 Objects and Classes
 Objects are basic runtime entities in an object-oriented system.
 They may represent a person, place, bank account, table or any item.
 Program objects should be chosen such that they match closely with the real world objects.
 When program is executed, the objects interact by sending messages to one another.
 Each object contains data and code to manipulate data.
 Objects can interact without having to know the details of each others data or code.
 The entire set od data and code of an object can be made user-defined data type using the concept of
class.
 Once a class is been defined, any number of objects can be created.
 A class is a collection of objects of similar type.

 Data Abstraction and Encapsulation


 The wrapping up of data and methods into a single unit is known as encapsulation.

Department of CSE By: Tejashree K. Gotkhindi Page 1


Programming with Java

 The data is not accessible to the outside world and only those methods, which are wrapped in class can
access it.
 The insulation of data from direct access by the program is called data hiding.
 Abstraction refers to the act of representing essential features without including the background details
or explanations.

 Inheritance
 Inheritance is the process by which objects of one class acquire the properties of another class.
 This provides the idea of reusability.
 This means that we can add additional features to an existing class without modifying it.
 This is possible by deriving a new class from the existing one.
 The new class will have the combined features of both the classes

 Polymorphism
 This means the ability to take more than one form.
 For example, an operation may exhibit different behavior in different instance.
 The behavior depends upon the types of data used in operation.
 The following figure depicts this idea:

 Dynamic binding
 Binding refers to linking of procedure call to code to be executed in response to the call.
 Dynamic binding means that the code associated with given procedure call is not known until the time
of call at runtime.

 Message Communication
 An object-oriented program consists of set of objects that communicate with each other.
 The process of programming in an object-oriented language involves following steps:
1. Creating classes that define objects and their behavior.
2. Creating objects from that class definition
3. Establishing communication among objects.
 Objects communicate with one another by sending and receiving information by calling the methods.

 Benefits of OOP
 The principle advantages are:
1. Through inheritance, redundant code is reduced and extends the use of existing class.
2. The principle of data hiding helps the programmer to build secure programs that cannot be invaded by
code in other parts of program.
3. It is possible to have multiple objects to coexist without any interference.
4. It is easy to partition the work in project based on objects.
5. Software complexity can be easily managed.

Department of CSE By: Tejashree K. Gotkhindi Page 2


Programming with Java

 Applications of OOP
The promising areas for application of OOP include:
1. Real-time systems
2. Simulation and modeling
3. Object-orients databases
4. Hypertext, hypermedia and expertext
5. Al and expert systems
6. Neural networks and parallel programming
7. Decision support and office automation systems.
8. CIM/CAD/CAD systems.
3
 Java History
 Java is general-purpose, object oriented programming language developed by Sun Microsystems of USA in
1991
 Originally called ‘Oak’ by James Gosling, one of the inventors of language.
 The goal was to make language simple, portable and reliable.
 The Java team included Patrick Naughton discovered that existing language like C and C++ had limitations.
 They modeled their new language Java on C and C++ but removed number of features of C and C++ that
were considered as source of problems and thus made Java really simple, reliable, portable and powerful
language.
 The most striking feature of language is that it is platform-neutral language.
 Java is first language that is not tied to any particular hardware or operating system.

 Java Features
 The inventors of Java wanted the language to be not reliable, portable and distributed but also simple,
compact and interactive.
 Sun Microsystems officially describes Java with the following attributes:
o Compiled and Interpreted
 Usually computer language is either compiled or interpreted.
 Java combines both these approaches thus making Java 2-stage system.
 First, Java compiler translates source code into what is known as bytecode instructions
 Bytecodes are not machine instructions and therefore, in second stage, Java interpreter generates
machine code that can be directly executed by machine that is running Java program.

o Platform-Independent and Portable


 The most significant contribution of Java over other languages is its portability.
 Java programs can be easily moved from one computer system to another, anywhere and anytime.
 Changes and upgrades in operating systems, processors and system resources will not force any
changes in Java programs.
 Java ensures portability in two ways: First, Java compiler generates bytecode instructions that can
be implemented on any machine.
 Secondly size of primitive data types are machine independent.

o Object-Oriented
 Java is true object-oriented language; almost everything in Java is an object.
 All program code and data reside within objects and classes.
 Java comes with extensive set of classes, arranged in packages that can be used in programs by
inheritance.

Department of CSE By: Tejashree K. Gotkhindi Page 3


Programming with Java

o Robust and Secure


 Java is robust language; it has strict compile time & run time checking for data types.
 It is defined as garbage-collected language relieving programmer virtually from all memory
management problems.
 Security becomes an important issue for language that is used for programming on Internet.
 Java ensures that no viruses are communicated with applet.

o Distributed
 Java is designed as distributed language for creating applications on internet.
 It has the ability to share both data and programs.
 Java applications can open and access remote objects on internet as easily as they can do in local
systems.

o Simple, Small and Familiar


 Java is small and simple language.
 Many features of C and C++ that are either redundant or sources of unreliable code are not part of
Java.
 For example: pointers, header files, goto statements etc.
 Familiarity is another striking feature of Java.
 To make language look familiar it uses many constructs of C and C++.

o Multithreaded and Interactive


 Multithreaded means handling multiple tasks simultaneously.
 Java supports multithread programs.
 This feature improves interactive performance of graphical applications.

o High Performance
 Java programming is impressive for an interpreted language, due to the use of intermediate
bytecode.
 Java architecture is also designed to reduce overheads during runtime.
 The incorporation of multithreading enhances overall execution speed of Java programs.

o Dynamic and Extensible


 Java is dynamic language and is capable of dynamically linking in new class libraries.
 Java programs support functions written in other language such as C and C++.
 These functions are known as native methods; they are linked dynamically at run time.

 How Java differs from C and C++


 Although Java was modelled after C & C++ languages, it differs from C and C++ in many ways.
 Few major differences between C/C++ and Java language are as follows:
 Java and C
 Java is lot like C but major difference is that Java is object-oriented language.
 Java term did not include some of the C features.
 It does not include C unique statements keywords: sizeof and typedef.
 Java does not contain data type struct and union.
 It does not define type modifier keywords: auto, extern, register, signed, unsigned.
 It does not support an explicit pointer type.
 It does not have preprocessors, therefore we cannot use #define and #include.
 Java adds new operators such as instanceof, and >>>
 It adds labelled break and continue statements.
 It adds many features required for object-oriented programming.

Department of CSE By: Tejashree K. Gotkhindi Page 4


Programming with Java

 Java and C++


 Java is true object-oriented language while C++ is basically C with object-oriented extension
 Java does not support Operator Overloading.
 Java does not have template classes as in C++.
 Java does not support multiple inheritance of classes. This is accomplished using new feature called
“interface”.
 Java does not support global variables.
 Java does not use Pointers.
 Java has replaced destructor function with finalize ( ).
 No header files in Java.

 C++ is superset of C, Java is neither superset nor subset of C and C++.


 Java may be considered as first cousin of C++ and second cousin of C as
shown in below figure:

 Java and World Wide Web


 World Wide Web is open-ended information retrieval system designed to be used in Internet’s distributed
environment. This contains web pages.
 Web pages contain HTML tags that enable us to find, retrieve, manipulate & display documents worldwide.
 Java was meant to be used in distributed environment such as Internet.
 Since, both Web and Java share same philosophy, Java could be easily incorporated into web.
 With support of Web, we can run Java program on other system using Internet.
 Java communicates with Web page through special tab called < APPLET >.
 The following figure illustrate this process:

Bytecode Applet Source Code

HTML
Document Bytecode
Java Web Browser

Request Applet Tag

HTML Document

Output Web Server

User
 The figure shows the following communication steps:
1. The user sends request for HTML document to remote computer’s Web Server. The Web Server is
program that accepts request, process request and sends required document.
2. HTML document is returned to user’s browser that contains APPLET tag, which identifies applet.
3. The corresponding applet bytecode is transferred to user’s computer.
4. The Java – enabled browser on user’s computer interprets bytecode and provides output.
5. The user may further interact with applet but with no further downloading from providers web server.

Department of CSE By: Tejashree K. Gotkhindi Page 5


Programming with Java

 Java Environment
 Java environment includes large number of development tools & hundreds of classes & methods
 The development tools are part of system known as Java Development Kit and classes and methods are part
of the Java Standard Library ( JSL ), also known as Application Programming Interface ( API ).
 Java Development Kit
 The Java Development Kit comes with collection of tools that are used for developing and running Java
programs. They includes:
o appletviewer --- Enables to run Java applets.
o javac --- Java compiler, which translates Java sourcecode to bytecode.
o java--- Java interpreter, which runs the applet.
o javap --- Java disassembler, enables to convert bytecode files into programdescription.
o javah --- Produces header files for use with native mathods.
o javadoc --- creates HTML format documentation from Java source code files.
o jdb --- Java debugger, which helps us to find errors in our programs.
 The way these tools are applied to build & run application programs is illustrated in below figure:
Test Editor

Java Source Code javadoc HTML


files
javac javac

Java Class File javah Header Files

java jdb

Java Program
Output

 To create Java program, we need to create source code file using text editor.
 The source code is compiled using Java compiler ( javac ) and executed using Java interpreter ( java ).
 The java debugger jdb is used to find errors if any.
 A compiled Java program can be converted into source code with help of Java disassembler javap.

 Application Programming Interface


 The Java Standard Library ( or API ) includes hundreds of classes and methods grouped into
several functional package.
 Most commonly used packages are:
o Language Support Package: Collection of classes and methods required for implementing basic
features of Java.
o Utilities Package: Collection of classes to provide utility functions such as date/time functions.
o Input/Output Package: Collection of classes required for input/output manipulation.
o Networking Package: Collection of classes for communicating with other computers via Internet.
o AWT Package: the Abstract Window Tool kit package contains classes that implements platform
independent graphical user interface.
o Applet Package: Includes set of classes that allows us to create Java applets.

Department of CSE By: Tejashree K. Gotkhindi Page 6


Programming with Java

 Java Runtime Environment:


 The JRE facilitates the execution of programs developed in Java
 It primarily comprises of following:
o Java Virtual Machine ( JVM ): It is program that interprets intermediate Java bytecode and
generates desire output.
o Runtime Class Libraries: These are set of core class libraries that are required for execution of
program.
o User Interface toolkits: AWT and Swing are examples of toolkits that support varied input
methods for users to interact with application program.
o Development Technologies: JRE comprises following key development technologies:
 Java plug-in: Enables execution of Java applet on browser.
 Java Web Start: Enables remote-deployment of application.

 Simple Java Program


 A simple java program which prints line of text as output is as follows:
classSampleOne
{
publicststic void main ( String args [] )
{
System.out.println(“ Java is better than C++ “);
}
}
 Class Declaration:
 The first line “ classSampleOne “ declares class which is an object-oriented construct.
 Hence everything should be placed in this class.
 ‘class’ is keyword and declares new class definition, ‘SampleOne’ is Java identifier that specifies class
name.
 Braces:
 Every class definition in Java begins with opening brace “{“ & ends with closing brace “}“.
 The Main Line:
 The third line “ public static void main ( String args [] ) “ defines method named main.
 This is the starting point for interpreter to begin execution of program.
 The line contains keywords like:
o public: it is access specifier that makes the method accessible to all the classes.
o static: declares this method as one that belongs to entire class and not part of object. It will be called
before objects are created.
o void: states that main method does not return any value.
 String args[] declares parameter named args, which contains array of objects of type String.
 Output Line:
 The only executable statement in program is:
System.out.println(“ Java is better than C++ ”);
 This is similar to ptintf() in C and cout<< in C++.
 The println() method is member of out object, which is a static data member of System class.
 The method println appends newline character at the end of string.
 Every Java statement must end with semicolon.

Department of CSE By: Tejashree K. Gotkhindi Page 7


Programming with Java

 An Application with Two Classes


 A real life application will generally require multiple classes.
 The below program illustrates Java application with 2 classes.
class Room
{
float length, breadth;
voidgetdata( float a, float b )
{
lenght = a;
breadth = b;
}
}
classRoomArea
{
public static void main ( String args [ ] )
{
float area;
Room room1 = new Room ( );
room1.getdata( );

area = room1.length * room1.breadth;


System.out.println(“ Area:= “ + area );
}
}

 Java Program Structure


 A Java program may contain one or more section as shown below:
Documentation Section Suggested
Optional
Package Statement
Import Statements Optional
Interface Statements Optional
Class Definitions Optional
Main Method Class
{ Essential
Main Method Definition
}

 Documentation Section:
 This section comprises set of comment lines giving name of the program, author and other details, which
programmer would like to refer at latter stage.
 Comments must explain why and what of classes and how of algorithms.

 Package Statement:
 The first statement allowed in Java file is package statement.
 This statement declares package name and informs compiler that classes defined here belong to this
package.
 Example: package student;

Department of CSE By: Tejashree K. Gotkhindi Page 8


Programming with Java

 Import Statements:
 The next statement will be number of import statements. It is similar to #include statement.
 Example: import student.text;
 This statement instructs interpreter to load test class contained in package student.

 Interface Statements:
 An interface is like class but includes group of method declarations.
 This is also an optional section and is used only when we wish to implement multiple inheritance
features.

 Class Definitions:
 Java program may contain multiple class definitions.
 Classes are primary and essential element of Java program.
 The number of classes used depends on complexity of problem.

 Main Method Class:


 Main method as its starting point, this is essential part.
 The main method creates objects of various classes and establishes communications between them.

 Java Tokens
 A Java program is basically collection of classes.
 Class is defined by set of declaration statements and methods containing executable statements
 Most statements contain expressions, which describe the actions carried out on data.
 Smallest individual units in program are known as tokens.
 There are 5 types of tokens:
o Reserved keywords
o Identifiers
o Literals
o Operators
o Seperators
 Java Character Set
 Smallest units of Java language are characters used to write Java tokens.
 These characters are defined by Unicode character set.
 The Unicode is 16-bit character coding system.
 However we use only basic ASCII characters, which include letters, digits and punctuation marks.

 Keywords
 Keywords are an essential part of a language.
 They implement specific features of the language.
 Java has reserved 50 words as keywords.
 Below table list few keywords:
abstract case continue byte
for import static final
new public if switch
do class while this

 Since keywords have specific meaning in Java, we cannot use them as names for variables, classes, methods
and so on.
 All keywords are written in lower-case letters

Department of CSE By: Tejashree K. Gotkhindi Page 9


Programming with Java

 Identifiers
 Identifiers are programmer-designed tokens.
 They are used for naming classes, methods, variables, objects, labels, packages and interfaces in a program.
 Java identifiers follow the following rules:
o They can have alphabets, digits and underscore and dollar sign characters.
o They must not begin with numbers.
o Uppercase and lowercase characters are distinct.
o They can be of any length.
 Identifiers should be short and meaningful.

 Literals
 Literals in Java are a sequence of characters ( digits, letters and other characters) that represent constant
values to be stored in variables.
 Java language specifies 5 types of literals. They are:
o Integer Literals
o Floating-point Literals
o Character Literals
o String Literals
o Boolean Literals

 Operators
 An operator is symbol that takes one or more arguments and operates on them to produce result.

 Separators
 Separators are symbols used to indicate where groups of code are divided and arranged.
 They basically define function of our code.
 Below table contains list of separators and their function:

() specifies method definition and invocation


{} used to initialize array variables
[] used to declare array types
; used to separate statements
, used to separate variables declaration
. used to separate package names and to access class variables

 Java Statements
 The statements in Java are like sentences in natural languages.
 A statement is an executable combination of tokes ending with a semicolon ( ; ).
 Statements are executed in sequence in the order in which they appear.
 It is possible to control flow of execution using special statement

Department of CSE By: Tejashree K. Gotkhindi Page 10


Programming with Java

 Below figure shows the type of statements:

Java Statements
Synchronization
Expression Statement
Statement Guarding
Labelled Statement
Statement Control
Statement

Selection Statement Iteration Statement Jump Statements

if switch
while for break continue
if-else do return

 Implement a Java program


 Implementation of a Java program involves a series of steps. They include:
o Creating the program
o Compiling the program
o Running the program

 Creating the program:


 We can create a program using any text editor. Consider the following program:
class Test
{
public static void main( String args[] )
{
System.out.println(“ Hello!”);
System.out.println(“ Welcome to the world of Java.”);
}
}
 We must save this program in a file called test.java ensuring that the file name contains class name
properly which has main().

 Compiling the program:


 To compile the program we must run the Java Compluerjavac, with the name ofsource file on
command line as shown below:
javac Test.java
 If everything is OK, javac complier creates file called Test.class containing bytecode of program.
Compiler automatically names the bytecode file as “<classname>.class “.

 Running the program:


 We need to use the Java interpreter to run a stand-alone program.
 At the command prompt, type
Java Test

Department of CSE By: Tejashree K. Gotkhindi Page 11


Programming with Java

 Now, interpreter looks for main method in program and begins execution from there.
 When executed it will display the following:
Hello!
Welcome to the world of Java.

 Machine Neutral
 The compiler converts source code files into bytecode files.
 These are machine-independent and therefore can be run on any computer.
 Java interpreter reads bytecode files and translates them into machine code for specific machine on
which Java program is running.
 This is illustrate in below figure:

Source Code

Java Compiler

Bytecode
Source Code

Windows Interpreter ABC Interpreter Macintosh


Interpreter
Machine Code Machine Code Source Code
Machine Code

Windows Computer ABC Computer


Macintosh Computer

 Java Virtual Machine


 Java compiler produces an intermediate code known as byetcode for machine that does not exist.
 This machine is called Java Virtual Machine and it exists only inside the computer memory.
 It is a simulated computer within computer and does all major functions of a real computer.
 The below figure shows the process of compiling Java program into bytecode which is also referred
to as virtual machine code:

Java Program Java Compiler Virtual Machine


 The virtual machine code is not machine specific.
 The machine specific code is generated by acting as an intermediary between virtual machine and
real machine as shown in below figure:

Bytecode Java Interpreter Machine Code

Department of CSE By: Tejashree K. Gotkhindi Page 12


Programming with Java

 Below figure illustrates how Java works on typical computer:

Real Machine
Operating System
Java Virtual Machine
Java Object Framework ( API )
Compiler Interpreter

User Application Programs

User
 Command Line Arguments
 Command line arguments are parameters that are supplidto program at execution time.
 We can write Java program that can receive and use arguments provided in command line.
 Consider the line:
public static void main( String args[] )
 Here args contains the array of values passed from command line arguments.
 For example consider,
Java Test BASIC FORTRAN C++ JAVA
 The values stored in args are as follows:
args[ 0 ]  BASIC
args[ 1 ]  FORTRAN
args[ 2 ]  C++
args[ 3 ]  JAVA

 Programming Style
 Java is a freefrom language.
 Java system does not care where on the line we begin typing.
 Several alternative styles are possible, from which any one can be selected.
 For example,
System.out.println( “ Java is wonderful “);
Can be written as,
System.out.println
( “ Java is wonderful “);

Can also be written as,


System.
out.
Println
( “ Java is wonderful “);

 Constants
 Constants in Java refer to fixed values that do not change during the execution of program.

Department of CSE By: Tejashree K. Gotkhindi Page 13


Programming with Java

 Java supports several types of constants as shown in below figure.


JAVA CONSTANTS

Numeric Constants Character Constants

Integer Real Character String


Constants constants constants constants

 Integer Constants
 An integer constant refers to sequence of digits. There are three types of constants:
o Decimal integer: constits of any combination set of digits from 0 to 9
Example: 789, -98, 3
o Octal integer: constits of any combination of digits from 0 to 7
Example: 037, 09
o Hexadecimal integer: constits of any combination of 0 to 9 and A to F
Example: 0X2, 0X5A
 Real Constants
 The floating values assigned to any variables are nothing but real constants.
 Example: 0.890, 78.97
 Real constants may also be expressed in exponential notation. General form is as follow:
mantissa e exponent
 The mantissa is either real numbers or integer numbers, exponent is integer with + or – sign.
 Example: 0.65e4, 12e-9

 Single Character Constants


 A single character constant contains single character enclosed within a pain of single quote.
 Example: ‘k’, ‘G’

 String Constants
 A string constant is a sequence of characters enclosed between double quotes.
 The character may be alphabet, digits, special character and blank spaces.
 Example: “ Hello “, “hi 123” Constants Meaning
\b backspace
 Backslash Character Constants \f form feed
 Java supports some special backslash character constants that are \n new line
used in output methods. \t horizontal tab
 The below table shows some of them: \’ single quote
\\ back slash
 Variables
 A variable is an identifier that denotes a storage location used to store a data value.
 Variable may take different values at different times during execution of program.
 A variable name can be choosen by programmer in meaning way so as to reflect wht it represents in the
program.
 Some of the examples are:
average, height, total_height.
 Variable may consist of alphabets, digits underscore( _ ), $ with following conditions:
o They must not begin with digit
o Uppercase and lowercase characters are distinct.
o It should not be keyword
o White spaces not allowed.

Department of CSE By: Tejashree K. Gotkhindi Page 14


Programming with Java

 Data types
 Data types specify the size and type of values that can be stored. Java is rich in its data types.
 Data types in Java under various categories are shown below:

Data Types in Java

Primitive (Intrinsic) Non-Primitive (Derived)

Numeric Non-Numeric Classes Arrays

Interface
Integer Floating - Character Boolean
point

 Integer types
 It can hold numbers such as 123, -98 etc.
 The following table shows the types on integer, its size and range available in Java
Type Size Minimum value Maximum value
byte 1 byte -128 127
short 2 bytes -32,768 32,767
int 4 bytes -2,147,483,648 2,147,483,647
long 8 bytes -9,223,372,036,854,775,808 9,223,372,036,854,775,807

 Floating types
 Floating point type holds numbers containing fractional parts such as 23.87, -9.67.
 There are 2 types in floating point type:
Type Size Minimum value Maximum value
float 4 bytes 3.4e-038 1.7 e+0.38
double 8 bytes 3.4e-038 1.7e+308

 Character types
 In order to store character constants in memory, Java provides character data type called char.
 The char type assumes size of 2 bytes, but basically holds only 1 character.

 Boolean types
 This is used when we want to test particular condition during execution of program.
 It holds only 2 values either TRUE or FALSE.

 Scope of variable
 Java variables are actually classified into 3 kinds:
o Instance variable
o Class variable
o Local variable
 Instance and class variables are declared inside class.
 Instance variables are created when objects are instantiated and are associated with objects. They
take different values for each objects.

Department of CSE By: Tejashree K. Gotkhindi Page 15


Programming with Java

 Class variables are global to class and belong to entire set of objects that class creates. One memory
location is created for each class variable.
 Variables declared and used inside methods are called local variables.
 The area of program where the variable is accessible is called its scope.
 The scope of local variables is between the opening brace { and closing brace }

 Symbolic Constants
 The symbolic constants may appear repeatedly in number of places in program.
 One example of such constant is 3.142 i.e PI value, another example is total number of students let it
be 50.
 While using this we face 2 problems:
o Problem in modification of program.
o Problem in understanding the program
 Modifiability
 We may like to change the value of PI from 3.142 to 3.14159 to improve the accuracy of calculation or
the number 50 to 100.
 In both the cases the values have to change wherever it is being used, if not then program may produce
disastrous outputs.

 Understandability
 When numeric value appears in program, it is not always clear, same number can be used in different
place in program.
 For example 50 may mean number of students at one place and pass marks in another place.
 So to overcome these problems we should declare variable as final and assign a constant value it as
follows:
final int STRENGTH = 100;
final int PASS_MARKS = 50;
final float PI – 3.14159;

 Type casting
 We often encounter situations where there is a need to store a value of one typeinto variable of
another type.
 In such situations we must cast the value , its syntax is as follows:
Type variable1 = (type) variable2;
 The process of converting one data type to another is called casting.
 Example:
int m = 50;
byte n = (byte) m;
long count = (long) m;
 Casting is often necessary when method return a type different than the one we require.
 Casting into smaller may result into loss of data.
 Automatic Conversion
 It is possible to assign a value of one type to variable of different type without cast.
 Java does the conversion of assigned value automatically. This is known as automatic type
conversion.
 Automatic type conversion is possible only if destination type has enough precission to store source
value.
 Example:
byte b = 75;

Department of CSE By: Tejashree K. Gotkhindi Page 16


Programming with Java

int a = b;
 The process of assigning smaller type to larger one is known as widening or promotion, and that of
assigning larger type to smaller type is known as narrowing.
Type of Default value
 Standard Default Values variable
 In Java, every variable has a default value. byte 0
 If we don’t initialize a variable when it is first created, Java provides short 0
int 0
default value to variable.
long 0L
 The following tables specify some default values:
float 0.0f
double 0.0d
char null character
 Special Operators Boolean false
 Java supports some special operators of interest such as:
 Instanceof Operator
 The instanceof is an object reference operator and returns true if the object on left-hand side is an
instance of the class given on right-hand side.
 This operator allows us to determine whether the object belongs to particular class or not.
 Example:
Person instanceof student
Is true if object person belongs to class student; otherwise flase.

 Dot Operator
 The dot operator is used to access the instance variables and methods of class objects.
 Example:
person1.age //References to variable age
person1.salary() //Reference to the method salary()

 Mathematical Functions
 Mathematical functions such as cos,sqrt, log etc are frequently used in analysis of real-life problems.
 Java supports these basic mathfunctions through Math class defined in java.lang package.
 The functions should be used as follows:
Math.function_name(); Functions Actions
sin(x) Returns sine of angle x in radians
 The following table list the math functions cos(x) Returns cosine of angle x in radians
defined Math class: tan(x) Returns tangent of angle x in radians
pow(x,y) Returns x raised y ( xy )
exp(x) Returns e raised to x ( ex )
sqrt(x) Returns square root of x
max(a,b) Returns maximum of a and b
min(a,b) Returns minimum of a and b
log(x) Returns natural logarithm of x
 Labeled Loops ( break and continue )
 In Java, we can give a label to a block of statement. A label is any valid Java variable name.
 To give label to a loop, place it before the loop with a colon at the end.
 Example: Loop1: for( …………….. )
{
………………….
………………….
}

Department of CSE By: Tejashree K. Gotkhindi Page 17


Programming with Java

………………….
If we want to jump outside a nested loops or the continue loop that is outside the current one, then we have
to use labelled break and labelled continue stataments.
 Example:
Outer: for(int m=1; m<11; m++)
{
for(int n=1; n<11; n++)
{
System.out.print(“ “ + m*n);
if ( n==m )
continue outer;
}
}
 Here continue statement terminates inner loop when n=m and continues with next iteration of outer
loop.
 Consider another example:
loop1: for(inti=0; i<10; i++ )
{
loop2: while ( x< 100 )
{
y = i * x;
if ( y> 500 )
break loop1;
………………..
}
……………………
}
……………………..
 Here break loop1 causes the execution to break out of both the loops.

 Operators and Expressions


 Java operator can be classified into number of categories as showed below:
1. Arithmetic operators ( +, -, *, / )
2. Relational operators (<, >, =, <=, >=, != )
3. Logical operators ( &&, ||, ! )
4. Assignment operators (=, +=, -=, *=, /= )
5. Increment and Decrement operators ( ++, -- )
6. Conditional operator ( ? : )
 An arithmetic expression is a combination of variables, constants, and operator arranged as per
syntax of language.

 Decision making, Branching and Looping


 Java language possesses decision making capabilities and supports following statements:
1. if statement
a. Simple if statement
b. if . . . else statement
c. Nested if . . . else statement
d. else if ladder
2. switch statement

Department of CSE By: Tejashree K. Gotkhindi Page 18


Programming with Java

 Java language provides 3 constructs for performing loop operations, they are:
1. while construct
2. do-while construct
3. for construct

CHAPTER 2

CLASSES, OBJECTS AND METHODS

 Introduction
 Anything we wish to represent in a Java program must be encapsulated in a class that defines the state
and behavior of the basic program components known as objects.
 Classes create objects and objects use methods to communicate between them.
 Classes provide a convenient method for packing together a group of logically related data items and
functions that work on them.
 In Java, the data items are fields and functions are called methods.
 A class is essentially a description of how to make an object that contains fields and methods.

 Defining a class
 A class is a user-defined data type with a template that serves to define its properties.
 Once the class type has been defined, we create “variables” of that type using declarations that are
similar to the basic type declaration.
 In Java, these variables and termed as instances of classes, which are the actual objects.
 The basic form of a class definition is:
classclassname [ extendssuperclassname]
{
[ fields declaration; ]
[ methods declaration; ]
}
 Classname and superclassname are any valid Java identifiers.
 The keyword extends indicates that the properties of superclassname class are extended to classname
class.

 Fields declaration
 Data is encapsulated in a class by placing data fields inside the body of class definition.
 These variables are called instance variables because they are created whenever an object of a class is
instantiated. Also called as member variables.
 We can declare the instance variables exactly the same way as we declare local variables.
 Example:
class Rectangle
{
int width;
int length;
}
 These variables are only declared and therefore no storage space has been created in memory.

 Methods declaration
 A class with only data fields has no life. Objects created by such class cannot respond.
 Adding of methods is necessary for manipulating the data contained in the class.

Department of CSE By: Tejashree K. Gotkhindi Page 19


Programming with Java

 Methods are declared inside body of class but immediately after declaration of variables.
 The general form of method declaration is as follows:
typemethodname ( parameter_list )
{
Method-body;
}
 Method declaration have four basic parts:
o Name of method ( methodname )
o Type of value the method returns ( type )
o List of parameters ( parameter_list )
o Body of method.

 The type specifies the type of value the method would return. This could be simple data type such as
int or any class type, or even void i.e will not return anything.
 The parameter list is always enclosed in parentheses. This contains variable name and types of all
values we want to give to method as input, separated by commas.
 The body actually describes the operations to be performed on the data.
 Consider the below given example:
class Rectangle
{
int length, width;
void getdata(int x, int y)
{
length = x;
width = y;
}
int rectArea()
{
int area = length * width;
return area;
}
}

 Creating objects
 An object in Java is essentially a block of memory that contains space to store all instance variables.
 Creating of objects is also referred to as instantiating an object.
 Objects are created using new operator.
 The new operator creates an object of the specified class and returns reference to that object.
 Here is an example of creating an object type of Rectangle:
Rectangle rect1;
rect1 = new Rectangle ( );
 The first statement declares variable to hold object reference and second one actually assigns object
reference to the variable.

 The variable rect1 is now an object of Rectangle class. It is showed in below figure:
Action Statement Result

Declare Rectangle rect1; null rect1

Department of CSE By: Tejashree K. Gotkhindi Page 20


Programming with Java

Instantiate rect1 = new Rectangle ( ); rect1

rect1 is a reference to Rectangle


Rectangle object object
 The method Rectangle( ) is default constructor of the class.
 We can create any number of objects as follows:
Rectangle rect1 = new Rectangle ( );
Rectangle rect2 = rect1;
 Each object has its own copy of instance variable of its class.
 This means that any changes to variables of one object will not effect on other object’s variables
Observe the following figure: rect1

 Rectangle
rect2
object

 Accessing class members



 We have created objects, each containing its own set of variables, we should assign values to these
variables in order to use them in our program.
 All variables must be assigned value before they are used.
 Since we are outside the class, we cannot access instance variables and methods directly.
 To do this, we use dot operator as follows:
objectname.variablename = value;
objectname.methodname(parameter_list);
 objectname is the name of the object, variablename is the name of the instance variable.
 methodname is the method that we wish to call and parameter_list is comma separated list of “actual
values” that must match in type and number with the parameter list of methodname declared in the class.
 The instance variable of Rectangle class can be accessed and assigned as follows:
rect1.length = 15; rect1.width=10;
rect2.length = 20; rect2.width=12;
 Both objects rect1 and rect2 store different values as shown below:
rect1.length 15 rect2.length 20
rect1.width 10 rect2.width
12
 Method will be called as follows: rect1.getdata(10,15);
 Constructors
 All objects that are created must be given initial values.
 To initialize an object when it is first created, Java has special type of method called constructor.
 It enables an object to initialize itself when it is created.
 Constructor have the same name as class itself, they don’t specify any return type, not even void
 This is because they return the instance of class itself.
 Constructor is as shown below:
class Rectangle
{

Department of CSE By: Tejashree K. Gotkhindi Page 21


Programming with Java

int length, width;


Rectangle ( int x, int y)
{
length = x;
width = y;
}
int areaRect()
{
return length * width;
}
}

 Method Overloading
 In Java, it is possible to create methods that have the same name, but different parameter lists and
different definitions. This is called method overloading.
 Method overloading is used when objects are required to perform similar tasks but using different
input parameters.
 When we call method in an object, Java matches up the method name first and then the number and
type of parameters to decide which one of definitions to execute. This is polymorphism.
 To create an overloaded method, all we have to do is to provide several different method definitions in
class, all with same name, but with different parameters lists.
 The difference may be either be in the number or type of arguments.

 Here is an example of creating an overloaded method:


class Room
{
float length, breadth;
Room ( float x, float y )
{
length = x;
breadth = y;
}
Room ( float x )
{
length = breadth = x; }
int area ( )
{
return ( length * breadth );
}
 An object representing rectangular room will be created as follows:
Room room1 = new Room ( 25.0, 56.9 );
Room room2 = new Room (29.0 );

 Static Members
 A class basically contains two sections: One declares variables and other declares methods.
 These variables and methods are called instance variables and methods.
 This is because everytime the class in instantiated a new copy of each of them is created.
 They are accessed using objects with dot operator.

Department of CSE By: Tejashree K. Gotkhindi Page 22


Programming with Java

 Let us assume that we want to define a member that is common to all objects and accessed without
using a particular object.
 That is, the member belongs to class as a whole rather than the object created.
 Such members can be defined as follows:
static int count;
static int max ( int x, int y );
 The members that are declared static are called as static members.
 Since these members are associated with the class itself rather than individual objects, the static
variables and static methods are often referred to as class variables and class methods.
 Static variables are used when we want to have variable common to all instance of a class.
 Static methods can be called without using the objects.
 Java class libraries contain large number of class methods.
 For example, Math class of Java library defines many static methods to perform math operations like:
float x = Math.sqrt(25.0);
 The static members are called using class names, no objects will be created.
 Static methods have several restrictions:
o They can only call other static methods.
o They can only access static data.
o They cannot refer to this or super.
 Below program illustrate this:
class MathOperation
{

static float mul( float x, float y )


{
return x * y;
}
static float divid( float x, float y )
{
return x / y;
}
}
class MathApplication
{
public static void main ( String args [ ] )
{
float a = MathOperation.mul(4.0, 5.0);
float b = MathOperation.divid(10.0, 5.0);
System.out.println( “ A = “ + a);
System.out.println( “ B = “ + b);
}
}
 Nesting of Methods
 A method can be called by using only its name by another method of the same class. This is known as
Nesting of methods.
 Below program illustrates the nesting of methods inside a class:
class Nesting
{

Department of CSE By: Tejashree K. Gotkhindi Page 23


Programming with Java

int m, n;
Nesting ( int x, int y )
{
m = x;
n = y;
}
int largest( )
{
if ( m >= n )
return ( m );
else
return ( n );
}
void display( )
{
int large = largest ( );
System.out.println( “ largest value + “ + large );
}
}
class NestingTest
{
public static void main ( String args [ ] )
{
Nesting nest = new Nesting ( 50, 40 );
nest.display( );
}
}
 The class Nesting defines one constructor and two methods, namely larget() and display().
 The method display() calls the method largest() to determine the largest of two numbers and then
display the result.

 Inheritance: Extending Class


 Reusability is another aspect of OOP paradigm i.e reusing properties of existing one’s.
 The mechanism of deriving a new class from an old one is called inheritance.
 The old class is known as base class or super class or parent class and new one is called the sub class
or derived class or child class.
 The inheritance allows subclasses to inherit all the variables and methods of their parent classes.
 Inheritance may take different forms:
a) Single inheritance ( only one super classes )
b) Multiple inheritance ( several super classes )
c) Hierarchical inheritance ( one super classes many subclasses )
d) Multilevel inheritance ( derived from derived class )
 These forms of inheritance are shown in below figure:

A A A A B

B
B D C

Department of CSE By: Tejashree K. Gotkhindi C Page 24


C
Programming with Java

B
(a) (b) (c) (d)

 Java does not directly implement multiple inheritance, it is done using interfaces.
 Defining a Subclass
 A sub class is defined as follows:
class subclassname extends superclassname
{
variables declaration;
methods declaration;
}
 The keyword extends signifies that the properties of superclassname are extended to the subclassname.

 Subclass Constructor
 It is used to construct the instance variables of both subclass and superclass.
 The subclass constructor uses keyword super to invoke the constructor method of superclass.
 The keyword super is used subject to the following conditions:
o Super may only be used within subclass constructor method.
o The class to superclass constructor must appear as first statement in subclass constructor.
o The parameter in super call must match the order and type of instance variable declared in
superclass.
 Below program illustrates the concept of Single inheritance:
class Room
{
int length, breadth;
Room ( int x, int y )
{
length = x;
breadth = y;
}
int area ( )
{
return ( length * breadth );
}
}
class BedRoom extends Room // Inheriting Room
{
int height;
BedRoom ( int x, int y , int z )
{
super( x, y ); // Pass values to supercalss
height = z;
}
int volume ( )
{
return ( length * breadth * height );

Department of CSE By: Tejashree K. Gotkhindi Page 25


Programming with Java

}
}
class InherTest
{
public static void main ( String args [ ] )
{
BedRoom room1 = new BedRoom( 14, 12, 10 );
int area1 = room1.area( );
int volume1 = room1.volume( );
System.out.println(“ Area = “ + area1 );
System.out.println(“ Volume = “ + volume1 );
}
}

 Multilevel Inheritance
 A common requirement in object-oriented programming is the use of derived class as a super class.
 Java supports this concept and uses it extensively in building its class library.
 This concept allows us to build a chain of classes as shown in figure: A

 The class A server as base class for derived class B which in turn serveras base
class for derived class C. This chain ABC is known as Inheritance path. B
 A derived class with multilevel base classes is declared as follows:
Class A
{ C
...........
...........
}
Class B extends A
{
...........
...........
}
Class C extends B
{
...........
...........
}

 Hierarchical Inheritance
 Many programming problems can be cast into hierarchy when certain features of one level are shared
by many others below the level.
 As an example, below figure shows a hierarchical classification of accounts in a commercial bank.This
is possible because all accounts possess certain common features.
Account

Savings Fixed - Deposit Account

Department of CSE By: Tejashree K. Gotkhindi Page 26


Programming with Java

Short Long
Medium

 Overriding Methods
 There may be occasions when we want an object to respond to the same method but have different
behavior when that method is called.
 That means, we should override the method defined in superclass.
 This is possible by defining a method in the subclass that has same name, same arguments and same
return type as method in superclass.
 When the method is called the method defined in subclass is invoked and executed instead of the one
in superclass.
 Below program illustrates the concept of overriding. The method display() is overriden.
class Super
{
int x;
Super (int x)
{
this.x = x;
}
void display()
{
System.out.println(“Super x = “ + x );
}
}
class Sub extends Super
{
int y;
Sub (int x, int y)
{
super ( x );
this.x = x;
}
void display()
{
System.out.println(“Super x = “ + x );
System.out.println(“Sub y = “ + y );
}
}
class OverrideTest
{
public static void main ( String args [ ] )
{
Sub s1 = new Sub( 100, 200 );
s1.display( );
}
}

Department of CSE By: Tejashree K. Gotkhindi Page 27


Programming with Java

 Output of this program will be:


Super x = 100
Sub y = 200

 Final Variables and Methods


 All methods and variables can be overridden by default in subclasses.
 If we wish to prevent the subclasses from overriding the members of superclass, we can declare them as final
using keyword final as modifier.
 Example:
Final int size = 100;
Final void showstatus ( )
{.........}
 Making method final ensures that the functionality defined in this method will never be altered in anyway.
 Similarly, the value of final variable can never be changed.
 Final variables, behave like class variables & they do not take any space on individual object of class.

 Final Classes
 Sometimes we may like to prevent class being further subclasses for security reasons.
 A class that cannot be subclassed in called final class.
 This is achieved in Java using the keyword final as follows:
final class A { . . . . . . . }
 Any attempt to inherit these classes will cause an error and complier will not allow it.
 Declaring a class final prevents any unwanted extensions to the class.

 Finalizer Methods
 Constructor method is used to initialize an object when it is declared, this is initialization.
 Java supports a concept classed finalization, which is just opposite to initialization.
 Java run-time is an automatic garbage collection system.
 It automatically frees up the memory resources used by the objects.
 But objects may hold other non-object resources such as file descriptors or window system fonts. The
garbage collector cannot free these resources.
 In order to free these resources we must use a finalizer method.

 The finalizer method is simply finalize( ) and can be added to any class.
 Java calls that method whenever it is about to reclaim the space for that object.
 The finalize method should explicitly define the tasks to be performed.

 Abstract Methods and Classes


 We have seen that by making a method final we ensure that the method is not redefined in a subclass.
That is, the method can never be subclassed.
 Java allows to do something that is exactly opposite to this.
 That is, we can indicate that method must always be redefined in subclass, thus making overriding
compulsory.
 This is done using the modifier keyword abstract in the method definition.

 Example:
abstract class Shape

Department of CSE By: Tejashree K. Gotkhindi Page 28


Programming with Java

{
.............
abstract void draw ( );
.............
}
 When class contains one or more abstract methods, it should also be declared abstract.
 While using abstract classes, we must satisfy the following conditions:
o We cannot use abstract classes to instantiate objects directly. For example:
Shape s = new Shape ( );
is illegal because shape is an abstract class.
o The abstract methods of an abstract class must be defined in its subclass.
o We cannot declare abstract constructors or abstract static methods.

 Methods with Variable arguments ( Varargs )


 Varargs represents variable length arguments in methods, which is one of the features introduced by
J2SE 5.0.
 It makes the Java code simple and flexible.
 Varargs take the following form:
<access specifier><static> void method_name( Object . . . arguments )
{. . .. . .}
 In the above syntax, the method contains an argument called varargs in which Object is the type of an
argument, ellipsis ( . . . ) is the key to varargs and arguments is the name of variable.

 Varargs allows us to declare method with unspecified number of parameters for given argument.
 The varargs must be the final argument in argument list of method.
 Varargs is identified by type of an argument followed by ellipsis ( . . . ) & name of variable.
 Below program illustrates varargs
Class Exampleprg
{
Exampleprg ( String . . . person )
{
for( String name: person )
{
System.out.println(“ Hello “ + name );
}
}
public static void main ( String args [ ] )
{
Exampleprg(“ABC”, “CDE”, “FGH”, “IJK”);
}
}

 Output of above program is:


Hello ABC
Hello CDE
Hello FGH
Hello IJK

Department of CSE By: Tejashree K. Gotkhindi Page 29


Programming with Java

 Dynamic Method Dispatch


 Dynamic method dispatch is an important mechanism in Java that is used to implement runtime
polymorphism.
 In this mechanism, method overriding is resolved at runtime instead of compile time.
 Choice of version of overridden method to be executed in response to method call is done at
runtime.
 Below figure illustrate dynamic method dispatch
class Super
{
public void method ( )
{
System.out.println(“ Method Super “);
}
}
class Sub extends Super
{
public void method ( )
{
System.out.println(“ Method Sub “);
}
}
class dyn_dis
{
public static void main ( String args [ ] )
{
Super A = new Sub ( ); //Sub’s object reference assigned Super type reference
A.method( ); //Sub’s version of method will be called at runtime
}
}

 In above program A.method ( ) call invokes the Sub class version and not of Super class
irrespective of the fact that A is Super type variable.
 Thus in dynamic method dispatch the type of reference variable is irrelevant while choosing
particular version of overridden method for invocation; instead it soley depends on type of object being
referred by reference variable.

 Visibility Control
 It may be necessary in some situations to restrict the access to certain variables and methods from
outside the class. The visibility modifiers are also known as access modifiers.
 For example, we may not like objects of class directly alter value of variable or access method.
 We can achieve this in Java by applying visibility modifiers to instance variables and methods.
 Java provides three types of visibility modifiers: public, private and protected.
 public Access
 Any variable or method is visible to entire class in which it is defined.
 A variable or method declared as public has widest possible visibility and accessible everywhere.
 Example:
public int number;
public void Sum ( ) { . . . . . . . . . . . }

Department of CSE By: Tejashree K. Gotkhindi Page 30


Programming with Java

 friendly Access
 when no access modifier is specified, member defaults to limited version of accessibility known as “
friendly “ level of access.
 Friendly access makes fields visible only in the same package, but not in other packages.

 protected Access
 The visibility level of “protected” field lies in between public and friendly access.
 The protected modifier makes the field visible not only to all classes and subclasses in same package
but also to subclasses in other packages.

 private Access
 The private fields enjoy the highest degree of protection. They are accessible only with their own
class.
 They cannot be inherited by subclasses and therefore not accessible in subclasses.
 Method declared as private behaves like method declared as final.

 private protected Access


 A field can be declared with two keywords private and protected together like:
private protected int codeNumber;
 This modifier makes fields visible in all subclasses regardless of what package they are.
Access modifier
Access location public protected friendly private protected private
Same class Yes Yes Yes Yes Yes
Subclass in same package Yes Yes Yes Yes No
Other classes in same package Yes Yes Yes No No
Subclass in other packages Yes Yes No Yes No
Non-subclasses in other package Yes No No No No

 Strings
 Strings represent a sequence of characters.
 The easiest way to represent a sequence of character in Java is by using a character array.
 Example:
char charArray[ ] = new char[4];
charArray[0] = ‘ a ‘;
charArray[1] = ‘ T ‘;
charArray[2] = ‘ S ‘;
charArray[3] = ‘ g ‘;
 In Java, strings are class objects and implemented using two classes namely: String and StringBuffer.A
Java string is an instantiated object of the String class.
 Java strings, as compared to C strings are more reliable and predictable.
 Java string is not a character array and is not NULL terminated.
 It may be declared as follows:
String stringName;
stringName = new String (“ string “);
 It can also be written as:
String firstName = new String(“ Preetam “);
 String can be concatenated using + operator as follows:

Department of CSE By: Tejashree K. Gotkhindi Page 31


Programming with Java

String fullName = name1 + name2;

 String Arrays
 We can also create and use arrays that contain string.
 The statement
String itemArray[ ] = new String [ 3 ];
will create an itemArray of size 3 to hold three string constants.
 We can assign the strings to the itemArray element by element using three different statements or by
using for loop.

 String Methods
 The String class defines a number of methods that allow us to accomplish a variety of string
manipulation tasks.
 Below table lists some of the most commonly used methods and their tasks.

MethodCall Task performed


s2 = s1.toLowerCase; Converts string s1 to all lowercase
s2 = s1.toUpperCase; Converts string s1 to all uppercase
s2 = s1.replace ( ‘x’, ‘y’ ); Replace all appearances of x with y
s2 = s1.trim( ); Remove white spaces at beginning and end of string s1
s1.equals( s2 ); Returns ‘true’ if s1 is equal to s2
s1.equalsIgnoreCase( s2 ); Returns ‘true’ if s1=s2, ignoring case of character
s1.length(); Gives the length of s1
s1.CharAt( n ); Gives nth character of s1
s1.compareTo( s2 ); Returns negative if s1 < s2, positive if s1 > s2, zero if s1 is equal s2
s1.concat ( s2 ); Concatenates s1 and s2
s1.substring( n ); Gives substring starting from nth character
s1.substring( n , m ); Gives substring starting from nth character up to mth (excluding mth )
String.Valueof( p ) Creates a string object of the parameter p
p.toString( ); Creates string representation of the object p
s1.indexof(‘x’) Gives position of first occurrence of ‘x’ int string s1
s1.indexof( ‘x’, n ) Gives the position of ‘x’ that occurs after nth position in string s1
String.Valueof(variable) Converts parameter value to string representation

 StringBuffer Class
 StringBuffer is a peer class of String.
 While String creates strings of fixed_lenth, StringBuffer creates strings of flexible length that can be
modified in terms of both length and content.
 We can insert characters and substrings in the middle of a string, or append another string to the end.
 Below table shows some of the methods that are frequently used in string manipulation:
Method Task
s1.setCharAt( n, ‘x’ ) Modifies the nth character to x
s1.append ( s2 ) Appends the string s2 to s2 at the end
s1.insert ( n, s2 ) Inserts string s2 at the position n of the string s1
s1.setLength ( n ) Sets the length of string s1. If n < s1.length( ) s1 is truncated. If n >
s1.length zeros are added to s1

 Vectors
 Vector class is contained in java.util package.

Department of CSE By: Tejashree K. Gotkhindi Page 32


Programming with Java

 This class can be used to create a generic dynamic array known as vector that can hold objects of any
type and any number.
 The object do not have to be homogeneous.
 Arrays can be easily implemented as vectors.
 Vectors are created as follows:
Vector intVect = new Vector( );
Vector list = new Vector( 3 );
 Vector can be declared without specifying any size explicitly.
 It can accommodate an unknown number of items.
 Vector posses a number of advantages over arrays:
o It is convenient to use vectors to store objects.
o Vector can be used to store list of objects that may vary in size.
o We can add and delete objects from the list as and when required.
 A major constraint in using vectors is that we cannot directly store simple data type in a vector; we can
only store objects.
 Therefore, we need to convert simple types to objects. This can be done using the wrapper classes.
 The vector class supports number of methods that can be used to manipulate the vectors created.
 Below tables shows some of them:
Method Call Task performed
list.addElement ( item ) Adds item specified to the list at the end
list.elementAt ( 10 ) Gives the name of the 10th object
list.size( ) Gives the number of object present
list.removeElement ( item ) Removes specified item from list
list.removeElementAt ( n ) Removes item stored in nth position in list
list.removeAllElements( ) Removes all the element in the list
list.copyInto ( array ) Copies all items from list to array
list.insertElementAt ( item, n ) Inserts the item at nth position

 Below program illustrates use of array, string and vectors.


Import java.util.*;
class LanguageVector
{
public static void main ( String args [ ] )
{
Vector list = new Vector( );
int length = args.length( );
for ( int i = 0; i< length; i++ )
list.addElement(args[i]);
list.insertElementAt( “COBOL”, 2 );

int size = list.size( );


String listArray[ ] = new String[size];
list.copyInfo(listArray);
System.out.println(“List of Languages “);
for( i =0; i< size; i++)
System.out.println(listArray[i]);
}
}
 The command line input and output are:

Department of CSE By: Tejashree K. Gotkhindi Page 33


Programming with Java

> Java LanguageVector Ada BASIC C++ FORTRAN Java


List of Languages
Ada
BASIC
COBOL
C++
FORTRAN
Java
 Wrapper classes
 Primitive data types may be converted into object types by using the wrapper classes contained in the
java.lang package.
 Below table shows the simple data types and their Simple Type Wrapper Class
corresponding wrapper class types: boolean Boolean
char Character
 The wrapper classes have number of unique methods for double Double
handling primitive data types & objects float Float
 They are listed in the following tables: int Integer
long Long
 Converting Primitive Numbers to Object Number using Constructor Methods:
Constructor Calling Conversion Action
Integer IntVal = new Integer( i ); Primitive integer to Integer object
Float FloatVal = new Float( f ); Primitive float to Float object
Double DoubleVal = new Double( d ); Primitive double to Double object
Long LongVal = new Long( l ); Primitive long to Long object

 Converting Object Numbers to Primitive Number Using typeValue( ) Method


Method Calling Conversion Action
int i = IntVal.intValue( ); Object to primitive integer
float f = FloatVal.floatValue( ); Object to primitive float
long f = LongVal.longValue( ); Object to primitive long
double d = DoubleVal.doubleValue( ); Object to primitive double

 Converting Numbers to Strings Using toString( ) Method


Method Calling Conversion Action
str = Integer.toString( i ); Primitive integer to string
str = Float.toString( f ); Primitive float to string
str = Double.toString( d ); Primitive double to string
str = Long.toString( l ); Primitive long to string

 Converting String Objects to Numeric Objects using Static Method Valueof( )


Method Calling Conversion Action
DoubleVal = Double.Valueof( str ); Converts string to Double object
FloatVal = Float.Valueof( str ); Converts string to Float object
IntVal = Integer.Valueof( str ); Converts string to Integer object
LongVal = Long.Valueof( str ); Converts string to Long object

 Converting Numeric Strings to Primitive Numbers Using Parsing Methods


Method Calling Conversion Action
int i = Integer.parseInt( str ); Converts string to primitive integer
long i = Long.parseLong( str ); Converts string to primitive long

Department of CSE By: Tejashree K. Gotkhindi Page 34


Programming with Java

 Below program illustrates the wrapper class methods


import java.io.*;
class Invest
{
public static void main ( String args [ ] )
{
Float principalAmount = new Float( 0 ); //Converting number to object
Float interestRate = new Float( 0 );
int numYears = 0;

try
{
DataInputStream in = new DataInputStream( System.in );
System.out.print(“Enter Principle Amount: “);
System.out.flush( );
String principalString = in.readLine( );
principalAmount = Float.valueOf( principalString );

System.out.print(“Enter Interesr Rate: “);


System.out.flush( );
String interestString = in.readLine( );
interestRate = Float.valueOf( interestString );

System.out.print(“Enter Number of Years: “);


System.out.flush( );
String yearsString = in.readLine( );
numYears = Integer.parseInt( yearString );
}
catch( IOException e )
{
System.out.println(“ IO Error “);
System.exit(1);
}

float value = loan( principalAmount.floatValue( ), interestRate.floatValue( ), numYears );


System.out.println(“Final Value = “ + value );
}

static float load( float p, float r, int n )


{
int year =1;
float sum = p;
while( year <= n )
{
sum = sum * ( 1 + r );
year = year + 1;
}
return sum;

Department of CSE By: Tejashree K. Gotkhindi Page 35


Programming with Java

}
}

 Autoboxing and Unboxing


 The autoboxing and unboxing feature, introduced in J2SE 5.0, facilitates the process of handling
primitive data types in collections.
 We can use this feature to convert primitive data types to wrapper class types automatically.
 The compiler generates a code implicitly to convert primitive type to corresponding wrapper class type
and vice-versa.
 For example, consider following statement:
Double d_object = 98.42;
double d_primitive = d_object.doubleValue( );
 Using autoboxing and unboxing feature, we can rewrite above code as:
Double d_object = 98.42;
double d_primitive = d_object;
 How the Java compiler provides restrictions to perform the following conversions:
 Convert from null type to any primitive type.
 Convert to the null type other than the indentify conversion.
 Convert from any class type C to any array type if C is not object.

 Enumerators
 J2SE 5.0 allows us to use the enumerated type in Java using enum keyword.
 This keyword can be used similar to static final constants.
 For example, consider the following code:
Public class Days
{
public static final int DAY_SUNDAY = 0;
public static final int DAY_MONDAY = 1;
public static final int DAY_TUESDAY = 2;
public static final int DAY_WEDNESDAY = 3;
public static final int DAY_THURSDAY = 4;
public static final int DAY_FRIDAY = 5
public static final int DAY_SATURDAY = 6;
}
 Using the enumerated type feature provided by J2SE 5.0, the above code can be rewritten as:
Public enum Day { SUNDAY, MONDAY, T UESDAY, WEDNESDAY, THURSDAY, FRIDAY,
SATURDAY };
 The advantages of using the enumerated type are:
 Compile-time type safety
 We can use the enum keyword in switch statements.

 Below program illustrates the enum data type:


public class Workingdays
{
enum Days
{
SUNDAY,
MONDAY,

Department of CSE By: Tejashree K. Gotkhindi Page 36


Programming with Java

TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY
};

public static void main( String args[ ] )


{
for ( Days d: Days.values ( ) );
weekend( d );
}
private static void weekend( Days d )
{
if( d.equals( Days.Sunday ) )
System.out.println(“Value = “ + d + “ is a Holiday “);
else
System.out.println(“Value = “ + d + “ is a working day “);
}
}

 Annotations
 The annotations feature, introduced by J2SE 5.0 is also known as metadata.
 We can use this feature to merge additional Java elements with the programming elements, such as
classes, methods, parameters, local variables, packages and fields.
 Metadata is stored in Java class files by compiler and these class files are used by JVM or by program
to find the metadata for interacting with programming elements.
 Java contains the following standard annotations:
Annotation Purpose
@Deprecated Compiler warns when deprecated java elements are used in non-deprecated program
@Overrides Compiler generated error when the method uses this annotation type does not override
methods present in super-class

 In addition to the above standard annotations, Java also contains some meta-annotations available in
the java.lang.annotation package.
 The following table provides the meta-annotations:
Meta-annotation Purpose
@Documented Indicates annotation of this type to be documented by Javadoc
@Inherited Indicates that this type is automatically inherited
@Retention Indicates the extended period using annotation type.
@Target Indicates to which program element the annotation is applicable

 The declaration of annotation is similar to that of an interface.


 We use the symbol ‘@’ before keyword interface.

 For example, consider the following code that contains declaration of annotation:
Package njunit.annotation;
Import java.lang.annotation.*;
@Retention( Retentionpolicy.RUNTIME )

Department of CSE By: Tejashree K. Gotkhindi Page 37


Programming with Java

@Target((ElementType.METHOD))
Public @interface UnitTest
{
String value ( );
}
 @Retention is meta-annotation, which declares that the @UnitTest annotation must be stored in class
file.
 The @Target meta-annotation is used to declare @UnitTest annotation, which annotates the methods
in Java class files.
 The @interface meta-annotation is used to declare @UnitTest annotation with member called value,
which returns String as an object.

 While using annotations, we need to follow some guidelines:


o Do not use extends clause. It automatically extends the marker interface
java.lang.annotation.Annotation
o Do not use any parameter for a method
o Do not use generic methods
o Do not use throws clause.
 An annotation can also be applied to programming elements.
 After merging the annotation with the programming elements, we can use the methods available in the
interface, java.lang.reflect.
 Annotated Elements to query about the existence of programming element and get their values.
 The methods of the AnnotatedElement interface are:
 IsAnnotationPresent( )
 getAnnotations( )
 getAnnotation( )
 getDeclaredAnnotations( )
 The classes that implement the AnnotatedElement interface are:
 java.lang.reflect.accessibleobject
 java.lang.reflect.constructor
 java.lang.reflect.field
 java.lang.reflect.method
 java.lang.class
 java.lang.package

Department of CSE By: Tejashree K. Gotkhindi Page 38


Programming with Java

CHAPTER 3
INTERFACES: MULTIPLE INHERITANCE

 Introduction
 Java cannot have more than one superclass.
 For instance, a definition like:
class A extends B extends C
{
............
............
}
is not permitted in Java.
 Java provides an alternate approach known as interfaces to support the concept of multiple inheritance.
 Although a Java class cannot be a subclasss of more than one superclass, it can implement more than
one interface, thereby enabling us to create classes that build upon other classes without the problems
created by multiple inheritance.

 Defining Interfaces
 An interface is basically a kind of class.
 Like classes, interfaces contain methods and variables but with major difference.
 The difference is that interfaces define only abstract method and final fields.
 This means that interfaces do not specify any code to implement these methods and data fields contain
only constants.
 Therefore, it is the responsibility of the class that implements an interface to define the code for
implementation of these methods.
 The syntax for defining an interface is very similar to that for defining class.
 The general form of an interface definition is:
interface interfaceName
{
variable declaration
methods declaration
}
 Here, interface is the keyword and interfaceName is an valid Java variable.
 Variables will be declared as constants as follows:
static final type variableName = value;
 Methods declaration will contain only a list of methods without any body statements.
 Exmaple:
return_type methodName ( parameter_list );
 Here is an example of an interface definition:
interface Item
{

Department of CSE By: Tejashree K. Gotkhindi Page 39


Programming with Java

static final int code = 1001;


static final String name = “Fan”;
void display( );
}
 Note that the code for the method is not included in the interface and the method declaration simply
ends with a semicolon.
 The class that implements this interface must define the code for the method
 Below table lists the difference between class and interface:
Class Interface
the members of a class can be constant variables The members of an interface are always declared as
constant. i.e their value is final.
The class definition can contain the code for each of The methods here are abstract in nature i.e there is
its methods no code associated with the.
It can be instantiated by declaring objects. It cannot be used to declare objects. It can only be
inherited by a class
It can use various access specifiers like public, It can only use public access specifier
private, or protected.

 Extending Interfaces
 Like classes, interfaces can also be extended i.e interface can be subinterfaces from other interface.
 The new subinterface will inherit all the members of superinterface.
 This is achieved using keyword extends as shown below:
interface name2 extends name1
{
body of name2
}
 For example, we can put all constants in one interface and methods in another, as shown below:
interface ItemConstants
{
int code = 1001;
String name = “Fan”;
}
interface Item extends ItemConstants
{
void display( );
}
 The interface Item would inherit both the constant code and name into it.
 We can also combine several interfaces together into single interface.
 Sub interfaces cannot define the methods declared in the super interfaces.
 It is the responsibility of any class that implements derived interface to define all the methods.

 Implementing Interfaces
 Interfaces are used as “superclasses” whose propertied are inherited by classes.
 It is therefore necessary to create class that inherits give interface.
 This is done as follows:
class classname implements interfacename
{
Body of classname
}

Department of CSE By: Tejashree K. Gotkhindi Page 40


Programming with Java

 Here the class classname “implements” the interface interfacename.


 The more general form of implementation may look like:
class classname extends superclass implements interface1, interface2, . . . . . .
{
Body of classname
}
 This shows that class can extend another class while implementing interfaces.
 When class implements more than one interface, they are separated by comma.
 The implementation of interfaces can take various forms as illustrated in below figure:

Interface A Class A D Interface Interface A


Implementation Extension Extension

Class B Class B E
Extension Extension Implementation Implementation
B C
Class C Class C

(a) (b) (c)

Interface A Interface B

Extension

C Interface

(d) Implementation

D Class

 Implementation of interface as class type is illustrated by below given program:


interface Area
{
final static float pi = 3.14F;
float compute ( float x, float y );
}
class Rectangle implements Area
{

public float compute ( float x, float y )


{
return ( x * y );
}
}
class Circle implements Area
{
public float compute ( float x, float y )

Department of CSE By: Tejashree K. Gotkhindi Page 41


Programming with Java

{
return ( pi * x * y );
}
}

class InterfaceTest
{
public static void main ( String args[ ] )
{
Rectangle rect = new Rectangle( );
Circle cir = new Circle ( );
Area area;

area = rect;
System.out.println(“Area of Rectangle = “ + area.compute(10,20) );

area = cir;
System.out.println(“Area of Circle = “ + area.compute(10,0) );
}
}

 The Output is as follows:


Area of Rectangle = 200
Area of Circle = 314
 Any number of dissimilar classes can implement an interface.
 To implement methods, we need to refer to class objects as types of the interface rather than types of
their respective classes.

 Accessing Interface Variables


 Interfaces can be used to declare a set of constants that can be used in different classes.
 This is similar to creating header files in C++ to contain large number of constants.
 Since such interfaces do not contain methods, there is no need to worry about implementating any
methods.
 The constant values will be available to any class that implements the interface.
 The values can be used in any method, as part of any variable declaration, or anywhere we can use
final value.
 Example:
interface A
{
int m = 10;
int n = 50;
}
class B implements A
{
int x = m;

Department of CSE By: Tejashree K. Gotkhindi Page 42


Programming with Java

void method( int size )


{
..........
..........
if ( size < n )
}
}

 Below program illustrates the implementation of the concept of multiple inheritance using interfaces.
class Student
{
int rollNumber;
void getNumber ( int n )
{
rollNumber = n;
}
void putNumber( )
{
System.out.println(“ Roll No: “ + rollNumber );
}
}
class Test extends Student
{
float part1, part2;
void getMarks( float m1, float m2 )
{
part1 = m1;
part2 = m2;
}
void putMarks( )
{
System.out.println(“ Marks obtained are: “);
System.out.println(“ Part1: “ + part1);
System.out.println(“ Part2: “ + part2);
}
}
interface Sports
{
float sportWt = 6.0F;
void putWt( );
}
class Results extends Test implements Sports
{
float total;
public void putWt( )
{
System.out.printlm(“Sports Wt = “ + sportWt);
}

Department of CSE By: Tejashree K. Gotkhindi Page 43


Programming with Java

void display( )
{
total = part1 + part2 + sportWt;
putNumber( );
putMarks( );
putWt( );
System.out.println(“ Total score = “ + total );
}
}
class Hybrid
{
publicstatic void main( String args [ ] )
{
Results student1 = new Results ( );
student1.getNumbers(1234);
student1.getMarks(27.5F, 33.0F);
student1.display();
}
}
 The Output of above program:
Roll No: 1234
Marks obtained:
Part1 = 27.5
Part2 = 33
Sports Wt = 6
Total score = 66.5

Department of CSE By: Tejashree K. Gotkhindi Page 44


Programming with Java

Chapter 4
Packages: Putting Classes Together

 Introduction
 Packages are Java’s way of grouping a variety of classes and/or interfaces together.
 The grouping is usually done according to functionality.
 Packages act as “containers” for classes.
 By organizing our classes into packages we achieve the following benefits:
o The classes container in the packages of other programs can be easily reused.
o In packages, classes can be unique compared with classes in other packages. i.e two classes in two
different packages can have same name.
o Packages provide a way to “hide” classes thus preventing other programs or packages from
accessing classes that are meant for internal use only.
o Packages provide a way for separating “design” and “coding”.
 We can build our own classes for handling our data and use existing class libraries for designing user
interfaces.
 Java packages are therefore classified into two types i.e Java API packages and User Defined
packages.

 Java API Packages


 Java API provides number of classes grouped into different packages according to functionality.
 Below figure shows the functional breakdown of packages that are frequently used in program.

Java

lang applet

util net
io awt

 Below table shows the classes that belong to each package.


Package name Contents
java.lang Language support classes. These are classes that Java compiler itself uses & therefore
they are automatically imported. They include classes for primitive types, strings, math
functions, threads and exception.
java.util Language utility classes such as vectors, hash tables, random numbers, date etc.
java.io Input/Output support classes. They provide facilities for input and output of data.
java.awt Set of classes for implementing graphical user interface. They include classes for

Department of CSE By: Tejashree K. Gotkhindi Page 45


Programming with Java

windows, buttons, lists, menus and so on.


java.net Classes for networking. They include classes for communicating with local computers
as well as with internet servers.
java.applet Classes for creating and implementing applets.

 Using System Packages


 The packages are organized in a hierarchical structure as illustrated in below figure:
Java
awt Package containing
awt package
Color

Graphics
Package containing classes

Font



Image Classes containing methods

 This shows that package named java contains the package awt, which inturn contains various classes
required for implementing graphical user interface.
 There are two ways of accessing the classes stored in a package.
 The first approach is to use the fully qualified class name of class that we want to use.
 This is done by using the package name containing class and then appending class name to it using dor
operator.
 For example, if we want to refer to class Color in awt package, then it is done as follows:
java.awt.Color
 This approach is perhaps the best and easiest one if we need to access the class only once or when we
need not have to access any other classes of the package.

 Sometimes we might want to use a class in number of places in program or we may like to use many
of classes contained in package.
 We achieve this as follows:
import packagename.classname
import packagename.*
 These are known as import statements and must appear at the top of file, before any class declaration,
import is a keyword.

Department of CSE By: Tejashree K. Gotkhindi Page 46


Programming with Java

 Example: import java.awt.Color;


import java.awt.*;

 Naming Conventions
 Packages can be named using the standard Java naming rules i.e packages begin with lowercase letters.
 This makes it easy for users to distinguish package names from class names when looking at an
explicit reference to class.

 For example,
double y = java.lang.Math.sqrt(x);

package class method


name name name
 This statement uses a fully qualified class name Math to invoke the method sqrt( ).
 Note that methods begin with lowercase letters.Duplicate names will cause run-time errors.
 Every package name must be unique to make the best use of packages.

 Creating Packages
 To create our own package we must first declare the name of the package using package keyword
followed by package name.
 This must be the first line in Java source file, then we define class.
 For example,
package firstPackage;
public class FirstClass
{
...........
...........
}
 Here package name is firstPackage, the class FirstClass is now considered part of this package.
 This would be saved as file called FirstClass.java, and located in directory named firstPackage.
 When source file is compiled, Java will create .class file and store in same directory.
 Steps followed to create our package involves following steps:
1. Declare package at the beginning of file using the form:
package packagename;
2. Define the class that is to be put in package and declare it public.
3. Create a subdirectory under the directory where the main source files are stored.
4. Store the listing as classname.java file in subdirectory created.
5. Compile the file. This creates .class file in subdirectory.
 Java also supports the concept of package hierarchy.
 This is done by specifying multiple names in package statement, separated by dots.
 For example,
package firstPackage.secondPackage;

 Accessing a Package

Department of CSE By: Tejashree K. Gotkhindi Page 47


Programming with Java

 We use the import statement when there are many references to a particular package or package name
is too long and unwieldy.
 The same import statements can be used to access user-defined packages.
 The general form of import statement is as follows:
import package1 [.package2] [.package3] .classname;
 Here package1 is the name of the top level package, package2 is name of package that is inside
package1 and so on.
 We can have any number of packages in package hierarchy followed by classname.
 Multiple import statements are allowed and should end with semicolon ( ; ).
 Example:
import firstPackage.secondPackage.MyClass;
 After defining this statement, all members of class MyClass can be directly accessed using class name
or its objects directly without using package name.
 We can also write as:
import packagename.*;
 The major drawback of shortcut approach is that it is difficult to determine from which package a
particular member came.
 The advantage is that we need not have to sue long package names repeatedly in program.

 Using a Package
 The listing below shows a package named package1 containing single class ClassA.
package package1;
public class ClassA
{
public void displayA( )
{
System.out.println(“Class A”);
}
}
 This source file should be named ClassA.java and stored in subdirectory package1.
 Compile the file and resultant ClassA.class will be stored in same directory.

 Consider below listing:


import package1.ClassA;
class PackageTest1
{
public static void main ( String args [ ] )
{
ClassA objectA = new ClassA( );
objectA.displayA( );
}
}

 This listing shows simple program that imports ClassA from package1.
 The source file should be saved as PackageTest1.java and then compiled.
 The source file and compiled file would be saved in directory of which package1 was subdirectory.
Now we can run the program.

Department of CSE By: Tejashree K. Gotkhindi Page 48


Programming with Java

 During the compilation of PackageTest1.java the compiler checks for the file ClassA.class in package1
directory for information it need, but it does not actually include the code from ClassA.class in file
PackageTest1.class.
 When PackageTest1 program is rum, Java looks for file PackageTest1.class and loads it using
something called class loader.
 Now interpreter knows that it also need code in file ClassA.class and loads it as well.

 Consider another package named package2 containing single class as shown below:
package package2;
public class ClassB
{
protected int m =10;
public void display( )
{
System.out.println(“Class B”);
System.out.println(“ M = “ + m );
}
}
 We can have a sub class of the imported class from package. It is as shown below:
import package1.ClassA;
import package2.ClassB;
class classC extends ClassB
{
int n = 20;
void display( )
{
System.out.println(“Class C”);
System.out.println(“M = “ + m);
System.out.println(“N = “ + n);
}
}
class PackageTest2
{
public static void main( String args [ ] )
{
ClassA objectA = new ClassA( );
ClassC objectC = new ClassC( );

objectA.displayA( );
objectC.displayB( );
objectC.displayC( );
}
}
 When we import multiple packages it is likely that two or more packages contain classes with identical
names.
 Example:
package pack1;
public class Teacher

Department of CSE By: Tejashree K. Gotkhindi Page 49


Programming with Java

{ ............ }
public class Student
{ ............ }

package pack2;
public class Courses
{ ............ }
public class Student
{ ............ }
 We can import both the packages like this:
import pack1.*;
import pack2.*;
Student student1;
 Since both the packages contain class Student, compiler cannot understand which one to use and
therefore generates an error.
 This problem can be overcome as follows:
import pack1.*;
import pack2.*;
pack1.Student student1;
pack2.Student student2;
 The access protection detains for packages is as given in below table:
Access modifier
Access location public protected friendly private protected private
Same class Yes Yes Yes Yes Yes
Subclass in same package Yes Yes Yes Yes No
Other classes in same package Yes Yes Yes No No
Subclass in other packages Yes Yes No Yes No
Non-subclasses in other package Yes No No No No

 Adding a Class to a Package


 It is simple to add a class to an existing package.
 Consider the following package:
package p1
public class ClassA
{
//body
}
 Package p1 contains one public class by name ClassA.
 Suppose we want to add another classB to this package.
 This can be done as follows:
1. Define the class and make it public.
2. Place the package statement
package p1;
before the class definition as follows:
package p1
public class ClassB
{ //body }
3. Store this as ClassB.java file under the directory p1

Department of CSE By: Tejashree K. Gotkhindi Page 50


Programming with Java

4. Compile ClassB.java file. This will create ClassB.class file and place it in p1
 Now package p1 will contain both the class ClassA and ClassB.
 Statement like import p1.* will import both the classes.

 If we want to create package with multiple public classes in it, we follow the following steps:
1. Decide the name of the package.
2. Create a subdirectory with this name under the directory where main source files are stored.
3. Create classes that are to be placed in package in separate source files and declare package
statement
package packagename
At the top of each source file
4. Switch to the subdirectory created earlier and compile each source file. When completed, package
would contain .class files for all the source files.

 Hiding Classes
 When we import package using asterisk ( * ), all public classes are imported.
 However we may prefer to ‘not import’ certain classes i.e we may like to hide these classes from
accessing outside of packages.
 Such classes should be declared “not public”.
 Example:
package p1;
public class X
{
//body
}
class Y
{
//body
}
 Here class Y is not declared public and is hidden from outside of package p1.
 This class can be seen and used only by other classes in same package.
 Consider the following code, which imports package p1:
Import p1.*;
X objectX; //OK; class X is available
Y objectY; //Not OK; Y is not available

 Static Import
 Static import is another feature introduced with J2SE 5.0 release.
 This eliminates the need of qualifying static member with class name.
 The static import declaration is similar to that of import.
 Static import statements are used to import static members from classes and use them without
qualifying the class name.
 They syntax is as follows:
import static package_name . subpackage_name . class_name . staticmember_name;
import static package_name . subpackage_name . class_name . *
 Without using static import the static member PI from Math class was accessed as follows:
double area = Math.PI * radius * radius;

Department of CSE By: Tejashree K. Gotkhindi Page 51


Programming with Java

 The following code provides an example of importing the interface into class.
 The interface declared is as follows:
public interface Salary_increment
{
public static final double Manager = 0.5;
public static final double Clerk = 0.25;
}
 This interface is present in the sub-package employee_details of employee package.
 If we need to access the interface, we can import the interface using static import statements as
follows:
import static employee. Employee_details. Salary_increment;
Class Salary_hike
{

public static void main ( String args [ ] )


{
.............
double manager_salary = Manager * Manager_current_salary;
double clerk_salary = Clerk * Clerk_current_salary;
.............
.............
}
}
 The use of static import statements is shown in below program:
Import static java.lang.Math.*;
public class Mathop
{
Public void circle( double r )
{
double area = PI * r * r;
System.out.println(“The Area of Circle is: “ + area );
}
public static void main ( String args [ ] )
{
Mathop obj = new Mathop( );
Obj.circle(2.3);
}
}

Department of CSE By: Tejashree K. Gotkhindi Page 52


Programming with Java

CHAPTER 5
MULTITHREADED PROGRAMMING

 Introduction
 Multithreading is a conceptual programming paradigm where a program is divided into two or more
subprograms ( processes ), which can be implemented at the same time in parallel.
 For example, one sub program can display an animation on screen while another may build next
animation to be displayed.
 This is something similar to dividing task into subtasks and assigning them to different people for
execution independently and simultaneously.

 A thread is similar to program that has single flow of control. It has a beginning, a body and an end
and executes commands sequentially.
 All main programs are called single-threaded programs.
 Every program will have atleast one thread as shown in below figure:
Main Thread
.......
Main method
....... module
.......

start start start

....... ....... .......


....... ....... .......
switching switching
....... ....... .......
Thread A Thread C
Thread B

 A unique property of Java is its support for multithreading i.e Java enables us to use multiple flows of
control in developing programs.
 Each flow of control may be thought of as separate tiny program known as thread that runs in parallel
to others as shown in figure.
 A program that contains multiple flow of control is known as multithread program.

Department of CSE By: Tejashree K. Gotkhindi Page 53


Programming with Java

 The above figure illustrates a Java program with four threads, one main and three others.
 The main thread is actually the main method module, which is designed to create and start other
threads namely A, B and C.
 Once initiated by main thread, the threads A, B and C run concurrently and share the resources jointly.
 The ability of a language to support multithreads is referred to as concurrency.
 Since threads in Java are subprograms of main application program and share the same memory space,
they are known as lightweight threads or lightweight processes.
 It is important to known that ‘threads running in parallel’ does not really mean that they actually run at
same time.
 Since all threads are running on single processor, flow of execution is shared between threads.
 The Java interpreter handles the switching of control between the threads in such a way that it appears
they are running concurrently.

 Creating Threads
 Creating threads in Java is easy.
 Threads are implemented in the form of objects that contains method called run( ).
 The run( ) method is the heart and soul of any thread.
 It makes up the entire body of thread and is the only method in which the thread’s behavior can be
implemented.
 A typical run( )would appear as follows:
public void run ( )
{
................
. . . . . . . . . . . . . . . . ( statements for implementing threads )
}
 The run ( ) method should be invoked by an object of the concerned thread.
 This can be achieved by creating thread and initiating it with help of another thread method called start ( ).
 A new thread can be created in two ways:
1. By creating a thread class: Define a class that extends Thread class and override its run( ) method
with the code required by the thread.
2. By converting a class to a thread: Define a class that implements Runnable interface. The
Runnable interface has only one method run ( ), that is to be defined in method with the code to be
executed by the thread.

 Extending the Thread Class


 We can make our class runnable as thread by extending the class java.lang.Thread.
 This gives us access to all the thread methods directly. It includes the following steps:
1. Declare the class as extending the Thread class.
2. Implement the run( ) method that is responsible for executing the sequence of code that the thread
will execute.
3. Create a thread object and call the start( ) method to initiate thread execution.

 Declaring the class


 The Thread class can be extended as follows:
class MyThread extends Thread
{
................

Department of CSE By: Tejashree K. Gotkhindi Page 54


Programming with Java

................
}
 Now we have a new type of thread MyThread.
 Implementing the run( ) Method
 The run( ) method has been inherited by the class MyThread.
 We have to override this method in order to implement code to be executed by our thread.
 The basic implementation of run() will look like this:
public void run( )
{
................
................
}
 When we start the new thread, Java calls the thread’s run( ) method, so it is the run( ) where all the
action takes place.
 Starting New Thread
 To actually create and run an instance of our thread class, we write the following:
MyThread aThread = new MyThread( );
aThread.start( );
 The first line instantiates or creates a new object of class MyThread.
 The thread that will run this object is not yet running. It is just in newborn state.
 The second line calls the start( ) method causing thread to move into runnable state.
 Then, Java runtime will schedule thread to run by invoking its run( ) method.
 Now, the thread is said to be in running state.
 An Example of Using the Thread Class
 The below Program illustrates the use of Thread class for creating and running threads in an
application.
 The program creates 3 threads for three different tasks by writing below line:
new A( ).start( );
 The main thread in ThreadTest class also constitutes another thread which may call “main thread”
class A extends Thread
{
public void run( )
{
for ( int i = 1; i<=5; i++)
{
System.out.println(“From ThreadA : i = “ + i );
}
System.out.println(“ Exit from A ”);
}
}
class B extends Thread
{
public void run( )
{
for ( int j = 1; j<=5; j++)
{
System.out.println(“From ThreadB : j = “ + j );

Department of CSE By: Tejashree K. Gotkhindi Page 55


Programming with Java

}
System.out.println(“ Exit from B ”);
}
}
class C extends Thread
{
public void run( )
{
for ( int k = 1; k<=5; k++)
{
System.out.println(“From ThreadC : k = “ + k );
}
System.out.println(“ Exit from C ”);
}
}
class ThreadTest
{
public static void main( String args[ ] )
{
new A( ).start( );
new B( ).start( );
new C( ).start( );
}
}

 Stopping and Blocking a Thread


 Stopping a Thread
 Whenever we want to stop a thread from running further, we may do so by calling its stop( ) method,
like:
aThread.stop( );
 This statement causes the thread to move to the dead state.
 A thread will also move to dead state automatically when it reaches the end of its method.
 The stop( ) method may be used when the premature death of a thread is desired.

 Blocking a Thread
 A thread can also be temporarily suspended or blocked from entering into the runnable and
subsequently running state by using either of the following thread methods:
sleep ( ) suspend ( ) wait ( )
 These methods cause the thread to go into the blocked or not-runnable state.
 The thread will return to the runnable state when the specified time is elapsed in the case of sleep( ),
the resume( ) method is invoked in the case of suspend( ), and notify( ) method is called in the case of
wait( ).

 Life Cycle of a Thread


 During the life time of a thread, there are many states it can enter.
 They include:
1. Newborn state
2. Runnable state

Department of CSE By: Tejashree K. Gotkhindi Page 56


Programming with Java

3. Running state
4. Blocked state
5. Dead state

 A thread is always in one of these five states. It can move from one state to another via a varity of
ways as shown in below figure:

New Thread
Newborn
stop

Active stop Killed


thread Running Runnable thread
Dead
yield

suspend resume
sleep notify stop
wait

Idle Thread Blocked

 Newborn state
 When we create a thread object, the thread is born and is said to be in
newborn state. Newborn
 The thread is not yet scheduled for running. At this state, we can do
only one of the following things with it: start stop
o Schedule it for running using start( ) method.
o Kill it using stop( ) method.
Runnable Dead
 If scheduled, it moves to the runnable state as shown in figure: state state
 If we attempt to use any other method at this stage, an exception
will be thrown.

 Runnable state
 The runnablestate means that the thread is ready for execution and is waiting for availability of the
processor.
 That is, the thread has joined the queue of threads that are waiting for execution.
 If all threads have equal priority, then they are given time slots for execution in round robin fashion, i.e
first-come, first-serve manner.

Department of CSE By: Tejashree K. Gotkhindi Page 57


Programming with Java

 The thread that relinquishes control joins the queue at the end and again waits for its turn.
 This process of assigning time to threads is known as time-slicing.
 If we want a thread to relinquish control to another thread to equal priority before its turn comes, we
can do so by using the yield( ) method as shown in below figure:
yield

Running Runnable Threads


Thread

 Running state
 Running means that the processor has given its time to the thread for its execution.
 Tread runs until it relinquishes control on its own or it is preempted by higher priority thread
 A running thread may relinquish its control in one of the following situations:
1. It has been suspended using suspend( ) method. A suspended thread can be revieved by using the
resume( ) method. This approach is useful when we want to suspend a thread for some time due to
certain reason, but do not want to kill.
suspend

resume

Running Runnable Suspended

2. It has been made to sleep. We can put a thread to sleep for a specified time period using the method
sleep ( time ) where timeis in miliseconds. This means that the thread is out of the queue during this
time period. The thread re-enters the runnable state as soon as this time period is elapsed.
sleep( t )

after( t )

Running Runnable Suspended

3. It has been told to wait until some events occur. This is done using the wait( ) method. The thread
can be scheduled to run again using the notify( ) method.
wait

notify

Running Runnable Suspended

 Blocked state
 A thread is said to be blocked when it is prevented from entering into the runnable state and
subsequently the running state.
 This happens when the thread is suspended, sleeping, or waiting in order to satisfy certain
requirements.
 A blocked thread is considered “not runnable” but not dead and therefore fully qualified to run again.

 Dead state
 Ever thread has a life cycle.

Department of CSE By: Tejashree K. Gotkhindi Page 58


Programming with Java

 A running thread ends its life when it has completed executing its run( ) method.
 It is a natural death.
 However we can kill it by sending the stop message to it at any state thus causing a premature death to
it.
 A thread can be killed as soon it is born or while it is running or even when it is in “not runnable”
(blocked) condition.

 Using Thread Methods


 There are methods that can move a thread from one state to another.
 Below program illustrates the use of yield( ), sleep( ) and stop( ) methods.
class A extends Thread
{
public void run( )
{
for( int i = 1; i <= 5; i++ )
{
if( i == 1 ) yield( );
System.out.println(“\t From Thread A: i = “ + i );
}
System.out.println(“Exit from A”);
}
}
class B extends Thread
{
public void run( )
{
for( int j = 1; j <= 5; j++ )
{
System.out.println(“\t From Thread B: j = “ + j );
if( j == 3 ) stop( );
}
System.out.println(“Exit from B”);
}
}
class C extends Thread
{
public void run( )
{
for( int k = 1; k <= 5; k++ )
{
System.out.println(“\t From Thread C: k = “ + k );
if( k == 1 )
try
{
sleep(1000);
}
catch ( Exception e )
{ }

Department of CSE By: Tejashree K. Gotkhindi Page 59


Programming with Java

}
System.out.println(“Exit from c”);
}
}
class ThreadDemo
{
public static void main(String args[ ] )
{
A threadA = new A ( );
B threadB = new B ( );
C threadC = new C ( );

System.out.println(“Start Thread A”);


threadA.start();

System.out.println(“Start Thread B”);


threadB.start();

System.out.println(“Start Thread C”);


threadC.start();

System.out.println(“End of main Thread”);


}
}

 Thread Exceptions
 The sleep( ) method is enclosed in a try block and followed by catch block.
 This is necessary because the sleep( ) method throws an exception, which should be caught.
 If we fail to catch the exception, program will not compile.
 Java run system will throw IllagalThreadStateException whenever we attempt to invoke method that a
thread cannot handle in given state.
 For example, a sleeping thread cannot deal with resume( ) method because a sleeping thread cannot
receive any instructions.
 The same is true with suspend( ) method when it is on a blocked thread.

 Whenever we call a thread method that is likely to throw an exception, we have to supply an
appropriate exception handler to catch it.
 The catch statement may take one of the following forms:
catch ( ThreadDeath e )
{
............
. . . . . . . . . . . . // Killed thread
}
catch ( InterruptedException e )
{
............
. . . . . . . . . . . . // cannot handle it in current state
}

Department of CSE By: Tejashree K. Gotkhindi Page 60


Programming with Java

catch ( IllegalArgumentException e )
{
............
. . . . . . . . . . . . // Illegal method argument
}
catch ( Exception e )
{
............
. . . . . . . . . . . . // Any other
}

 Thread Priority
 In Java, each thread is assigned a priority, which affects the order in which it is scheduled for running.
 The threads of the same priority are given equal treatment by the Java scheduler and therefore, they
share the processor on a first-come, first-serve basis.
 Java permits us to set the priority of a thread using the setPriority( ) method as follows:
ThreadName.setPriority( intNumber );
 The intNumber is an integer value to which the thread’s priority is set.
 The Thread class defines several priority constants:
MIN_PRIORITY = 1
NORM_PRIORITY = 5
MAX_PRIORITY = 10
 The intNumber may assume one of these constants or any value between 1 and 10.
 Default setting is NORM_PRIORITY
 By assigning priorities to threads, we can ensure that they are given the attention they deserve.
 For example, we may need to answer an input as quickly as possible.
 Whenever multiple threads are ready for execution, the Java system chooses the highest priority thread
and executes it.
 For a thread of lower priority to gain control, one of the following things should happen:
1. It stops running at the end of run( )
2. It is made to sleep using sleep( )
3. It is told to wait using wait( )
 However, if another thread of higher priority comes along, the currently running thread will be
preempted by the incoming thread thus forcing the current thread to move to runnable state.
 Highest priority thread always preempts any lower priority threads.
 Below program illustrate the effect of assigning higher priority to thread.
class A extends Thread
{
public void run( )
{
System.out.println(“ ThreadA started “);
for( int i = 1; i <= 4 ; i++ )
System.out.println(“ From Thread A : i = “ +i );
System.out.println(“ Exit form A “);
}
}
class B extends Thread
{

Department of CSE By: Tejashree K. Gotkhindi Page 61


Programming with Java

public void run( )


{
System.out.println(“ ThreadB started “);
for( int j = 1; j <= 4 ; j++ )
System.out.println(“ From Thread B : j = “ +j );
System.out.println(“ Exit form B “);
}
}
class C extends Thread
{
public void run( )
{
System.out.println(“ ThreadC started “);
for( int k = 1; k <= 4 ; k++ )
System.out.println(“ From Thread C : k = “ +k );
System.out.println(“ Exit form C “);
}
}
class ThreadPriority
{
public static void main ( String args [ ] )
{
A threadA = new A ( );
B threadB = new B ( );
C threadC = new C ( );
threadC.setPriority( Thread.MAX_PRIORITY );
threadB.setPriority( threadA.getPriority( ) + 1 );
threadA.setPriority( Thread.MIN_PRIORITY );

System.out.println(“ Start thread A “ );


threadA.start( );

System.out.println(“ Start thread B “ );


threadB.start( );

System.out.println(“ Start thread C “ );


threadC.start( );

System.out.println(“ End of main thread “);


}
}

 Synchronization
 One thread may try to read a record from a file while another is still writing to the same file.
 Depending on the situation, we may get strange results.
 Java enables us to overcome this problem using a technique known as synchronization.
 In case of Java, the keyword synchronized helps to solve such problems by keeping a watch on such
locations.

Department of CSE By: Tejashree K. Gotkhindi Page 62


Programming with Java

 For example, the method that will read information from a file and the method that will update the same
file may be declared as synchronized.
 Example:
synchronized void update ( )
{
................
................ // code here is synchronized
}
 When we declare a method synchronized, Java creates a “monitor” and hands it over to the thread that
calls method first time.
 As long as the thread holds the monitor, no other thread can enter the synchronized section code.
 A monitor is like a key and the thread that holds the key can only open the lock.
 It is possible to mark a block of code as synchronized as shown below:
synchronized ( lock-object )
{
................
................ // code here is synchronized
}
 An interesting situation may occur when two or more threads are waiting to gain control of resource.
 Due to some reasons, the condition on which the waiting threads rely on to gain control does not
happen.
 This results in what is known as deadlock.
 For example, assume that the thread A must access Method1 before it can release Method2, but the
thread B cannot release Method1 until it gets hold of Method2.
 Because these are mutually exclusive conditions, a deadlock occurs.
 The code below illustrates this:
Thread A
synchronized Method2 ( )
{
synchronized Method1 ( )
{
................
................
}
}
Thread B
synchronized Method1 ( )
{
synchronized Method2 ( )
{
................
................
}
}

 Implementing the ‘Runnable’ Interface


 Thread can also be created by implementing Runnable interface.

Department of CSE By: Tejashree K. Gotkhindi Page 63


Programming with Java

 The Runnable interface declares the run ( ) method that is required for implementing threads in our
programs.
 To do this, we must perform the steps listed below:
1. Declare the class as implementing the Runnable interface.
2. Implement the run ( ) method.
3. Create a thread by defining an object that is instantiated from this “ runnable “ class as the target of
the thread.
4. Call the thread’s start( ) method to run the thread.
 Below program illustrates the implementation of the above steps:
class X implements Runnable // Step 1
{
public void run ( ) // Step 2
{
for ( int i = 1; i<= 10; i++ )
System.out.println(“ ThreadX “ + i );
System.out.println(“ End of ThreadX “);
}
}
class RunnableTest
{
public static void main ( String args[ ] )
{
X runnable = new X( );
Thread threadX = new Thread( runnable ); // Step 3
threadX.start ( ); // Step 4
System.out.println(“End of main Thread”);
}
}

 Inter-Thread Communication
 Inter-thread communication is be defined as exchange of message between two or more threads.
 The transfer of message takes place before or after the change of state of a thread
 For example, an active thread may notify to another suspended thread just before switching to the suspend
state.
 Java implements inter-thread communication with the help of following three methods:
 notify ( ): resumes the first thread that went into sleep mode. The object declaration of this method is
shown below:
final void notify ( )
 notifyall ( ): resumes all the threads that are in sleep mode. The execution of these threads happens as
per priority. The object class declaration of this method is shown below:
final void notifyall ( )
 wait ( ): sends calling thread into sleep mode. This thread can now be activated only by above 2
methods. One can also specify the time as parameter for which the thread has to wait. The object class
class declaration of this method is as shown below:
final void wait ( )
 All above methods are declared in the root class i.e Object.
 Since methods are declared as final they cannot be overridden.
 All the three methods throw InterruptedException

Department of CSE By: Tejashree K. Gotkhindi Page 64


Programming with Java

 Suspending, Resuming and Stopping Thread


 The functions of suspending, resuming and stopping a thread are performed using Boolean type flags in
multithreading program.
 These flags store the current status of thread.
 The run ( ) method works according to the current Boolean value of these three flags.
 For instance, if suspend flag is set to true then run ( ) will suspend the execution of currently running
thread.
 Similarly, if the resume flag is set to true then run ( ) will resume the execution of suspended thread.
 Thread will get terminated once the stop flag of thread is set to true.
 Below figure illustrates these flags:
class sus_res_stop implements Runnable
{
Thread th;
boolean sus_flag, stop_flag;

sus_res_stop ( String th )
{
th = new Thread ( this.tn );
sus_flag = false;
stop_flag = false;

th.start( );
}
public void run ( )
{
try
{
int j = 1;
while ( ++j < 20 )
{
synchronized ( this );
{
while ( sus_flag )
wait( );
if ( stop_flag )
break;
}
}
}
catch ( InterruptedException IE )
{
System.out.println(“ Thread interrupted “);
}
}
synchronized void my_sus( )
{
sus_flag = true;

Department of CSE By: Tejashree K. Gotkhindi Page 65


Programming with Java

}
synchronized void my_resume( )
{
sus_flag = false;
notify( );
}
synchronized void my_stop( )
{
sus_flag = false;
stop_flag = true;
notify ( );
}
}
public class eg_RSR
{
public static void main( String args [ ] )
{
try
{
sus_res_stop SRST = new sus_res_stop( “ SRS“ );

System.out.println( “ Thread SRST is created and started “);


Thread.sleep(2000);

SRST.my_sus( );
System.out.println(“ Thread SRST is suspended “);
Thread.sleep(2000);

SRST.my_resume( );
System.out.println(“ Thread SRST is resumed “);
Thread.sleep(2000);

SRST.my_sus( );
System.out.println(“ Thread SRST is suspended “);
Thread.sleep(2000);

SRST.my_resume( );
System.out.println(“ Thread SRST is resumed “);
Thread.sleep(2000);

SRST.my_stop( );
System.out.println(“ Thread SRST is stopped”);
}
catch ( InterruptedException IE )
{
System.out.println(“ Generated interrupted exception” );
}
}
}

Department of CSE By: Tejashree K. Gotkhindi Page 66


Programming with Java

CHAPTER 6
MANAGING ERRORS AND EXCEPTIONS

 Introduction
 Rarely a program runs successfully at its very first attempt.
 A mistake might lead to an error causing to program to produce unexpected results.
 Errors are the wrongs that can make program go wrong.
 An error may produce an incorrect output or may terminate execution of program abruptly.
 It is therefore important to detect and manage it properly.

 Types of Errors
 Errors may broadly be classified into two categories:
o Compile time error
o Run time error

 Compile-time Errors
 All syntax errors will be detected and displayed by the Java compiler and therefore these errors are
known as compile-time error.
 Whenever the compiler displays an error, it will not create .class file.
 Therefore it is necessary to fix all those errors in time.
 Java compiler does a nice job of telling us where the errors are in program. For example, missing semi-
colon at the end of the statement.
 Example:
System.out.printf(“Hello….”)
 For above statement will be “ ‘;’ expected “

 The most common compile-time error are:

Department of CSE By: Tejashree K. Gotkhindi Page 67


Programming with Java

o Missing semicolons
o Missing ( or mismatch of ) brackets in classes and methods.
o Misspelling of identifiers and keywords
o Missing double quotes in string
o Use of undeclared variables
o Incompatible types in assignments / initialization
o Bad references to objects
o Use of = in place of = = operator

Run-time Error
 Sometimes, program may compile successfully creating .class file but may not run properly.
 Such programs may produce wrong results due to wrong logic or may terminate due to errors such as
stack overflow.
 Most common run-time errors are:
o Dividing an integer by zero
o Accessing an element that is out of bounds of an array.
o Trying to store value into an array of incompatible class or type
o Trying to cast an instance of class to one of its subclasses
o Passing parameter that is not in valid range for a method
o Attempting to use negative size for an array
o Accessing character that is out of bounds of a string, etc..
 Exceptions
 An exception is a condition that is caused by run-time error in the program.
 When the Java interpreter encounters an error such as dividing an integer by zero, it creates an exception
object and throws.
 If the exception object is not caught and handled properly, the interpreter will display an error message as
shown below:
class error
{
public static void main(String args[])
{
int a = 10, b = 5, c = 5;
int x = a / ( b – c );
System.out.println(“ X = “ +x );

int y = a / ( b + c );
System.out.println(“Y = “ +y );
}
}

 After compiling above program we get the below error an execution will be terminated:
java . lang . ArithmeticException: / by zero at error.main(error.java:10)

 If we want the program to continue with the execution of remaining code, then it should be caught and
display an appropriate message. This task is caked exception handling.
 The purpose of exception handling mechanism is to provide a means to detect and report an “exceptional
circumstance” so that appropriate action can be taken.

 The mechanism suggests incorporation of separate error handling code that performs the following task:

Department of CSE By: Tejashree K. Gotkhindi Page 68


Programming with Java

1. Find the problem ( Hit the exception )


2. Inform that an error has occurred ( Throw the exception )
3. Receive the error information ( Catch the exception )
4. Take corrective actions ( Handle the exception )
 Some common exception are listed below in table:
Exception Type Cause of Exception
ArithmeticException Caused by math errors such as divide by zero
ArrayIndexOutOfBoundsException Caused by bad array indexes
ArrayStoreException Caused when a program tries to store the wrong type of
data in an array
FileNotFoundException Caused by an attempt to access a non-existent file
IOException Caused by general I/O failures, such as inability to read
from file
NullPointer Exception Caused by referencing null object
NumberFormatException Caused when conversion between strings and number fails
OutOfMemoryException Caused when there’s not enough memory to allocate new
object
SecurityException Caused when an applet tries to perform an action not
allowed by browser’s security setting.
StachOverFlowException Caused when the system runs out of stack space
StringIndexOutOfBoundsException Caused when program attempts to access nonexistent
character position in string

 Exceptions in Java can be categorized into two types:


 Checked exceptions: These exceptions are explicitly handled in code itself with the help of try-catch
blocks. Checked exceptions are extended from java.lang.Exception class
 Unchecked exceptions: These exceptions are not essentially handled in program code; instead JVM
handles such exceptions. Unchecked exceptions are extended from the java.lang.RuntimeException.

 Syntax of Exception handling code


 The basic concepts of exception handling are throwing an exception and catching it.
 This is illustrated in below figure:

try Block
Statement that causes
Exception object creator
an exception
Throws
exception
object

catch Block
Statement that handles
Exception handler
an exception

 Java uses keyword try to preface block of code that is likely to cause an error condition and “throw” an
exception.

Department of CSE By: Tejashree K. Gotkhindi Page 69


Programming with Java

 A catch block defined by keyword catch “catches” exception “thrown” by try block and handles it
appropriately.
 The catch block is added immediately after try block.

 The following example illustrates use of simple try and catch statements:
.............
.............
try
{
statement;
}
catch ( Exception-type e)
{
statement;
}
.............
.............

 The try block can have one or more statements that could generate exception.
 If any one statement generates an exception, the remaining statements in block are skipped and execution
jumps to catch block.
 The catch block too can have one or more statements that are necessary to process the exception.
 Every try statement should be followed by at least one catch statement.
 The catch statement is passed a single parameter, which is reference to exception object thrown.
 If catch parameters matches with type of exception object, then exception is caught and statements in catch
block will be executed.

 Below program illustrates the use of try and catch block:


class Error1
{
public static void main( String args[ ] )
{
int a = 10, b = 5, c = 5;
int x, y;
try
{
x = a / ( b – c );
}
catch ( ArithmeticException e )
{
System.out.println(“ Division by zero “);
}
y = a / ( b + c );
System.out.println(“ Y = “ +y );
}
}
 There could be situations where there is possibility of generation of multiple exceptions of different types
within a particular block of program code.

Department of CSE By: Tejashree K. Gotkhindi Page 70


Programming with Java

 Nested try statements will be used in this situation.


 The execution of corresponding catch blocks of nested try statements is done using stack.
 Below program shows the example of nested try statements
class multiTry
{
public static void main( String args [ ])
{
try
{
int a = 2, b = 4, c = 2, x = 7, z;
int p [ ] = { 2 };
p[3] = 33;
try
{
z = x / (( b * b ) – ( 4 * a * c ));
System.out.println(“ The value of z = “ + z );
}
catch ( ArithmeticException e )
{
System.out.println(“ Divide by zero in Arithmetic expression “);
}
}
catch ( ArrayIndexOutOfBoundsException e )
{
System.out.println(“ Array index is out-of-bounds “);
}
}
}

 Multiple Catch statements


 It is possible to have more than one catch statement in the catch blocks as illustrated below:
............
............
try
{
statement;
}
catch ( Exception-Type1 e )
{
statement;
}
catch ( Exception-Type2 e )
{
statement;
}
.
.
.

Department of CSE By: Tejashree K. Gotkhindi Page 71


Programming with Java

catch ( Exception-Type-n e )
{
statement;
}

 When an exception is n try block is generated, the Java treats the multiple catch statements like cases in
switch statement.
 The first statement whose parameter matches with the exception object will be executed, and remaining
statement will be skipped.
 A catch statement with an empty block can also be used as follows to avoid program abortion
catch ( Exception e );
 The catch statement simply ends with a semicolon, which does nothing.
 This statement will catch an exception and then ignore it.

 Below program illustrates the use of multiple catch statements:


class Error4
{
public static void main( String args [ ] )
{
int a[ ] = { 5, 10 };
int b = 5;
try
{
int x = a[2] / b – a[1];
}
catch(ArithmaticException e)
{
System.out.println(“Divide by zero error”);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(“Array index error”);
}
catch(ArrayStoreException e)
{
System.out.println(“Wrong data type”);
}
int y = a[1] / a[0];
System.out.println(“ Y = “ +y);
}
}

 Using Finally statement


 Java supports another statement known as finally statement that can be used to handle an exception that is
not caught by any of the previous catch statements.
 finally block can be used to handle any exception generated within try block.

 It may be added immediately after try block or after last catch block shown as follows:

Department of CSE By: Tejashree K. Gotkhindi Page 72


Programming with Java

try try
{ {
............. .............
............. .............

} }

finally catch(. . . . . )
{ {
............. .............
............. }
} catch (. . . . . )
{
.............
}
finally
{
.............
.............
}

 When finally block is defined, code is executed, regardless of whether or not exception is thrown.
 We can use it to perform operations like closing files and releasing system resources.

 Throwing our own Exceptions


 We can throw our own exception as follows:
throw new Throwable subclass;

 Example:
throw new ArithmeticException( );
throw new NumberFormatException( );

 Below program demonstrates the use of user-defined subclass of Throwable class:


class MyException extends Exception
{
MyException( String message )
{
super(message);
}
}
class TestMyException
{
public static void main( String args [ ])
{
int x = 5, y = 1000;
try
{
float z = (float) x / (float)y;

Department of CSE By: Tejashree K. Gotkhindi Page 73


Programming with Java

if ( z < 0.01 )
{
throw new MyException ( “ Number is too small “);
}
}
catch ( MyException e)
{
System.out.println(“ Caught my exception “);
System.out.println( e.getMessage( ) );
}
finally
{
System.out.println(“ I am always here “);
}
}
}

 The object e which contains the error message “Number is too small” is caught by the catch block which
then displays the message using the getMessage( ) method.

 There could be situations where there is a possibility that a method might throw certain kinds of exceptions
but there is no exception handling mechanism prevalent within method.
 In such case, it is important that the method caller is intimated explicitly that certain types of exceptions
could be expected from the calledmethod, and the caller must get prepared with some catching mechanism
to deal with it.
 The throw clause is used in such situation.
 It is specified immediately after the method declaration statement and just before opening brace.

 Below program illustrates the use of throws clause:


class Examplethrows
{
static void divide_m( ) throws ArithmeticException
{
int x = 22, y = 0, z;
z = x / y;
}
public static void main ( String args [ ] )
{
try
{
divide_m( );
}
catch( ArithmeticException e )
{
System.out.println(“ Caught the exception “ + e );
}
}

Department of CSE By: Tejashree K. Gotkhindi Page 74


Programming with Java

Department of CSE By: Tejashree K. Gotkhindi Page 75

You might also like