You are on page 1of 258

Core

Java
Core Java offers a total immersion approach for
C\C++ programmer wanting to learn java.
The course will use JDK(Java Development Kit)
and covers all the new language features.
This course inclues worked out examples
illustrating new JDK features that are fully
explained thoughout the course.
Core Java has been carefully planned to help students create
faster, smaller and more efficient Java application.

Copyright
Core Java
Copyright reserved by the Author
All rights reserved. No part of this book shall be
reproduced, stored in a retrieval system, or
transmitted by any means, electronic, mechanical,
photocopying, recording, or otherwise, without
written permission from the publisher. No patent
liability is assumed with respect to the use of the
information contained herein. Although every
precaution has been taken in the preparation of
this book, the publisher and author assume no
responsibility for errors or omissions. Nor is any
liability assumed for damages resulting from the
use of the information contained herein.
Warning and Disclaimer
Every effort has been made to make this book as complete
and as accurate as possible, but no warranty or fitness is implied.
The information provided is on an as is basis. The authors and
the publisher shall have neither liability nor responsibility to any
person or entity with respect to any loss or damages arising from
the information contained in this book.

Contents :
Core Java
Java Evolution
Java History
Java features
Benefits of java over C and C++
How Java works
The Java programming language
The Java Platform
Java Development Kit*
Hardware & Software requirement
Disadvantage of Java

Overview of Java Language


Developing the Java Application*
Comments in Java Code
Defining a class
The main method
Using classes and objects
Java Program Structure
Java Tokens

Constant, Variable and Datatype


Variable
Declaring Variable
Rule on Variable Names
Variable Type
Primitive Data Type
Constants

Operator and Expression


Expression and operators
Operator Precedence
Integer Operators
Relational Integer Operators
Floating point Operators
Relational Floating point Operator
String Operator
Assignment Operator
Arithmetic assignment Operator
Conversions
Casts
Boolean expression & Operations
Logical Operators
Bitwise operators
The complement operator

Decision making, Branching & looping


Flow control with if & else
The if statement
The else statement
Switch statement
For, while & do-while statement
The for statement
The while statement
The do-while statement
Using break and continue
The break statement
The continue statement
Class Object and Method
Introduction to class
The benefit of classes
Defining class
Class using constructor
Object
Declaring an Object
Instantiating an Object
Initializing an Object
Referencing an objects Variable
Calling an Object method
Creating a class
The class Declaration
The class body
Constructions for class
Implementing Methods
The Method body
A Methods name
Example of Math class

Array and String


Array
Java array
Object array
Strings
String concatenation

Inheritance
Introduction
Creating subclasses
Member variables in subclass inherit
Hiding member Variables
Methods in subclass Inherit
Overriding Method
Methods a subclass cannt override
Methods a subclass must override
The Benefits of Inheritance
Interfaces
Interfaces and Classes
Implementing and using Interface
Implementing Multiple Interfaces
Creating and Extending Interfaces
Methods inside Interfaces
Extending Interfaces

Packagee
Introduction
Declaring Packages
Importing Packages
Creating Our Own Packages
The Java Language Packages
The Java I/O Packages
The Java Utility Packages
The Java Networking Packages
The Java Applet Packages
The Java abstract Window Toolkit Packages

Multithreading
Introduction
Thread
Thread Attributes
Thread State
Thread Group
Method that Operate on All Thread
Access Restriction Methods
The notify All () and wait () methods
Frequently used Method

Exception Handling
Introduction
What is an Exception
If Exception than?
The exception handling technique
Some terminology
Throw an exception
Throw, try, and catch blocks
Multiple catch blocks
The finally clause
The throwable class
Type of exception
Different list of exception
Built-in Exceptions

Applet
Introduction
How Applets and Applications are different
Limitation of Applets
The Applet Class
Major Applets Activities
The life cycle of a Web page Applet
Including am Applet on a Web Page
Essential HTML to launch an Applet & pass it parameters
Launching an Applet in an html/core_java Document
A Sample Applet that receives a Parameter
Posting a Web Page that launches a custom Applet

Managing Input/Output Files in Java


Introduction
Stream
Input Streams
The Abstract Class Input Stream
The File Class
The FileDialog Class
Low-level & High-level Stream Class
The FileOutputStream Class
The FileInputStream Class
The DataOutputStream Class
The DataInputStream Class
The ObjectOutputStream Class
The objectInputStream Class
Java Evolution
Java History

Java is a general purpose , object-oriented programming language.
The fastest growing programming language in the history of computing
Developed by Sun Microsystems of USA in 1991.
Originally it is called Oak by James Gosling.
Originally designed in 1991 for use in embedded consumer applications
Java team which headed James Gosling developed a Web browser called Hot Java to
locate and run applet programs on Internet in 1994.
Oak was renamed Java in 1995.
Redesigned in early 1995 with Internet application capabilities
Introduced in May, 1995 and immediately supported by the Netscape Web browser.
Rapidly overtaking C++ in popularity among commercial software developers
Java established itself not only as a leader for Internet programming but also as a
general purpose, object-oriented programming language.
Java found its home in the heart of the modern programmer.
Java is the first programming language that is not tied to any particular hardware or
operating system.
Programs developed in java can be executed anywhere on any system.
Therefore Java comes as a revolutionary technology because it has brought in a
fundamental shift in how we develop and use programs.



Java Evolution
Java Features

Simple Architecture-neutral
Object-oriented Portable
Distributed High-performance
Interpreted Multithreaded
Robust Dynamic
Secure


Simple:
Java is a simple language as it does not use pointers, preprocessor header files; go to
statement and many more others.


Object oriented:
In Java everything is object. All program code and data within objects and classes.


Distributed:
Java is designed as a distributed language for creating applications on networks. It has
ability to share both data and programs.


Interpreted:
Interpreting means actually compiling the program line by line and generates the
machine code. Java interpreter generates machine code that can be directly executed by the
machine is that running the Java program.


Robust:
Java is a robust language as it provides many safeguards to ensure reliable code. It has
strict compile time and run time checking for data types.


Secure:
No viruses can affect the Java program as Java ensures that program cannot gain access
to memory locations without proper authorization. Java systems not only verify all memory
access but also ensure that no viruses are communicated with an applet.


Architecture-neutral:
In Java, changes and upgrade in operating system, processors and system resources will
not force any changes in Java programs.


Portable:
Java programs can be easily moved from one computer system to another, anytime, and
anywhere. Java ensures portability in two ways. First, Java compiler generates bytecode
instructions that can be implemented on any machin. Secondly the sizes of the primitive data
type are machin-independent.


High-performance:
Java performance is impressive for an interpreted language , mainly due to the use of
intermediate bytecode.


Multithreaded:
Java support multithreaded programs means handling multiple task simultaneously.


Dynamic:
Java is capable of dynamically linking in new class libraries, methods and objects.







Java Evolution
Benefits of Java over C and C++
Architecturally neutral. Once written, the same Java program can be run on any
platform computer and operating system) that supports Java.
Entirely object-oriented. Existing code can be easily re-used and maintained.
Secure. Dangerous program actions are prohibited.
Supports Internet programming. Java applets are run by Web browsers.
Robust. Language features and packaged code support graphical programming,
exception handling, networking, multi-threading, I/O, and more.






Java Evolution
How Java works

To develop and distribute a Java program
1. Programmer codes Java source statements
2. Java compiler converts source statements into bytecode
(platform-independent object language)
3. Bytecode is copied to the target platform


To execute a Java program
1. Platform-dependent Java Virtual Machine (JVM) software must be installed
2. A copy of the JVM is started
3. Each bytecode statement is interpreted (translated) by the JVM into platform-dependent
machine language and executed under JVM control.


Java has two things: a programming language and a platform.





Java Evolution
The Java Programming Language

Java is a high-level programming language that is all of the following:

Simple Architecture-neutral
Object-oriented Portable
Distributed High-performance
Interpreted Multithreaded
Robust Dynamic
Secure



Java is also unusual in that each Java program is both compiled and interpreted.
With a compiler, it translates a Java program into an intermediate language called Java
bytecodesthe platform-independent codes interpreted by the Java interpreter.

With an interpreter, each Java bytecode instruction is parsed and run on the computer.
Compilation happens just once; interpretation occurs each time the program is executed. This
figure show how it works.



The Java bytecodes is the machine code instructions for the Java Virtual Machine (Java
VM). Every Java interpreter, whether its a Java development tool or a Web browser that can
run Java applets, is an implementation of the Java VM. The Java VM can also be
implemented in hardware.

Java bytecodes help make write once, run anywhere possible.

One can compile your Java program into bytecodes on any platform that has a Java compiler.
The bytecodes can then be run on any implementation of the Java VM.





Java Evolution
The Java Platform

A platform is the hardware or software environment in which a program runs. The Java
platform differs from most other platforms in that its a software-only platform that runs
on
top of other, hardware-based platforms. Most other platforms are described as a
combination of hardware and operating system.

The Java platform has two components:
The Java Virtual Machine (Java VM)
The Java Application Programming Interface (Java API)

The Java VM is the base for the Java platform and is ported onto various hardware-
based platforms.

The Java API is a large collection of ready-made software components that provide many
useful capabilities, such as graphical user interface (GUI) widgets. The Java API
(application programming interface) is grouped into libraries (packages) of related
components.

Java Evolution
Java Development Kit

The Java Development Kit comes with a collection of tools that are used for developing
and running Java programs. They include:

appletviewer ( for viewing Java applet)
javac (Java compiler)
java (Java Interpreter)
javap (Java disassembler)
javah (for C header files)
javadoc (for creating HTML documents)
jdb (Java debugger)



The below figure depicts a Java program, such as an application or applet, thats running
on the Java platform. As the figure shows, the Java API and Virtual Machine insulates the
Java program from hardware dependencies.







Java Evolution

HARDWARE AND SOFTWARE REQUIREMENTS

Java is currently supported on Window 95, Window NT, Sun Solaries, Macintosh and
UNIX machine.
The minimum software and hardware requirement for 95 version of Java are as follows:

IBM-compatible 486 system
Minimum of 8 MB memory
Windows 95 software
A Windows-compatible sound card
A hard drive;
A CD-ROM drive
A Microsoft-compatible mouse



Java Evolution
Disadvantages of Java

Not supported by all platforms (though third-party JVM software is usually available)
Slower in execution than compiled languages
Restricts or prohibits machine-level operations required by certain applications
(operating systems, etc.)



Overview of Java Language
Developing the Java Application

A Java application is a standalone Java program a program written in the Java
language that runs independently of any browser.

Example:

/**
* The HelloWorldApp class implements an application that
* simply displays Hello World! to the standard output.
*/
class HelloWorldApp
{
public static void main(String[] args)
{
System.out.println(Hello World!); //Display the string.
}


}
To view, how to write,compile and run the Java Program.(With Sound)





Overview of Java Language
Comments in Java Code
The bold characters in the following listing are comments.
/**
* The HelloWorldApp class implements an application that
* simply displays Hello World! to the standard output.
*/
class HelloWorldApp
{
public static void main(String[] args)
{
System.out.println(Hello World!); //Display the string.
}
}
The Java language supports three kinds of comments:
/* text */
The compiler ignores everything from /* to */.
/** documentation */
This indicates a documentation comment (doc comment, for short). The compilerignores
this kind of comment, just like it ignores comments that use /* and */.
// text
The compiler ignores everything from // to the end of the line





Overview of Java Language
Defining a Class

The first bold line in the following listing begins a class definition block.

/**
* The HelloWorldApp class implements an application that
* simply displays Hello World! to the standard output.
*/
class HelloWorldApp
{
public static void main(String[] args)
{
System.out.println(Hello World!); //Display the string.
}
}



A classthe basic building block of an object-oriented language such as Javais a
template that describes the data and behavior associated with instances of that class.

The data associated with a class or object is stored in variables; the behavior associated
with a class or object is implemented with methods. Methods are similar to the functions
or procedures in procedural languages such as C.

In the Java language, the simplest form of a class definition is



class name {

}

The keyword class begins the class definition for a class named name.
The variables and methods of the class are embraced by the curly brackets that begin and
end the class definition block. The Hello World application has no variables and has a
single method named main.





Overview of Java Language
The main Method

The first bold line in the following listing begins the definition of a main method.

/**
* The HelloWorldApp class implements an application that
* simply displays Hello World! to the standard output.
*/

class HelloWorldApp {
public static void main(String[] args)
{
System.out.println(Hello World!); //Display the string.
}
}

Every Java application must contain a main method whose signature looks like this:

public static void main(String[] args)


{
System.out.println(Hello World!); //Display the string.
}
}

Every Java application must contain a main method whose signature looks like this:

public static void main(String[] args)

The method signature for the main method contains three modifiers:

public indicates that the main method can be called by any object.
static indicates that the main method is a class method.
void indicates that the main method doesnt return any value.


How the main Method Gets Called
The main method in the Java language is similar to the main function in C and C++.
When the Java interpreter executes an application (by being invoked upon the
applications controlling class), it starts by calling the classs main method. The main
method then calls all the other methods required to run your application. If you try to
invoke the Java interpreter on a class that does not have a main method, the interpreter
refuses to compile your program and displays an error message similar to this: In class
NoMain: void main(String argv[]) is not defined


Arguments to the main Method
Here the main method accepts a single argument: an array of elements of type String.
public static void main(String[] args)

This array is the mechanism through which the runtime system passes information to your
application. Each String in the array is called a command-line argument.





Overview of Java Language
Using Classes and Objects

The Hello World application is about the simplest Java program one can write that
actually does something. Because it is such a simple program, it doesnt need to define
any classes except for HelloWorldApp.

The Hello World application does use another classthe System classthat is part of
the API (application programming interface) provided with the Java environment. The
System class provides system-independent access to system-dependent functionality.

The bold code in the following listing illustrates the use of a class variable of the System
class, and of an instance method.

/**
* The HelloWorldApp class implements an application that
* simply displays Hello World! to the standard output.
*/

class HelloWorldApp
{
public static void main(String[] args)
{
System.out.println(Hello World!); //Display the string.
}
}


Using a Class Method or Variable

Lets take a look at the first segment of the statement:
System.out .println(Hello World!);
The construct System.out is the full name of the out variable in the System class.



Using an Instance Method or Variable

Methods and variables that are not class methods or class variables are known as instance
methods and instance variables.

While Systems out variable is a class variable, it refers to an instance of the PrintStream
class (a class provided with the Java development environment) that implements the
standard output stream.

When the System class is loaded into the application, it instantiates PrintStream and
assigns the new PrintStream object to the out class variable.

System.out.println(Hello World!);

This line of code displays Hello World! to the applications standard output stream





Overview of Java Language
Java Program Structure

A Java program may contain one or more section which is as follow:

1. Documentation section (suggested)
The documentation section comprises a set of comment line of the program, the author
and other details, which the programmer would like to refer to at a later stage.

2. Package statement (Optional)
This statement declares a package name and inform the compiler that the class defined
here belong to this package.
Ex: package student;

3. Import Statement (Optional)
This statement instructs the interpreter to load the specific class contained in the package.
Ex: import student. test;

4. Interface Statement (Optional)
An interface is like a class but include a group of method declarations.

5. Class Definitions (Optional)
Classes are the primary and essential elements of Java program. These classes are used
to map the object of real world problems.

6. Main Method Class (Essential)

As every Java stand-alone program requires a main method as its starting point, this
class is the essential part of the program.






Overview of Java Language
Java Tokens

The smallest individual units in a program are known as TOKEN.
A java program is a collection of tokens, comments and white spaces.
Java language includes five types of tokens

They are:
1) Reserved Keyword
2) Identifiders
3) Literals
4) Operator
5) Separators



Reserved Keyword
Keywords have specific meaning and implement specific features of the language.
Java language has reserved 60 words as keywords.


Identifiers
Identifiers are programmer-designed tokens. They are used for naming class,
methods, variables, objects, labels, packages, and interface in a program.


Literals
Literal is a programming language term that essentially means that what you type is what
you get. Numbers, characters, and strings are all examples of literals.


Therefore literals
1) Are constants having no identifier?
2) Have their value specified within the programs source code?
3) Can only appear on the right side of an assignment operator (=) or within an
expression?
4) Have a data type associated with them?



Java program has five major types of literals:
1) Integer Literals
2) Floating type literals
3) Character literals
4) String literals
5) Boolean Literals



Integer literals
1. Represent an integer value
2. Can be expressed in decimal (the default), octal (base 8, or hexadecimal (base 16)
3. Are not enclosed in any special characters
4. Are automatically int (32 bits) unless the suffix L is appended to make it long (64
bits)



Floating-point literals
1. Represent a real number (having a decimal point)
2. Can be expressed as a standard decimal value or in scientific notation
3. Are not enclosed in any special characters
4. Are automatically double (64 bits) unless the suffix F is appended to make it float (32
bits)



char literals
1. Represent a single Unicode character (16 bits)
2. Must be enclosed within single quotes (apostrophes)
3. Are often associated with a single key stroke
4. Can represent special characters (escape sequences) used for device control



String literals
1. Represent a string of characters, such as Java is fun
2. Must be enclosed in double quotes
3. Are automatically stored as String class objects by the compiler. They will be covered
later.



boolean literals
1. Can only have the value true or false
2. Can only be assigned to boolean variables



Constants
1. Are similar to variables but, once initialized, their contents may NOT be changed?
2. Are declared with the keyword final?
3. By convention, have all capital letters in their identifier. This makes them easier to see
within the code.



Operators
An operator is a symbols that takes one or more arguments and operates on them to
produce a result.



Separators
Separators are symbols used to indicate where groups of code are divided and arranged.
They are basically define the shape and function of our code.
Ex: {}, [], ; , . , () etc.





Constaant, Variable and Datatype
Variables

Variables are locations in memory in which values can be stored. Each one has a name, a
type, and a value.

Before we can use a variable, we have to declare it. After it is declared, we can then
assign values to it.

Java actually has three kinds of variables:
1. instance variables,
2. class variables,
3. And local variables.

Instance variables, are used to define the attributes of a particular object.
Class variables are similar to instance variables, except their values apply to all that
classs instances (and to the class itself) rather than having different values for each
object.

Local variables are declared and used inside method definitions,

Although all three kinds of variables are declared in much the same ways, class and
instance variables are accessed and assigned in slightly different ways from local
variables.

Java does not have global variables-that is, variables that are global to all parts of a
program. Instance and class variables can be used to communicate global information
between and among objects.






Constaant, Variable and Datatype
Declaring Variables

To use any variable in a Java program, we must first declare it. Variable declarations
consist of a type and a variable name:

int myAge;
String myName;
boolean isTired;

Variable definitions can go anywhere in a method definition (that is, anywhere a regular Java
statement can go), although they are most commonly declared at the beginning of the
definition before they are used:

public static void main (String args[]) {
int count;
String title;
boolean isAsleep;

}

We can string together variable names with the same type on one line:
int x, y, z;
String firstName, LastName;

We can also give each variable an initial value when we declare it:
int myAge, mySize, numShoes = 28;
String myName = eBIZ;
boolean isTired = true;
int a = 4, b = 5, c = 6;


If there are multiple variables on the same line with only one initializer the initial
value applies to only the last variable in a declaration.

We can also group individual variables and initializers on the same line using
commas, as with the last example.






Constaant, Variable and Datatype
Rules on Variable Names

Variable names in Java can start with a letter, an underscore (_), or a dollar sign ($). They
cannot start with a number. After the first character, our variable names can include any
letter or number. Symbols, such as %, *, @, and so on, are often reserved for operators in
Java, so we should be careful when using symbols in variable names. The Java language
uses the Unicode character set. The Java language is case sensitive, which means that
uppercase letters are different from lowercase letters. This means that the variable X is
different from the variable x, and a rose is not a Rose is not a ROSE.






Constaant, Variable and Datatype
Variable Types

In addition to the variable name, each variable declaration must have a type, which defines
what values that variable can hold.

The variable type can be one of three things:
One of the eight primitive data types
The name of a class or interface
An array







Constaant, Variable and Datatype
Primitive Data Types

The eight primitive data types handle common types for integers, floating-point numbers,
characters, and boolean values (true or false).
Theyre called primitive because theyre built into the system and are not actual objects,
which makes them more efficient to use.

Integer Type
There are four Java integer types, each with a different range of values.
All are signed, which means they can hold either positive or negative numbers.


Table 3.1. Integer types

Type Size Range


byte 8 bits -128 to 127
short 16 bits -32,768 to 32,767
int 32 bits -2,147,483,648 to 2,147,483,647
-9,223,372,036,854,775,808 to
long 64 bits
9,223,372,036,854,775,807


Floating-point type:
Floating-point numbers are used for numbers with a decimal part.
There are two floating-point types: float (32 bits, single precision) and double (64 bits,
double precision).



Char type:
The char type is used for individual characters. Because Java uses the Unicode character
set, the char type has 16 bits of precision, unsigned.






Constaant, Variable and Datatype
Constants

Are similar to variables but, once initialized, their contents may NOT be changed?
Are declared with the keyword final?
By convention, have all capital letters in their identifier. This makes them easier to see
within the code.


Example 1:
This program defines a number of constants and then displays some of their values.

public class App
{
public static void main(String[] args)
{

final boolean YES = true;


final char DEPOSIT_CODE = D;
final byte INCHES_PER_FOOT = 12;
final int FEET_PER_MILE = 5280;
final float PI = 3.14F;
final double SALES_TAX_RATE = .06;
final String ADDRESS = 119 South Street;

// Display some of the values

System.out.println(INCHES_PER_FOOT);
System.out.println(ADDRESS);
}
}


Example 2:
This program will not compile because an attempt is made to change the value of its
constant.

public class App1
{
public static void main(String[] args)
{

final double SALES_TAX_RATE = .06;


SALES_TAX_RATE = .04;

// Display the sales tax rate

System.out.println(SALES_TAX_RATE);
}
}
Here the compiler error will occur like this






Operator and Expression

Expressions and Operators

Operators enable us to perform an evaluation or computation on a data object or objects.
Operators applied to variables and literals form expressions.

An expression can be thought of as a programmatic equation. Therefore, an expression is a
sequence of one or more data objects (operands) and zero or more operators that produce a
result.

An example of an expression follows:
x = y / 3;

In this expression, x and y are variables, 3 is a literal, and = and / are operators.

This expression states that the y variable is divided by 3 using the division
operator (/), and the result is stored in x using the assignment operator (=).

Here the expression is described from right to left.



Operator and Expression

Operator Precedence

Java expressions are typically evaluated from left to right, there still are many times
when the result of an expression would be indeterminate without other rules.

The following expression illustrates the problem:
x = 2 * 6 + 16 / 4


By using the left-to-right evaluation of the expression, the multiplication operation 2 * 6
is carried out first, which leaves a result of 12. The addition operation 12 + 16 is then
performed, which gives a result of 28. The division operation 28 / 4 is then performed,
which gives a result of 7. Finally, the assignment operation x = 7 is handled, in which the
number 7 is assigned to the variable x.
Butits wrong! The problem is that using a simple left-to-right evaluation of
expressions can yield inconsistent results, depending on the order of the operators.

The solution to this problem lies in operator precedence, which determines the order in
which operators are evaluated. Every Java operator has an associated precedence.

Following is a list of all the Java operators from highest to lowest precedence.

In this list of operators, all the operators in a particular row have equal precedence.

The precedence level of each row decreases from top to bottom. This means that the []
operator has a higher precedence than the * operator, but the same precedence as the ()
operator.


. [] ()
++ - ! ~
* / %
+ -
<< >> >>>
<</td> > <= >=
= !=
&
^
&&
||
?:
=


Evaluation of expressions still moves from left to right, but only when dealing with
operators that have the same precedence.

Otherwise, operators with a higher precedence are evaluated before operators with a lower
precedence.

Knowing this, take another look at the sample equation:
x = 2 * 6 + 16 / 4

Before using the left-to-right evaluation of the expression, first look to see whether any of
the operators have differing precedence.

Here the multiplication (*) and division (/) operators both have the highest precedence,
followed by the addition operator (+), and then the assignment operator (=).

Because the multiplication and division operators share the same precedence, evaluate
them from left to right. Doing this, we first perform the multiplication operation 2 * 6
with the result of 12. Then we perform the division operation 16 / 4, which results in 4.

After performing these two operations, the expression looks like this:
x = 12 + 4;

Because the addition operator has a higher precedence than the assignment operator, we
perform the addition operation 12 + 4 next, resulting in 16. Finally, the assignment
operation x = 16 is processed, resulting in the number 16 being assigned to the variable x.

As we can see, evaluating the expression using operator precedence yields a completely
different result.

Just to get the point across, take a look at another expression that uses parentheses for
grouping purposes:
x = 2 * (11 - 7);

Without the grouping parentheses, we would perform the multiplication operation first
and then the subtraction operation. However, referring back to the precedence list, the ()
operator comes before all other operators. So the subtraction operation 11 - 7 is
performed first, yielding 4 and the following expression:
x = 2 * 4;

The rest of the expression is easily resolved with a multiplication operation and an
assignment operation to yield a result of 8 in the variable x.





Operator and Expression
Integer Operators

There are three types of operations that can be performed on integers:
1. unary,
2. binary,
3. And relational.

Unary operators act on only single integer numbers,

Binary operators act on pairs of integer numbers.

Both unary and binary integer operators typically return integer results.

Relational operators, act on two integer numbers but return a Boolean result rather than
an integer.

Unary and binary integer operators typically return an int type. For all operations
involving the types byte, short, and int, the result is always an int.
The only exception to this rule is when one of the operands is a long, in which case the
result of the operation is also of type long.



Unary Integer Operators
Unary integer operators act on a single integer. Lists of the unary integer operators.

The unary integer operators.

Description Operator
Increment ++
Decrement
Negation -
Bitwise complement ~



The increment and decrement operators (++ and ) increase and decrease integer
ariables by 1.

These operators can be used in either prefix or postfix form. A prefix operator takes effect
before the evaluation of the expression it is in; a postfix operator takes effect after the
expression has been evaluated.

Prefix unary operators are placed immediately before the variable; postfix unary
operators are placed immediately following the variable. Following are examples of each
type of operator:
y = ++x;
z = x;

In the first example, x is prefix incremented, which means that it is incremented before
being assigned to y.
In the second example, x is postfix decremented, which means that it is decremented after
being assigned to z. Here z is assigned the value of x before x is decremented.
The IncDec program, which uses both types of operators.

The IncDec class.

class IncDec
{
public static void main (String args[])
{
int x = 8, y = 13;
System.out.println(x = + x);
System.out.println(y = + y);
System.out.println(++x = + ++x);
System.out.println(y++ = + y++);
System.out.println(x = + x);
System.out.println(y = + y);
}

}

The IncDec program produces the following results:

x = 8
y = 13
++x = 9
y++ = 13
x = 9
y = 14


The negation unary integer operator (-) is used to change the sign of an integer value.
x = 8;
y = -x;

In this example, x is assigned the literal value 8 and then is negated and assigned to y.
The resulting value of y is -8.

The Negation class.

class Negation
{
public static void main (String args[])
{
int x = 8;
System.out.println(x = + x);
int y = -x;
System.out.println(y = + y);
}


The bitwise complement operator (~), which performs a bitwise negation of an
integer value.
Bitwise negation means that each bit in the number is toggled. In other words, all the
binary 0s become 1s and all the binary 1s become 0s.

Example:
x = 8;
y = ~x;

In this example, x is assigned the literal value 8 again, but it is bitwise complemented
before being assigned to y.

Well, without getting into the details of how integers are stored in memory, it means that
all the bits of the variable x are flipped, yielding a decimal result of -9. This result has to
do with the fact that negative numbers are stored in memory using a method known as
twos complement.

NOTE: Integer numbers are stored in memory as a series of binary bits that can each
have a value of 0 or 1. A number is considered negative if the highest-order bit in the
number is set to 1. Because a bitwise complement flips all the bits in a number
including the high-order bitthe sign of a number is reversed.



The Bitwise Complement class.
class BitwiseComplement
{
public static void main (String args[])
{
int x = 8;
System.out.println(x = + x);
int y = ~x;
System.out.println(y = + y);
}



Binary Integer Operators
Binary integer operators act on pairs of integers. Lists of the binary integer operators.

The binary integer operators.

Description Operator
Addition +
Subtraction -
Multiplication *
Division /
Modulus %
Bitwise AND &
Bitwise OR |
Bitwise XOR ^
Left-shift <<
Right-shift >>
Zero-fill-right-shift >>>



The Arithmetic program, which shows how the basic binary integer arithmetic operators
work.



The Arithmetic class.

class Arithmetic
{
public static void main (String args[])
{
int x = 17, y = 5;
System.out.println(x = + x);
System.out.println(y = + y);
System.out.println(x + y = + (x + y));
System.out.println(x - y = + (x - y));
System.out.println(x * y = + (x * y));
System.out.println(x / y = + (x / y));
System.out.println(x % y = + (x % y));
}

}

The results of running the Arithmetic program follow:

x = 17
y = 5
x + y = 22
x - y = 12
x * y = 85
x / y = 3
x % y = 2


The bitwise AND, OR, and XOR operators (&, |, and ^) all act on the individual bits of an
integer. These operators are sometimes useful when an integer is being used as a bit field.



The Bitwise class.

class Bitwise
{
public static void main (String args[])
{
int x = 5, y = 6;
System.out.println(x = + x);
System.out.println(y = + y);
System.out.println(x & y = + (x & y));
System.out.println(x | y = + (x | y));
System.out.println(x ^ y = + (x ^ y));
}

}

The output of running Bitwise follows:

x = 5
y = 6
x & y = 4
x | y = 7
x ^ y = 3


In Bitwise, the variables x and y are set to 5 and 6, which correspond to the binary
numbers 0101 and 0110.

The bitwise AND operation compares each bit of each number to see whether they are the
same. It then sets the resulting bit to 1 if both bits being compared are 1; it sets the
resulting bit to 0 otherwise. The result of the bitwise AND operation on these two
numbers is 0100 in binary, or decimal 4.

The bitwise OR operator sets the resulting bit to 1 if either of the bits being compared is
1. For these numbers, the result is 0111 binary, or 7 decimal.

Finally, the bitwise XOR operator sets resulting bits to 1 if exactly one of the bits being
compared is 1 and 0 otherwise. For these numbers, the result is 0011 binary, or 3 decimal.
The left-shift, right-shift, and zero-fill-right-shift operators (<<, >>, and >>>) shift the
individual bits of an integer by a specified integer amount.

Following are some examples of how these operators are used:

x << 3;
y >> 7;
z >>> 2;

In the first example, the individual bits of the integer variable x are shifted to the left
three places.
In the second example, the bits of y are shifted to the right seven places.
Finally, the third example shows z being shifted to the right two places, with zeros shifted
into the two leftmost places.
The Shift class.

class Shift
{
public static void main (String args[])
{
int x = 7;
System.out.println(x = + x);
System.out.println(x >> 2 = + (x >> 2));
System.out.println(x << 1 = + (x << 1));
System.out.println(x >>> 1 = + (x >>> 1));
}
}

The output of Shift follows:

x = 7
x >> 2 = 1
x << 1 = 14
x >>> 1 = 3


The number being shifted in this case is the decimal 7, which is represented in binary as
0111. The first right-shift operation shifts the bits two places to the right, resulting in the
binary number 0001, or decimal 1.

The next operation, a left-shift, shifts the bits one place to the left, resulting in the binary
number 1110, or decimal 14.

The last operation is a zero-fill-right-shift, which shifts the bits one place to the right,
resulting in the binary number 0011, or decimal 3.






Operator and Expression

Relational Integer Operators

The last group of integer operators is the relational operators, which all operate on
integers but return a type boolean. Lists of the relational integer operators.

The relational integer operators.

Description Operator

Less-than <</td>
Greater-than >
Less-than-or-equal-to <=
Greater-than-or-equal-to >=
Equal-to ==
Not-equal-to !=


These operators all perform comparisons between integers.


The Relational class

class Relational
{
public static void main (String args[])

{
int x = 7, y = 11, z = 11;
System.out.println(x = + x);
System.out.println(y = + y);
System.out.println(z = + z);
System.out.println(x < y = + (x < y));
System.out.println(x > z = + (x > z));
System.out.println(y <= z = + (y <= z));
System.out.println(x >= y = + (x >= y));
System.out.println(y == z = + (y == z));
System.out.println(x != y = + (x != z));
}


The output of running Relational follows:

x= 7
y = 11
z = 11
x < y = true
x > z = false
y <= z = true
x >= y = false
y == z = true
x != y = true






Operator and Expression

Floating point Operators

There are three types of operations that can be performed on floating-point numbers:
1. unary,
2. binary,
3. and relational


Unary operators act only on single floating-point numbers,

binary operators act on pairs of floating-point numbers.

Both unary and binary floating-point operators return floating-point results.

Relational operators, act on two floating-point numbers but return a boolean result.

Unary and binary floating-point operators return a float type if both operands are of type
float. If one or both of the operands are of type double, however, the result of the
operation is of type double.



Unary Floating-Point Operators
The unary floating point operators act on a single floating-point number. Lists of the
unary floating-point operators.

The unary floating-point operators.
Description Operator
Increment ++
Decrement


The only two unary floating-point operators are the increment and decrement operators.
These two operators respectively add and subtract 1.0 from their floating-point operand.



Binary Floating-Point Operators
The binary floating-point operators act on a pair of floating-point numbers. Lists of the
binary floating-point operators.

The binary floating-point operators.
Description Operator
Addition +
Subtraction -
Multiplication *
Division /
Modulus %

The binary floating-point operators consist of the four traditional binary operations (+, -,
*, /), along with the modulus operator (%).



The FloatMath class.

class FloatMath
{
public static void main (String args[])
{
float x = 23.5F, y = 7.3F;
System.out.println(x = + x);
System.out.println(y = + y);
System.out.println(x + y = + (x + y));
System.out.println(x - y = + (x - y));
System.out.println(x * y = + (x * y));
System.out.println(x / y = + (x / y));
System.out.println(x % y = + (x % y));
}

}

The output of FloatMath follows:

x = 23.5
y = 7.3
x + y = 30.8
x - y = 16.2
x * y = 171.55
x / y = 3.21918
x % y = 1.6




width=410>
Operator and Expression
Relational Floating-Point Operators

The relational floating-point operators compare two floating-point operands, leaving a
boolean result.

Boolean Operators
Boolean operators act on boolean types and return a boolean result.


The boolean operators

Description Operator
Evaluation AND &
Evaluation OR |
Evaluation XOR ^
Logical AND &&
Logical OR ||
Negation !
Equal-to ==
Not-equal-to !=
Conditional ?:


The evaluation operators (&, |, and ^) evaluate both sides of an expression before
determining the result.

The following code shows how the evaluation AND operator is necessary for the
complete evaluation of an expression:

while ((++x < 10) && (++y < 15)) {
System.out.println(x);
System.out.println(y);

}

The three boolean operatorsnegation, equal-to, and not-equal-to (!, ==, and !=)
The negation operator toggles the value of a boolean from false to true or from true to
false, depending on the original value.
The equal-to operator simply determines whether two boolean values are equal (both
true or both false).

Similarly, the not-equal-to operator determines whether two boolean operands are
unequal.

The conditional boolean operator (? :) is the most unique of the boolean operators This
operator also is known as the ternary operator because it takes three items: a condition
and two expressions.

The syntax for the conditional operator follows:


Condition ? Expression1 : Expression2

The Condition, which itself is a boolean, is first evaluated to determine whether it is true
or false. If Condition evaluates to a true result, Expression1 is evaluated. If Condition
ends up being false, Expression2 is evaluated.



The Conditional class

class Conditional
{
public static void main (String args[])
{
int x = 0;
boolean isEven = false;
System.out.println(x = + x);
x = isEven ? 4 : 7;
System.out.println(x = + x);
}

}

The results of the Conditional program follow:
x = 0
x = 7





width=410>
Operator and Expression
String Operator

There is only one string operator: the concatenation operator (+). The concatenation
operator for strings works very similarly to the addition operator for numbersit adds
strings together.


The Concatenation class

class Concatenation
{
public static void main (String args[])
{
String firstHalf = What + did ;
String secondHalf = you + say?;
System.out.println(firstHalf + secondHalf);
}

}

The output of Concatenation follows:
What did you say?




Operator and Expression
Assignment Operators

Assignment operators actually work with all the fundamental data types.

The assignment operators

Description Operator

Simple =
Addition +=
Subtraction -=
Multiplication *=
Division /=
Modulus %=
AND &=
OR |=
XOR ^=


Examples:
x += 6;
x *= (y - 3);

In the first example, x and 6 are added and the result stored in x.
In the second example, 3 is subtracted from y and the result is multiplied by x.
The final result is then stored in x





Operator and Expression
Arithmetic assignment operations

1. Combine arithmetic and assignment into a single operation to save coding time
2. All five basic arithmetic operations have a corresponding arithmetic assignment
operator .


Operator Operation

+= Addition assignment
-= Subtraction assignment
*= Multiplication assignment
/= Division assignment
%= Modulo (remainder) assignment


Example:
This program reads a percentage value from the user (such as 5.5) and converts to the
correct decimal equivalent (such as 0.055) by using the division assignment operator.

import java.util.*;
import java.io.*;

public class AppStr


{
public static void main(String[] args)
{

// Variable for holding a percentage entered by the user // Prompt for and read the
percentage System.out.print(Enter a percentage in n.nn format: );
double percent;
Scanner Keyboard = new Scanner(System.in);

percent = Keyboard.nextDouble();
//percent = Keyboard.readDouble();
// Convert it to its decimal equivalent and display the result
percent /= 100;
System.out.println(The decimal equivalent is + percent);
}
}




Operator and Expression
Conversions

Conversion is performed automatically when mixed numeric types appear within an
expression.

Variables and constants are not altered, but their values are copied to intermediate
areas of larger size or precision to prevent possible data loss during the calculation.
This is known as a widening conversion.

The order of conversion (from narrowest to widest) is as follows. Memorize this table!


The data type resulting from an expression is that of the largest or most precise variable
or constant appearing in the expression but is never smaller than int.


Example:
This program to convert a fahrenheit temperature to celsius will NOT compile.

import
java.util.*;
import java.io.*;
public class Conversion
{
public static void main(String[] args)
{

// Variables for holding fahrenheit and celsius temperatures

float tempC;

// Prompt for and read the fahrenheit temperature

System.out.print(Enter the fahrenheit temperature (nn.n): );


Scanner Keyboard=new Scanner(System.in);
double tempF;
tempF= Keyboard.nextDouble();

// Convert to celsius and display the result

tempC = 5 * (tempF - 32) / 9;


System.out.println(Celsius equivalent is + tempC);
}
}


Note:
To fix the compile error in this code, tempC must be double or tempF must be float .

After Correction changing the tempC into double the result is..




Operator and Expression
Casts

1. Are a programmers way of telling the compiler to ignore possible loss of data or
precision that might result from a conversion?

2. Should be used with caution. Data loss is not to be taken lightly.

3. The general syntax is
identifier = (data-type) expression;


Example:
This is a corrected version of the previous program that uses a cast to prevent the
compile error.

import java.util.*;
import java.io.*;
public class Application
{
public static void main(String[] args)
{

// Variables for holding fahrenheit and celsius temperatures

float tempC;

// Prompt for and read the fahrenheit temperature

System.out.print(Enter the fahrenheit temperature (nn.n): );


Scanner Keyboard=new Scanner(System.in);
double tempF;
tempF=Keyboard.nextDouble();

// Convert to celsius and display the result

tempC = (float) (5 * (tempF - 32) / 9);


System.out.println(Celsius equivalent is + tempC);
}
}


Some thing on arithmetic expressions
Arithmetic expressions can be large and complex with several variables, constants, and
operators. The order in which operations are performed is the same as in mathematics
(multiplication and division first, then addition and subtraction, working from left to
right).

To avoid confusion, good programmers keep their expressions as simple as possible and
use parenthesis for clarity.






Operator and Expression
Boolean expressions and operations

Boolean expressions
1. Always result in a boolean value (either true or false)

2. Are used to resolve logic questions, such as determining if two variables have the same
value?

3. May contain several different operators?


Comparison operators
1. Used to compare the values of two variables or constants

2. Require that the two operands be either both numeric (including char) or both
boolean. A numeric value cannot be compared to a boolean value.

3. Frequently appear in if, for, while, and do-while statements (to be covered later)

Operator Operation

<</td> Less than


<= Less than or equal
> Greater than
>= Greater than or equal
== Equal
!= Not equal


Example:
This program reads two numeric values from the user and displays the result of several
comparisons involving the two numbers.

import java.util.*;
import java.io.*;
public class Aman
{
public static void main(String[] args)
{

// Variables for holding two numeric values entered by the user

int x;
double y;

// Prompt for and read the two numeric values

System.out.print(First number (integer): );


Scanner Keyboard=new Scanner(System.in);
int x;
x=Keyboard.nextInt(();
System.out.print(Second number (floating-point): );
Scanner Keyboard=new Scanner(System.in);
double y;
y=Keyboard.nextDouble();

// Display information comparing the two values

System.out.println( + x + < + y + is + (x < y));


System.out.println( + x + <= + y + is + (x <= y));
System.out.println( + x + > + y + is + (x > y));
System.out.println( + x + >= + y + is + (x >= y));
System.out.println( + x + == + y + is + (x == y));
System.out.println( + x + != + y + is + (x != y));
}
}




Operator and Expression
Logical operators

1. Combine the results of two Boolean expressions into a single boolean value
2. Provide for complex logic. The following table shows the operators and how they can
be
used to combine the results of two Boolean expressions X and Y:

Resulting value true X and Y always
Operator Operation
if evaluated?

& AND X and Y are both true Yes


| OR either X or Y is true Yes
XOR (exclusive X and Y have different
^ Yes
OR) values
Conditional
&& X and Y are both true No
AND
|| Conditional OR either X or Y is true No\



The conditional AND ( && ) and OR ( || ) are sometimes called short-circuit operators
because the second operand is not always evaluated depending on the value of the first
operand. If the first operand is false, the result of an AND will always be false regardless
of the value of the second operand. If the first operand is true, the result of an OR will
always be true regardless of the value of the second operand.

Example:
The following program uses two values entered by the user to determine if a customer is
entitled to free shipping. A customer receives free shipping if their order amount is
greater than or equal to 100 and they are a preferred customer.

import java.util.*;
import java.io.*;
public class Aman
{
public static void main(String[] args)
{

// Variables for holding order amount and valued customer data to be


// entered by the user

// Variables for holding information about free shipping

boolean isFree;

// Prompt for and read order amount and valued customer data

System.out.print(Order amount (floating-point): );


Scanner Keyboard= new Scanner(System.in);
double amount;
amount =Keyboard.nextDouble();
System.out.print(Valued customer? (Y)es or (N)o: );
Char valuedCust;

valuedCust = Keyboard.nextChar();

// Determine and display information about free shipping.


// Shipping is free if the order amount is greater than or equal $100
// AND the customer is a valued customer.

isFree = (amount >= 100 && (valuedCust == Y || valuedCust == y));


System.out.println( Free shipping? + isFree);
}
}
Certain logical operators can be combined with assignment

1. The operators are &= (AND equals), |= (OR equals), and ^= (XOR equals)
2. The left operand must be boolean



Example:
The following program reads a boolean value from the user and displays its
opposite value.

public class AppCer
{
public static void main(String[] args)
{

// Variable for holding a boolean value entered by the user

boolean value;

// Prompt for and read the boolean value

System.out.print(Enter a boolean value (true or false): );


value = Keyboard.readBoolean();

// Determine the opposite value. If the value is false, XOR with


// true results in true. If the value is true, XOR with true results
// in false.

value ^= true;

// Display the new value of the value.

System.out.println( The opposite value is + value);


}
}
***try this yourself






Operator and Expression
Bitwise operations

Bitwise operations act upon individual bits within integer data.
They are used to perform the logical operations AND, OR, and XOR (eXclusive OR),
complementing (reversing all bits), and shifting (sliding bits to the left or right).



Logical bitwise operations

1. Use the standard boolean operators (&, |, and ^) to act upon two integer values. The
short-circuit boolean operators (&& and ||) are not used for bitwise operations and will
result in a compile error if attempted.

2. Produce an integer result (of size int or larger) that is the logical AND, OR, or XOR of
two operands.

The rules for these operations are as follows:

Operation Rule

If both corresponding operand bits are on the result bit


&
is on
If either corresponding operand bit is on the result bit is
|
on
If corresponding operand bits are different the result bit is
^
on






Operator and Expression
The complement operator

1. Is unary (has a single operand)?
2. Uses the ~ operator to flip (reverse) the bits within an integer value. If a bit is on it
is turned off. If a bit is off it is turned on.

For example,
if
byte a = 17; // Binary value: 0001 0001 Hex value: 11
byte b;
after executing the statement
b = (byte) ~a;

variable b will have a value of -18 (because 0001 0001 reverses to 1110 1110). Once
again, the cast is needed in order to store the int value that results from the operation.

Example:
The following program can be run to test complement operations.

import java.util.*;
import java.io.*;
public class Aman
{
public static void main(String[] args)
{

// Variable to be read from the user

// Prompt for and read an integer value

System.out.print(Integer: );
Scanner Keyboard=new Scanner(System.in);
int number;
number=Keyboard.nextInt();

// Display the result of complementing the number

System.out.println( ~ + number + = + ~number);


}
}






Decision making, Branching and Looping
Flow control with if and else

The if and else statements allow a block of code or a single statement to be ither executed
or bypassed depending upon the result of a comparison operation.

They are an essential part of Java flow control by which execution may proceed along
alternate logic paths within a program.








Decision making, Branching and Looping
The if statement

1. Identifies a single statement or a block of statements to be executed if an expression
evaluates to true.
2. Have two forms.

The general syntax is either of the following:

if (expression) statement;
or
if (expression)
{
statements;
}



The first form is often referred to as a single statement if.

For example,
if (amountDue > 0)
System.out.println(You owe + amountDue);

will display the message only if the value of amountDue is greater than zero. The logic
path merges at the next statement in the program.

The second form is often referred to as a multiple statement if.

For example,
if (amountDue > 0)
{
totalDueFromAllCustomers += amountDue;
System.out.println(You owe + amountDue);
}

will add to the grand total and display the message only if the value of amountDue is
greater than zero. The logic path merges at the first statement after the end of the
statement block.

3) Can lure unsuspecting programmers into some nasty errors.

For example,
if (amountDue > 0);
System.out.println(You owe + amountDue);

will display the message no matter what value amountDue has.






Decision making, Branching and Looping
The else statement

1) Defines an alternative and mutually exclusive logic path to be followed if the
preceding if statement expression evaluates to false

2) Must always be paired with, and follow, an if statement. It can never be coded
separately

3) Have two forms.

The general syntax is either of the following:
else statement;
or

else
{
statements;
}

The first form is often referred to as a single statement else .

For example,
if (amountDue > 0)
System.out.println(You owe + amountDue);
else
System.out.println(You owe nothing);

will add to the grand total and display how much is owed if amountDue is greater than
zero. Otherwise, the customerRating will be set to A and the You owe nothing
message will be displayed.

The two logic paths are mutually exclusive and merge at the first statement after the end
of the else block.

4) Can trap programmers who get sloppy.

For example,
if (age < 65)
System.out.println(Regular admission);
else;
System.out.println(Senior admission);

will always display the Senior admission message no matter what value age has. The
accidental semicolon after the else says that if the expression is false you want to do
nothing. Logic then merges at the next statement, so the message is always displayed.



Nesting
It is permitted.
An if or an if-else can be coded within an if or an if-else.

Example:

import java.util.*;
import java.io.*;
public class Aman
{
public static void main(String[] args)
{

System.out.print(Enter age: );
Scanner Keyboard= new Scanner(System.in);
int age;
age=Keyboard.nextInt();
if (age >= 10)
{
if (age < 65)
{
System.out.println($5 admission);
}
else
{
System.out.println($4 admission);
}
}
else
{
if (age < 5)
{
System.out.println(Free admission);
}
else
{
System.out.println($2 admission);
}
}

}
}


Notes:

1. The program prompts for and reads a persons age from the user.

2. Based upon the value of age, it displays a different admission fee (under 5 is free, 5-17
is 2 dollars, 18-64 is 5 dollars, and 65 and over is 4 dollars).





Decision making, Branching and Looping
Switch statements

Flow control with if and else statements gets cumbersome when a variable must be tested
for a large number of possible values, such as a menu selection that permits the user to
enter an integer from 1 to 20. The solution to this problem is the switch statement.

The switch statement

1) Specifies an expression whose value is be tested. This is known as the switch
expression and is typically the name of a single variable.

2) Defines a number of cases, each associated with a different value. There may also
be a default case that is not associated with a value. If the value of the switch
expression is equal to the value of a case, the statements for that case will be
executed. Otherwise, the statements of the default case will be executed.

3) General syntax:

switch (expression)
{
case value1:
statements;
break;
case value2:
statements;
break;
default:
statements;
break;
}


It has a number subtleties and restrictions:

1. The switch expression must be a primitive of type int or able to be promoted to int.
Specifically, it must be either byte, short, int, or char. Expressions of type boolean,
long, float, and double will not compile.

2. The value specified on a case must be a constant of type int or must be able to be
promoted to int (in other words a byte, short, int, or char). It must also be within the
possible range of values of the switch expression. For example, if the switch variable
is a byte, a case with a value of 200 would not compile because a byte may only have
a value from -128 to 127.

The value of a case may be an expression as long as the result is a constant.

For example,
case 5 + 1:
would compile successfully.

3. The break statement is optional and will be covered in more detail in a later lesson.
When encountered, it ends the execution of the switch statement. If omitted from a
case, processing falls through to the next case (a sometimes undesirable result).

4. Although the compiler doesnt care, cases should be arranged in a high probability to
low probability order to enhance processing efficiency. The default can be placed
anywhere (even first).

It may be nested.
A switch can be coded within another switch or in either leg of an if-else.
It can also contain if-else code.

Example:

import java.util.*;
import java.io.*;
import java.lang.*;
public class Aman
{
public static void main(String[] args)
{

// Variables
// Prompt for and read data

System.out.print(Enter customers starting balance: );


Scanner Keyboard=new Scanner(System.in);
Double balance;
balance = Keyboard.readDouble();
System.out.print(Enter transaction amount: );
double amount;
amount = Keyboard.readDouble();
System.out.println(Transaction codes are);
System.out.println( + C - charge);
System.out.println( + P - payment);
System.out.println( + R - refund or return);
System.out.print(Enter transaction code: );
char code;
code = Keyboard.readChar();

// Process based upon transaction code

switch (code)
{
case C:
case c:
balance += amount;
System.out.println(New balance is + Utility.moneyFormat(balance));
break;
case P:
case p:
balance -= amount;
System.out.println(New balance is + Utility.moneyFormat(balance));
break;
case R:
case r:
balance -= amount;
System.out.println(New balance is + Utility.moneyFormat(balance));
break;
default:
System.out.println(Invalid transaction code);
break;
}
}
}
***try it with the help of under given suggestion.

Notes:

1. The balance, code, and amount variables hold data entered by the user.

2. After all data has been read from the user, the transaction is processed by the switch
statement with the transaction code (code) used as the switch expression.

3. Within the switch, if the value of code matches the value of a particular case,
processing jumps to the statement block for that case, otherwise processing jumps to
the statement block for the default. Processing will then continue until either a break
statement is encountered or the end of the switch is reached.

4. Stacking two or more cases without at break statement constitutes an OR.

For example,
case R:
case r:

will result in the same processing being performed if the value of code is either an
uppercase or lowercase R.

5. For transactions with a valid transaction code, the customers new balance is
displayed using the moneyFormat() method of my Utility class. If the transaction
code is bad, an error message is displayed.





Decision making, Branching and Looping
for, while, and do-while statements

The ability to loop by executing one or more statements repetitively is an important part
of programming.

Loops reduce the number of statements a programmer must code and result in a smaller,
more memory efficient program.

In Java, looping is performed using the for, while, and do-while statements.





Decision making, Branching and Looping
The for statement

1) It is useful when the number of passes (iterations) can be predetermined

The general syntax
for (initialization; condition; update)
{
statements;
}

where:
initialization - represents the declaration of one or more local variables of the same data
type. When loop processing is complete, all such variables are destroyed.

condition - represents a binary expression that, if true, allows the loop to continue. The
condition is tested prior to each iteration. If no longer true, processing continues at the
first statement after the closing brace of the for loop.

update - represents one or more expressions to be executed at the end of each iteration.

The braces may be omitted if the loop consists of a single statement. This would
constitute a single statement for loop.

Example 1:
Counting to 10 with a local variable
for (int i = 1; i <= 10; i++)
System.out.println(i);

This is a single statement for loop.


Local variable i is initialized to 1 before the first iteration and is used for loop control (its
value determines when the loop will end). As long as i is less than or equal to 10, looping
will continue. At the end of each iteration, i is incremented. Within each iteration, the
current value of i is displayed.

When the condition is no longer true, processing will continue at the next statement in the
program and variable i will be destroyed.

Example 2:
Counting to 10 without a local variable

Counting to 10 without a local variable
int i = 1;
for (; i <= 10; i++)
System.out.println(i);
System.out.println(Now i is + i);

Here loop control variable i is initialized outside the loop, it will not be destroyed when
the loop completes. Its final value (which will be 11) is displayed by the statement after
the loop.

Notice that when no initialization expression is coded, a place holding semicolon is still
required.

Example 3:
Coding multiple initialization and update expressions

for (int i = 1, j = 10; i <= 10; i++, j)
System.out.println(i + x + j + = + (i * j));

This loop initializes two local variables, i and j, of the same data type before the first
iteration.

At the end of each iteration, i is incremented and j is decremented. Within each iteration,
the product of i and j is displayed.

Notice that commas are used to separate multiple initialization and update expressions.
It can be nested.

For example,
the following program generates a simple 9 x 9 multiplication table:

import java.io.*;
public class Aman
{
public static void main(String[] args)
{

// This outer loop generates one row of the multiplication


// table during each iteration.

for (int row = 1; row <= 9; row++) {

// This inner loop generates one column of the current row


// of the multiplication table during each iteration.

for (int column = 1; column <= 9; column++)


{

// If a one digit number is about to be displayed, preceed it


// with four spaces. Otherwise, preceed it with three spaces.

if ((row * column) < 10)


{
System.out.print( );
}
else
{
System.out.print( );
}

// Display the number.


System.out.print((row * column));
}

// End the current line.

System.out.print( );
}
}
}




Decision making, Branching and Looping
The while statement

1) It Defines a block of code to be executed as long as a particular condition is met.
The condition is tested prior to each iteration.

It has the general syntax
while (condition)
{
statements;
}

where condition represents a binary expression that, if true, permits an iteration of the
loop. If no longer true, processing continues at the first statement after the closing brace
of the while loop.

The braces may be omitted if the loop consists of a single statement. This would
constitute a single statement while loop.

1) It does not provide for initialization of variables or automatic update expressions. In
spite of these limitations, a while loop can be used in place of nearly any for loop as
shown by these examples:

Example 1:
Counting to 10 without a local variable

int i = 1;
while (i <= 10)
{
System.out.println(i);
i++;
}

Loop control variable i is initialized outside the loop. Prior to each iteration, the value of i
is tested to determine if it is still less than or equal to 10. If so, the current value of i is
displayed and i is incremented. Otherwise processing will jump to the first statement after
the closing brace of the loop.

Example 2:
An endless loop

while (true)
System.out.println(I wont end);

This is the preferred technique for launching an endless loop.

Example 3:
A small program using nested while loops to generate a 9 x 9 multiplication table:

import java.io.*;
public class Aman
{
public static void main(String[] args)
{

// Initialize the row number.

int row = 1;

// This outer loop generates one row of the multiplication


// table during each iteration.

while (row <= 9)


{

// Initialize the column number.

int column = 1;

// This inner loop generates one column of the current row


// of the multiplication table during each iteration.

while (column <= 9)


{

// If a one digit number is about to be displayed, preceed it


// with four spaces. Otherwise, preceed it with three spaces.

if ((row * column) < 10)


{
System.out.print( );
}
else {
System.out.print( );
}

// Display the number.

System.out.print((row * column));

// Increment the column number.

column++;
}

// End the current line.

System.out.print( );

// Increment the row number.

row++;
}
}
}




Decision making, Branching and Looping
The do-while statement

1) It defines a block of code to be executed as long as a particular condition is met. The
condition is tested at the end of each iteration. It always guaranteed at least one pass
through a do-while loop.

syntax
do
{
statements;
} while (condition);

where condition represents a binary expression that, if true, permits another iteration of
the loop to be performed. If no longer true, processing continues at the next statement.

The braces may be omitted if the loop consists of a single statement. This would
constitute a single statement do-while loop.

It can be used in place of nearly any while loop as shown by these examples:

Example 1:
Counting to 10 without a local variable

int i = 1;
do
{
System.out.println(i);
i++;
} while (i <= 10);

Loop control variable i is initialized outside the loop. Within the loop, the current value of
i is displayed and i is incremented. At the end of each iteration, the value of i is tested to
determine if it is still less than or equal to 10. If so, the body of the loop is repeated.
Otherwise processing will jump to the next statement.

Example 2:
An endless loop

do
{
System.out.println(I wont end);
} while(true);

Example 3:
A small program using nested do-while loops to generate a 9 x 9 multiplication table

import java.io.*;
public class Aman
{
public static void main(String[] args)
{

// Initialize the row number.

int row = 1;

// This outer loop generates one row of the multiplication


// table during each iteration.

do
{

// Initialize the column number.

int column = 1;

// This inner loop generates one column of the current row


// of the multiplication table during each iteration.

do
{

// If a one digit number is about to be displayed, preceed it


// with four spaces. Otherwise, preceed it with three spaces.

if ((row * column) < 10)


{
System.out.print( );
}
else
{
System.out.print( );
}

// Display the number.

System.out.print((row * column));

// Increment the column number.

column++;
} while (column <= 9);

// End the current line.

System.out.print( );

// Increment the row number.

row++;
}
while (row <= 9);
}
}




Decision making, Branching and Looping
Using break and continue

At times, it is desirable to exit from a loop or short-circuit the current iteration of a loop.
The break and continue statements make it happen.






Decision making, Branching and Looping
The break statement

1) Can only appear within a loop or a switch statement.

2) Causes an immediate exit from a loop structure before the test condition is met. Once a
break statement is encountered, the loop immediately terminates, skipping any
remaining code.

For example,
for (int x = 0; x < 10; x++)
{
if (x == 5)
break;
else
System.out.print( + x);
}

will end prematurely when x takes on a value of 5.

The output displayed will be:
0 1 2 3 4

3) It may specify the label of the loop to be abandoned. This makes it possible to abandon
a specific loop.

For example,

outer: for (int i = 1; i <= 5; i++)
{
for (int j = 5; j >= 1; j) {
if (i == j)
break outer;
else
System.out.println(i = + i + , j = + j);
}
}

will end the outer loop when i equals j.

The output displayed will be:
i = 1, j = 5
i = 1, j = 4
i = 1, j = 3
i = 1, j = 2

4) It causes an immediate exit from a switch, skipping any remaining code.

For example, if key is a char variable having a value entered by the user, the following
statements will display whether they entered an A or a B:

switch (key)
{
case a:
case A:
System.out.println(You entered an A);
break;
case b:
case B:
System.out.println(You entered a B);
break;
default:
System.out.println(You did not enter an A or a B);
break;
}






Decision making, Branching and Looping

The continue statement

1) Can only appear within a loop.
2) Skips all remaining statements within the loop and resumes with the next iteration.

For example,
for (int x = 0; x < 10; x++)
{
if (x == 5)
continue;
else
System.out.print( + x);
}

will short-circuit when x takes on a value of 5.

The output displayed will be:
0 1 2 3 4 6 7 8 9

3)It may specify the label of the loop to be continued. This makes it possible to skip to
the next iteration of the specified loop.

For example,
outer: for (int i = 1; i <= 5; i++)
{
for (int j = 5; j >= 1; j)
{
if (i == j)
continue outer;
else
System.out.println(i = + i + , j = + j);
}
}

will short-circuit to the next iteration of the loop labeled outer when i equals j.

The output displayed will be:
i = 1, j = 5
i = 1, j = 4
i = 1, j = 3
i = 1, j = 2
i = 2, j = 5
i = 2, j = 4
i = 2, j = 3
i = 3, j = 5
i = 3, j = 4
i = 4, j = 5






Class Object and Method
Introduction to Classes

Definition:
A class is a blueprint or prototype that defines the variables and methods common to all
objects of a certain kind.






Class Object and Method
The Benefit of Classes

Objects provide the benefit of modularity and information hiding. Classes provide the
benefit of reusability. Software programmers use the same class, and thus the same code,
over and over again to create many objects.






Class Object and Method
Defining Classes

To define a class, we use the class keyword and the name of the class:


class MyClassName
{

}

By default, classes inherit from the Object class.

If this class is a subclass of another specific class (that is, inherits from another class), use
extends to indicate the superclass of this class:

class myClassName extends mySuperClassName
{

}

Following is the code for a class called SimplePoint that represents a point in 2D space:

public class SimplePoint
{
public int x = 0;
public int y = 0;
}

This segment of code declares a class a new data type really called SimplePoint. The
SimplePoint class contains two integer member variables, x and y. The public keyword
preceding the declaration for x and y means that any other class can freely access these
two members.

We create an object from a class such as SimplePoint by instantiating the class. When we
create a new SimplePoint object space is allocated for the object and its members x and y.
In addition, the x and y members inside the object are initialized to 0 because of the
assignment statements in the declarations of these two members.


Ex:
public class SimpleRectangle
{
public int width = 0;
public int height = 0;
public SimplePoint origin = new SimplePoint();
}

Here the segment of code declares a class SimpleRectangle that contains two integer
members, width and height.
SimpleRectangle also contains a third member, origin, whose data type is SimplePoint.
Here the class name SimplePoint is used in a variable declaration as the variables type.
We can use the name of a class anywhere we can use the name of a primitive type.

As with SimplePoint, when we create a new SimpleRectangle object, space is allocated
for the object and its members, and the members are initialized according to their
declarations.

The initialization for the origin member creates a SimplePoint object with this code: new
SimplePoint() as illustrated here:

This diagram shows the difference between primitive types and reference types






Class Object and Method
Class using constructor

Heres a of SimpleRectangle, called Rectangle, that contains four constructors, a method
to move the rectangle, a method to compute the area of the rectangle, and a finalize
method to provide for clean up:

public class Rectangle

public int width = 0;


public int height = 0;
public Point origin;
// four constructors
public Rectangle()
{
origin = new Point(0, 0);
}

public Rectangle(Point p)
{
origin = p;
}

public Rectangle(int w, int h)


{
this(new Point(0, 0), w, h);
}

public Rectangle(Point p, int w, int h)


{
origin = p;
width = w;
height = h;
}

// a method for moving the rectangle

public void move(int x, int y)


{
origin.x = x;
origin.y = y;
}

// a method for computing the area of the rectangle

public int area()


{
return width * height;
}

// clean up!

protected void finalize() throws Throwable


{
origin = null;
super.finalize();
}
}

Here the four constructors allow for different types of initialization.






Class Object and Method
Object

An object is a distinct instance of its class (an actual occurrence). It has its own set of
variables (known as instance variables) and methods (known as instance methods).
When called, an objects instance methods automatically act upon its instance variables
Java program creates many objects from a variety of classes.

These objects interact with one another by sending each other messages. Through these
object interactions, a Java program can implement a GUI, run an animation, or send and
receive information over a network. Once an object has completed the work for which it
was created, it is garbage-collected and its resources are recycled for use by other objects.


Creating Objects
In Java, we create an object by creating an instance of a class or, in other words,
instantiating a class.

Ex:
Rectangle rect = new Rectangle();


The above single statement performs three actions:

1. Declaration: Rectangle rect is a variable declaration that declares to the compiler that
the name rect will be used to refer to a Rectangle object. Here a class name is used as
the variables type.

2. Instantiation: new is a Java operator that creates the new object (allocates space for it).

3. Initialization: Rectangle() is a call to Rectangles constructor, which initializes the
object.





Class Object and Method
Declaring an Object

Like other variable declarations, object declarations can also appear alone, like this:

Rectangle rect;
Declarations notify the compiler that you will use name to refer to a variable whose type
is type. Declarations do not create new objects. Rectangle rect does not create a new
Rectangle object, just a variable named rect to hold a Rectangle object.

To create a Rectangle object, or any other object, use the new operator.









Class Object and Method
Instantiating an Object

The new operator instantiates a class by allocating memory for a new object of that type.
new requires a single, postfix argument: a call to a constructor.

Each Java class provides a set of constructors used to initialize new objects of that type.
The new operator creates the object, and the constructor initializes it.

Heres an example of using the new operator to create a Rectangle object:
new Rectangle(100, 200);


Here, Rectangle(100, 200) is the argument to new. The new operator returns a reference
to the newly created object.

This reference can be assigned to a variable of the appropriate type, as here.
Rectangle rect = new Rectangle(100, 200);






Class Object and Method
Initializing an Object

The classes can provide one or more constructors to initialize a new object of that type.
We can recognize a classs constructors because they have the same name as the class and
have no return type.

Here are the declarations for Rectangles constructors:
public Rectangle(Point p)
public Rectangle(int w, int h)
public Rectangle(Point p, int w, int h)
public Rectangle()

If a class has multiple constructors, they all have the same name but a different number of
arguments or different typed arguments.

The compiler differentiates the constructors, and knows which one to call, depending on
the arguments.


Using Objects
Objects give us two ways to do these things:

1. Manipulate or inspect its variables.
2. Call its methods.


Java provides an access control mechanism whereby classes can restrict or allow access
to its variables and methods.

A class should protect variables against direct manipulation by other objects if those
manipulations could endanger the objects state. State changes should then be affected
and therefore controlled by method calls.






Class Object and Method
Referencing an Objects Variables

Assume we create a rectangle named rect as described in the previous section. To move
rect to a new location,
we would write:
rect.origin = new Point(15, 30);

This statement moves the rectangle by setting its point of origin to a new position.
rect.origin is the name of rects origin variable.

The Rectangle class has two other variables width and height that are accessible to
objects outside of the class. We can use the same notation to access them and calculate
the rectangles area using this statement
area = rect.height * rect.width;;

In general, to refer to an objects variables, append the name of the variable to an object
reference with an intervening period (.):
objectReference.variable

The first part of the variables name, objectReference, must be a reference to an object.






Class Object and Method
Calling an Objects Methods

To move rect to a new location using its move method, we write this:
rect.move(15, 30);

This Java statement calls rects move method with two integer parameters, 15 and 30. It
moves the rect object because the rect method assigns new values to origin.x and
origin.y, and is equivalent to the assignment statement used previously:
rect.origin = new Point(15, 30);


The notation used to call an objects method is similar to that used when referring to its
variables: we append the method name to an object reference with an intervening period
(.). Also, we provide any arguments to the method within enclosing parentheses. If the
method does not require any arguments, use empty parentheses.

objectReference.methodName(argumentList);
or
objectReference.methodName();





Class Object and Method
Creating Classes

The diagram lists the class and identifies the structure of the code.






Class Object and Method

The Class Declaration


public
The public modifier declares that the class can be used by any class regardless of its
package.

abstract
Declares that the class cannot be instantiated.

final
Declares that the class cannot be subclassed.

class Name Of Class
The class keyword indicates to the compiler that this is a class declaration and that the
name of the class is NameOfClass.

extends Super
The extends clause identifies Super as the superclass of the class, thereby inserting the
class within the class hierarchy

implements Interfaces
To declare that our class implements one or more interfaces, use the keyword implements
followed by a comma-delimited list of the names of the interfaces implemented by the
class





Class Object and Method

The Class Body

The class body contains all of the code that provides for the life cycle of the objects
created from it:

1. constructors for initializing new objects,

2. declarations for the variables that provide the state of the class and its objects,

3. methods to implement the behavior of the class and its objects,

4. And a finalize method to provide for cleaning up an object after it has done its job.



Variables and methods collectively are called members.
Constructors are not methods. Nor are they members.





Class Object and Method

Constructors for Classes

All Java classes have constructors that are used to initialize a new object of that type.
A constructor has the same name as the class.

For example,
The name of the Stack classs constructor is Stack, the name of the Rectangle classs
constructor is Rectangle, and the name of the Thread classs constructor is Thread. Stack
defines a single constructor:

public Stack()
{
items = new Vector(10);
}

A constructor uses its arguments to initialize the new objects state. When creating an
object, choose the constructor whose arguments best reflect how we want to initialize the
new object.
When declaring constructors for our class, we use the following access specifiers in the
constructor declaration to specify what other objects can create instances of our class:


private
No other class can instantiate our class. Our class may contain public class methods
(sometimes called factory methods), and those methods can construct an object and return
it, but no other classes can.


protected
Only subclasses of our class can create instances of it.


public
Any class can create an instance of our class.


package
Only classes within the same package as our class can construct an instance of it.
Constructors provide a way to initialize a new object.






Class Object and Method

Implementing Methods

Methods

1. Are named modules of code

2. May optionally receive one or more parameters (arguments)

3. May optionally return a single value

4. Are associated with either a class or an object of a class. The former are called class
methods and are the focus of this lesson. The latter are called instance methods and
will be covered in a later lesson.


Calling a class method
Involves the following general syntax for the call expression:
class-name.method-name(arguments)

If the method is defined within the current class, the class name may be omitted and the
general syntax for the call expression is:
method-name(arguments)


Can occur anywhere the returned data type makes sense. Methods that return no value
are called in a stand-alone statement. Methods that return a value are called within
another expression.

This method pushes an object, the one passed in as an argument, onto the top of the stack,
and returns it.



Like a class, a method has two major parts:
1. method declaration
2. And method body.

The method declaration defines all of the methods attributes, such as access level, return
type, name, and arguments, as illustrated here:



The method body is where all the action takes place. It contains the Java instructions that
implement the method.


The Method Declaration
At minimum, a method declaration has a name and a return type indicating the data type
of the value returned by the method:

returnType methodName()
{

}



Class Object and Method

The Method Body

The method body is where all of the action of a method takes place; the method body
contains all of the legal Java instructions that implement the method.


Returning a Value from a Method
We declare a methods return type in its method declaration. Within the body of the
method, we use the return operator to return the value. Any method that is not declared
void must contain a return statement. The Stack class declares the isEmpty method,
which returns a boolean:

public boolean isEmpty()
{
if (items.size() == 0)
return true;
else
return false;
}


The data type of the return value must match the methods return type; we cant return an
Object type from a method declared to return an integer.
The isEmpty method returns either the boolean value true or false, depending on the
outcome of a test.

The isEmpty method returns a primitive type.
Methods also can return a reference type.

For example,
Stack declares the pop method that returns the Object reference type:

public synchronized Object pop()
{
int len = items.size();
Object obj = null;
if (len == 0)
throw new EmptyStackException();
obj = items.elementAt(len - 1);
items.removeElementAt(len - 1);
return obj;
}



Class Object and Method

A Methods Name

Java supports method name overloading so that multiple methods can share the
same name.
For example

class DataRenderer
{
void draw(String s)
{

}

void draw(int i)
{

}

void draw(float f)
{

}
}


Overloaded methods are differentiated by the number and type of the arguments passed
into the method.
In the code sample, draw(String s) and draw(int i) are distinct and unique methods
because they require different argument types.
We cannot declare more than one method with the same name and the same number and
type of arguments because the compiler cannot differentiate them. So, draw(String s)
and draw(String t) are identical and result in a compiler error.

A class may override a method in its superclass. The overriding method must have the
same name, return type, and parameter list as the method it overrides.



Class Object and Method

Example of the Math class

Different methods in the Math Class:



Sample programs
The following small samples will help us understand how easy it is to use the class methods of
the Math class:

Example 1:
Performing some miscellaneous mathematical calculations on a number

public class AppMath
{
public static void main(String[] args)
{

double x;
char again;

do
{
Utility.separator(50, ~);
System.out.print(Enter a number: );
x = Keyboard.readDouble();
Utility.skip();
System.out.println( Ceiling = + Math.ceil(x));
System.out.println( Floor = + Math.floor(x));
System.out.println( Rounded = + Math.round(x));
System.out.println( Squared = + Math.pow(x, 2));
System.out.println( Square root = + Math.sqrt(x));
Utility.skip();
System.out.print(Again? (Y/N): );
again = Keyboard.readChar();
}
while (again == Y || again == y);
}
}



Array and String

Arrays

Introduction
An array is a collection of items. Each slot in the array can hold an object or a
primitive value. Arrays in Java are objects that can be treated just like other objects in
the language.
An array is an indexed table of like values. While they can exist in more than one
dimension, we are only interested in one-dimensional arrays as depicted by the following:



This array consists of five elements where each is an integer value (217, 138, 92, 12, and
168).


To access a particular element within an array, its corresponding index (offset) is used.
For example, the integer value at index location 3 within this array is 12. Notice that the
indexes are zero based because the offset of the first element is always zero (you dont
have to skip over any other elements to get there).

While all programming languages include the ability to process arrays, Java provides
enhanced integrity and other features not found in other languages.



Array and String

Java arrays

Java array require that all elements be of the same data type.

For example,
We may create an array of char values, or an array of float values, or an array of boolean
values, etc


Java array are objects and must be declared like one.
The general syntax is as follows:

data-type[] identifier = new data-type[n];
or
data-type identifier[] = new data-type[n];

Where identifier is the array (object) reference and n is an integer expression that
specifies how many elements are in the array.

For example,
int[] myArray = new int[5];

Alternatively, a programmer may split the definition of the array reference and the
instantiation of the array into two statements as follows:

int[] myArray;
myArray = new int[5];


Using either technique, it is important to understand what is created.
The following diagram may help:


The array reference (myArray) can be thought of as pointing to the array whose elements
reside on the memory heap.

Notes:
1. At the time an array reference is declared, it initially contains null (it doesnt point to
anything). It doesnt receive a value until an array is assigned to it.

2. An array reference can only be assigned an array of the correct type. In the above
example, myArray is declared to be a reference to an int array, so only an array of int
values can ever be assigned to it. To attempt otherwise will result in a compile error.

3. An array reference may be reused to point to a different array (of the appropriate type).
Like all objects, however, an array that can no longer be r eferenced will be garbage
collected.

4. When instantiated, all array elements are automatically initialized to binary zeros. This
means all elements of numeric arrays will be zero, all elements of boolean arrays will
be false, and all elements of char arrays will be the null character.


Java array can be constructed from a value list.
This is an alternative construction technique as shown by
char[] chars = {a, b, c};

This creates a three element array of characters. The first element is initialized to a, the
second is initialized to b, and the third is initialized to c.

Notes:
1. The number of elements in the array is determined by the number of values in the list
and the new keyword isnt coded.

2. When a value list is used, it can only be coded in the statement that declares the array
reference.


In other words, the following will not compile:
char[] chars;
chars = {a, b, c};


Java array permit an element to be accessed via its unique index.
For example,
int[] numbers = {12, 15, 3, 8};


The following expression will reference the third element (an int with a value of 3)
numbers[2]

Notes:
1. An index must be int or able to be widened to int to avoid a compile error. It may not
be boolean, long, float, or double.

2. If an index is negative of exceeds the maximum valid index for the array, a runtime
error occurs. The JVM will throw an ArrayIndexOutOfBoundsException. Catching and
processing such exceptions is a topic in advanced Java.


Java array have a publicly available length field. This is an int constant representing the
number of elements within the array. It is extremely useful when coding a loop to process
all the elements within an array.

For example,
The following small program creates an array, loads it with some random numbers, and
displays the arrays contents:

public class AppArr
{
public static void main(String[] args)
{
double[] values = new double[10];
for (int i = 0; i < values.length; i++)
{
values[i] = Math.random();
}
for (int i = 0; i < values.length; i++)
{
System.out.println(values[i]);
}
}
}


Notes:
1. In an array object, length is a field and NOT a method. A common mistake is to code
length() as you would with a String or StringBuffer object and get a compile error.

2. Because the for loops are limited by the length of the array, the code is very flexible.
Changing the number of elements in the array declaration is all that is needed to work
with a different sized array.



Array and String

Object arrays

Object array are declared with the class name as the type.

For example
String[] names = new String[5];


Constructs an array of 5 String object references. It is important to understand that no
String objects are created by this declaration. We have simply created an array of null
object references which may later be assigned to individual String objects (as shown by
the following diagram).


The statement
names[3] = new String(Hello World!);

would instantiate a String object and assign it to the fourth element in the names array
(which would look like the following).


To display the value of this particular String object, one might code
System.out.println(names[3]);

which retrieves the String object referenced by the fourth element in the names array.

Object array are arrays of object references. Each element is either the reference of an
instantiated object or is null. The general syntax to access an instance method of the
object being referenced is
array-identifier[index].method-name(arguments)

For example,
To determine the length of the encapsulated string in the fourth element of the names
array one would code the expression
names[3].length()

Object array can result in runtime errors if improperly used. In addition to an
ArrayIndexOutOfBoundsException, it is possible for a NullPointerException to occur if
an attempt is made to reference an object when the object reference is null.

Example:
Attempting to call an instance method of an object that doesnt exist.

public class AppObj
{
public static void main(String[] args)
{
String[] names = new String[5];
System.out.println(Length of first string: + names[0].length());
}
}


Object array can be constructed from a value list. This requires the instantiation or the
existence of the objects to be referenced by each array element.

For example,
String aString = new String(def);
String[] x = {new String(abc), aString, xyz};


creates a three element array of String object references. The first element references
a String object having the value abc , the second element references a String object
having the value def , and the third references the String object in the literal pool
having the value xyz .



Array and String

Strings

A sequence of character data is called a string and is implemented in the Java environment
by the String class (a member of the java.lang package). The character-counting program
uses Strings in two different places. The first is in the definition of the main() method:
String[] args

This code explicitly declares an array, named args, that contains String objects. The empty
brackets indicate that the length of the array is unknown at compilation time because the
array is passed in at runtime.

The second use of Strings in the example program is these two uses of literal strings
(a string of characters between double quotation marks and ):

Input has

chars.

The compiler implicitly allocates a String object when it encounters a literal string. So, the
program implicitly allocates two String objects one for each of the two literal strings
shown previously.
String String[] arrayOfStrings = new String[10];

The elements in this array are reference types, that is, each element contains a reference to
a String object.


At this point, enough memory has been allocated to contain the String references, but no
memory has been allocated for the Strings themselves. If we attempted to access one of
arrayOfStrings elements at this point, we would get a NullPointerException because the
array is empty and contains no Strings and no String objects.

We have to allocate the actual String objects separately:
for (int i = 0; i < arrayOfStrings.length; i ++)
{
arrayOfStrings[i] = new String(Hello + i);

objects are immutablethat is, they cannot be changed once theyve been created. The
java.lang package provides a different class, StringBuffer, which we can use to create and
manipulate character data on the fly.



Array and String

String Concatenation

Java lets us concatenate strings together easily using the + operator. The example program
uses this feature of the Java language to print its output. The following code snippet
concatenates three strings together to produce its output:
Input has + count + chars.


Two of the strings concatenated together are literal strings: Input has and chars. The
third stringthe one in the middleis actually an integer that first gets converted to a
string and then is concatenated to the others.




Inheritance

Introduction

Inheritance is a concept in object-oriented programming where all classes are arranged in
a strict hierarchy. Each class in the hierarchy has superclasses (classes above it in the
hierarchy) and any number of subclasses (classes below it in the hierarchy). Subclasses
inherit attributes and behavior from their superclasses

In Java, as in object-oriented programming languages, classes can be derived from other
classes.
The derived class (the class that is derived from another class) is called a subclass.
The class from which its derived is called the superclass.


In fact, in Java, all classes must be derived from some class.

The top-most class, the class from which all other classes are derived, is the Object class
defined in java.lang.

Object is the root of a hierarchy of classes.


The subclass inherits state and behavior in the form of variables and methods from its
superclass.
The subclass can just use the items inherited from its superclass as is, or the subclass can
modify or override it.

Definition:
A subclass is a class that derives from another class. A subclass inherits state and behavior
from all of its ancestors.




Inheritance

Creating Subclasses

For example,
suppose that we wanted to create a subclass named SubClass of another class named
SuperClass. We would write:

class SubClass extends SuperClass
{

}


This declares that SubClass is the subclass of the Superclass class. It also implicitly
declares that SuperClass is the superclass of SubClass. A subclass also inherits variables
and methods from its superclasss superclass, and so on up the inheritance tree.

A Java class can have only one direct superclass.
Java does not support multiple inheritance.

Creating a subclass can be as simple as including the extends clause in your class
declaration.



Inheritance

Member Variables In Subclass Inherit?

Rule:
A subclass inherits all of the member variables within its superclass that are accessible to
that subclass (unless the member variable is hidden by the subclass).

That is, subclasses
1. inherit those member variables declared as public or protected

2. inherit those member variables declared with no access specifier as long as the subclass
is in the same package as the superclass. These variables are sometimes known as
friendly.

3. dont inherit a superclasss member variable if the subclass declares a member variable
using the same name. The subclasss member variable is said to hide the member
variable in the superclass.

4. dont inherit private member variables


Inheritance

Hiding Member Variables

The member variables defined in the subclass hide member variables of the same name in
the superclass.
One interesting feature of Java member variables is that a class can access a hidden
member variable through its superclass.

Consider this superclass and subclass pair:

class Super
{
Number aNumber;
}
class Sub extends Super
{
Float aNumber;
}

The aNumber variable in Sub hides aNumber in Super. But we can access aNumber from
the superclass with:

super.aNumber
super is a Java language keyword that allows a method to refer to hidden variables and
overriden methods of the superclass.



Inheritance

Methods In Subclass Inherit?

The rule that specifies which methods get inherited by a subclass is similar to that for
member variables.

Rule:
A subclass inherits all of the methods within its superclass that are accessible to that
subclass (unless the method is overriden by the subclass).


That is, subclasses
1. inherit those methods declared as public or protected

2. inherit those methods declared with no access specifier as long as the subclass is in the
same package as the superclass

3. dont inherit a superclasss method if the subclass declares a method using the same
name. The method in the subclass is said to override the one in the superclass.

4. dont inherit private methods




Inheritance

Overriding Methods

A subclass can either completely override the implementation for an inherited method or
the subclass can enhance the method by adding functionality to it.









Inheritance

Methods a Subclass Cannot Override

A subclass cannot override methods that are declared final in the superclass (by
definition, final methods cannot be overriden).

If you attempt to override a final method, the compiler will display an error message
similar to this one and refuse to compile the program:

FinalTest.java:7: Final methods cant be overriden. Method void iamfinal() is final in
class ClassWithFinalMethod.
void iamfinal() { ^ 1 error




Inheritance

Methods a Subclass Must Override

Subclass must override methods that are declared abstract in the superclass, or the
subclass itself must be abstract.


ABSTRACT CLASSES
Abstract classes are those which can be used for creation of objects. However their
methods and constructors can be used by the child or extended class. The need for
abstract classes is that you can generalize the super class from which child classes can
share its methods. The subclass of an abstract class which can create an object is called as
concrete class.

Following is an example

Abstract class A
{
abstract void method1();
void method2()
{
System.out.println(this is concrete method);
}
}
class B extends A
{
void method1()
{
System.out.println(B is implementation of method1);
}
}
class demo
{
public static void main(String arg[])
{
B b=new B();
b.method1();
b.method2();
}
}



Inheritance

The Benefits of Inheritance

Subclasses provide specialized behaviors from the basis of common elements provided by
the superclass. Through the use of inheritance, programmers can reuse the code in the
superclass many times.

Programmers can implement superclasses called abstract classes that define generic
behaviors. The abstract superclass defines and may partially implement the behavior but
much of the class is undefined and unimplemented. Other programmers fill in the details
with specialized subclasses.





Inheritance

Interfaces

An interface is a collection of method names, without definitions, that can be added to
classes to provide additional behavior not included with those methods the class defined
itself or inherited from its superclasses.

The problem with multiple inheritance is that it makes a programming language far more
complex to learn, to use, and to implement.

A Java interface is a collection of abstract behavior that can be mixed into any class to
add to that class behavior that is not supplied by its superclasses. Specifically, a Java
interface contains nothing but abstract method definitions and constants-no instance
variables and no method implementations.

Interfaces are implemented and used throughout the Java class library whenever a
behavior is expected to be implemented by a number of disparate classes.

The Java class hierarchy, for example, defines and uses the interfaces
java.lang.Runnable

java.util.Enumeration,
java.util.Observable,
java.awt.image.ImageConsumer, and java.awt.image.ImageProducer.



Inheritance

Interfaces and Classes

Interfaces, like classes, are declared in source files, one interface to a file. Like classes,
they also are compiled using the Java compiler into .class files.

Interfaces complement and extend the power of classes, and the two can be treated almost
exactly the same.

One of the few differences between them is that an interface cannot be instantiated: new
can only create an instance of a class.






Inheritance

Implementing and Using Interfaces

The implements Keyword
To use an interface, we include the implements keyword as part of our class definition

// java.applet.Applet is the superclass

public class Neko extends java.applet.Applet

implements Runnable
{ // but it also has Runnable behavior

}


After our class implements an interface, subclasses of our class will inherit those new
methods (and can override or overload them) just as if our superclass had actually defined
them.

If your class inherits from a superclass that implements a given interface, we dont have
to include the implements keyword in your own class definition.

Example-

interface Fruitlike
{
void decay();
void squish();

}

class Fruit implements Fruitlike


{
private Color myColor;
private int daysTilIRot;

}

interface Spherelike
{
void toss();
void rotate();

}

class Orange extends Fruit implements Spherelike


{
// toss()ing may squish() me (unique to me)
}

Here the class Orange doesnt have to say implements Fruitlike because, by extending
Fruit, it already has!

Other Example-
class Sphere implements Spherelike
{ // extends Object
private float radius;

}

class Orange extends Sphere implements Fruitlike


{
. .// users of Orange never need know about the change!
}



Inheritance

Implementing Multiple Interfaces

To include multiple interfaces in a class, just separate their names with commas:

public class Neko extends java.applet.Applet

implements Runnable, Eatable, Sortable, Observable


{

}





Inheritance

Creating and Extending Interfaces

New Interfaces

To create a new interface,we declare it like this:
public interface Growable
{

}

This is, effectively, the same as a class definition, with the word interface replacing the
word class. Inside the interface definition we have methods and constants.

The method definitions inside the interface are public and abstract methods; We cannot
declare a method inside an interface to be either private or protected.

So, for example, heres a Growable interface with one method explicitly declared public
and abstract (growIt()) and one implicitly declared as such (growItBigger())

public interface Growable
{
public abstract void growIt(); //explicity public and abstract
void growItBigger(); // effectively public and abstract
}


In addition to methods, interfaces can also have variables, but those variables must be
declared public, static, and final (making them constant).
As with methods, we can explicitly define a variable to be public, static, and final.

Heres that same Growable definition with two new variables:
public interface Growable
{
public static final int increment = 10;
long maxnum = 1000000; // becomes public static and final

public abstract void growIt(); //explicitly public and abstract


void growItBigger(); // effectively public and abstract
}

Interfaces, like classes, can belong to a package by adding a package statement to the first
line of the class file.
Interfaces can also import other interfaces and classes from other packages, just as classes
can.




Inheritance

Methods Inside Interfaces

An actual implementation for the method in a class:

public class Orange extends Fruit
{

public germinate(Fruitlike self)


{
Orange theOrange = (Orange)self;

}
}



Inheritance

Extending Interfaces

When one interface inherits from another interface, that subinterface acquires all the
method definitions and constants that its superinterface defined.

To extend an interface, we use the extends keyword just as you do in a class definition:
public interface Fruitlike extends Foodlike
{

}


In multiply inherited interfaces, the rules for managing method name conflicts are the
same as for classes that use multiple interfaces; methods that differ only in return type will
result in a compiler error.



Package

Introduction

Java provides a powerful means of grouping related classes and interfaces together in a
single unit: packages

Packages are groups of related classes and interfaces.
Packages provide a convenient mechanism for managing a large group of classes and
interfaces while avoiding potential naming conflicts.

Packages are useful for several broad reasons:
They allow us to organize your classes into units. Just as we have folders or directories
on your hard disk to organize your files and applications, packages allow us to organize
your classes into groups so that we only use what we need for each program.

They reduce problems with conflicts in names. As the number of Java classes grows, so
does the likelihood that well use the same class name as someone else, opening up the
possibility of naming clashes and errors if you try to integrate groups of classes into a
single program. Packages allow you to hide classes so that conflicts can be avoided.

They allow you to protect classes, variables, and methods in larger ways than on a
class-by-class basis, as you learned yesterday. well learn more about protections with
packages later today.

They can be used to identify your classes. For example, if you implemented a set of
classes to perform some purpose, we could name a package of those classes with a
unique identifier that identifies you or your organization.

The Java API itself is implemented as a group of packages.



Package

Declaring Packages

The syntax for the package statement:
package Identifier;

This statement must be placed at the beginning of a compilation unit (a single source file),
before any class declarations.
Every class located in a compilation unit with a package statement is considered part of
that package

Packages can be nested within other packages. When this is done, the Java interpreter
expects the directory structure containing the executable classes to match the package
hierarchy.



Package

Importing Packages

The import statement enables us to import classes from other packages into a compilation
unit. We can import individual classes or entire packages of classes at the same time if we
want.

The syntax for the import statement follows:
import Identifier;

Identifier is the name of the class or package of classes we are importing.

Other Example:
import java.awt.Color;
import java.awt.*;

The first statement imports the specific class Color, which is located in the java.awt
package. The second statement imports all the classes in the java.awt package.

Note that the following statement doesnt work:
import java.*;

This statement doesnt work because we cant import nested packages with the *
specification. This wildcard works only when importing all the classes in a particular
package, which is still very useful.



There is one other way to import objects from other packages: explicit package
referencing.

By explicitly referencing the package name each time we use an object, we can avoid
using an import statement. Using this technique, the declaration of the color member
variable look like this:
java.awt.Color color;



Package

Creating Our Own Packages

The first step is to decide what the name of our package is going to be.
The name we choose for our package depends on how we are going to be using those
classes.

Step two in creating packages is to create a directory structure on our disk that matches
the package name.

If your package has just one name (mypackage), well only have to create a directory for
that one name.

If the package name has several parts, however, well have to create directories within
directories.

For the package name edu.nonsense.eng.fooblitzky, well need to create an edu directory
and then create a nonsense directory inside edu, an eng directory inside nonsense, and a
fooblitzky directory inside eng. Our classes and source files can then go inside the
fooblitzky directory.



Use package to Add Our Class to a Package

The final step to putting our class inside packages is to add the package command to our
source files.
The package command says this class goes inside this package, and is used like this:

package myclasses;
package edu.nonsense.eng.fooblitzky;
package java.awt;



Package

The Java Language Package

The Different Java Packages
Eight packages comprise the standard Java development environment.

The Java language package, also known as java.lang, contains classes that are core to the
Java language. The classes in this package are grouped as follow:


Object
The grand-daddy of all classesthe class from which all others inherit.


Data Type Wrappers
A collection of classes used to wrap variables of a primitive data type: Boolean,
Character, Double, Float, Integer and Long. Each of these classes are subclasses of the
abstract class Number.


Strings
Two classes that implement character data. is a thorough lesson on the use of both types
of strings.


System and Runtime
These two classes provide let your programs use system resources. System provides a
system-independent programming interface to system resources and Runtime gives you
direct system-specific access to the runtime environment.


Threads
The Thread, ThreadDeath and ThreadGroup classes implement the multi-threading
capabilities so important to the Java language. The java.lang package also defines the
Runnable interface. Runnable makes it convenient for Java class to be active without
subclassing the Thread class.


Classes
The Class class provides a runtime description of a class and the ClassLoader class
allows you to load classes into your program during runtime.


Math
A library of math routines and values such as pi.


Exceptions, Errors and Throwable
When an error occurs in a Java program, the program throws an object which indicates
what the problem was and the state of the interpreter when the error occurred. Only
objects that derive from the Throwable class can be thrown. There are two main
subclasses of Throwable: Exception and Error. Exceptions are a form of Throwable that
normal programs may try to catch. Errors are used for more catastophic errorsnormal
programs should not catch errors. The java.lang package contains the Throwable,
Exception and Error classes, and numerous subclasses of Exception and Error that
represent specific problems.


Processes
Process objects represent the system process that is created when you use Runtime to
execute system commands. The java.lang packages defines and implements the generic
Process class.


The compiler automatically imports this package for us. No other packages are
automatically imported.



Package

The Java I/O Package

The Java I/O Package (java.io) provides a set of input and output streams used to read and
write data to files or other input and output sources.









Package

The Java Utility Package

This Java package, java.util, contains a collection of utility classes. Among them are
several generic data structures (Dictionary, Stack, Vector, Hashtable) a useful object for
tokenizing a string and another for manipulating calendar dates. The java.util package also
contains the Observer interface and Observable class which allow objects to notify one
another when they change. The java.util classes arent covered separately in this tutorial
although some examples use these classes.








Package

The Java Networking Package

The java.net package contains classes and interface definitions that implement various
networking capabilities. The classes in this package include a class that implement a URL,
a connection to a URL, a socket connection, and a datagram packet. You can use these
classes to implement client-server applications and other networking communication
applications.







Package

The Applet Package

This package contains the Applet class the class that you must subclass if youre
writing an applet. Included in this package is the AudioClip interface which provides a
very high level abstraction of audio.








Package

The Abstract Window Toolkit Packages

Three packages comprise the Abstract Window Toolkit: java.awt, java.awt.image, and
java.awt.peer.

AWT Package
The java.awt package provides GUI elements used to get input from and display
information to the user such as windows, buttons, scrollbars, and text items.


AWT Image Package
The java.awt.image package contains classes and interfaces for managing image data,
such as setting the color model, cropping, color filtering, setting pixel values, and
grabbing snapshots of the screen.


AWT Peer Package
The java.awt.peer package contains classes and interfaces that connect platform-
independent AWT components to their platform-dependent implementation (such as
Motif widgets or Microsoft Windows controls).



Multithreading

Introduction:

Multithreading is a fundamental feature of the Java language specification. The creators
of Java understood the importance of multithreading and provided easy to use features for
application developers. Two of these features, the Thread class and the Runnable
interface, will be covered shortly.

Multithreading is not the same as multiprocessing. The latter involves the use of two or
more processors (CPUs). Multithreading involves the sharing of a single processor and
the interleaving of CPU time as shown by the following diagram:


Because there is only one processor, only one instruction can be executed at a time. In a
Java program, the decision as to which thread is currently executing is determined by the
JVM.

Multithreading is used to execute even the simplest Java program. Even if a program
doesnt create its own threads, the Java Virtual Machine creates multiple threads to
support the program. One thread performs the processing of the main() method. Other
threads manage and monitor system resources. The garbage collector, for example, is
always running as a low-priority thread.



Multithreading

Thread

A threadsometimes known as an execution context or a lightweight processis a
single sequential flow of control within a process.


Definition:
A thread is a single sequential flow of control within a program

Example:
The following program is a simple Java application that creates and starts two
independent threads:

class TwoThreadsTest
{
public static void main (String[] args)

{
new SimpleThread(Jamaica).start();
new SimpleThread(Fiji).start();
}
}

class SimpleThread extends Thread


{
public SimpleThread(String str)

{
super(str);
}
public void run() {
for (int i = 0; i < 10; i++)
{
System.out.println(i + + getName());
try

{
sleep((int)(Math.random() * 1000));
}
catch (InterruptedException e)
{

}
}
System.out.println(DONE! + getName());
}
}


A thread is similar to the sequential programs described above: a single thread also has a
beginning, an end, a sequence, and at any given time during the runtime of the thread
there is a single point of execution.

The HotJava Web browser is an example of a multithreaded application. Within the
HotJava browser we can scroll a page while its downloading an applet or image, play
animation and sound concurrently, print a page in the background while we download a
new page, or watch three sorting algorithms race to the finish.



Multithreading

Thread Attributes

Java threads are implemented by the Thread class, which is part of the java.lang package.
The Thread class implements a system independent definition of Java threads. But under the
hood, the actual implementation of concurrent operation is provided by a system-specific
implementation.

The life of a thread
In Java, all threads are objects that are constructed, pass between several states, and die in
keeping with the following diagram:



1. When initially constructed, the thread is New

2. When all resources the thread requires are available, it enters the Ready state. The thread
is ready to use the CPU.

1. When selected by the JVM for processing, the thread enters the Running state. This is the
state that all threads aspire to but only one is ever running at an instant in time.

2. When the thread requests to sleep for an interval of time, must wait for a resource (such as
completion of I/O), or is suspended by the JVM, it becomes Blocked. It gives up the CPU
and will return to the Ready state when the block is removed.

3. When the thread completes its processing or is killed by the JVM, it enters the Dead state.
The thread object will still exist, but will no longer be allowed to use the CPU.


Thread Body
All of the action takes place in the threads bodythe threads run() method. We can
provide the body to a Thread in one of two ways:
a. by subclassing the Thread class and overriding its run() method,
b. by creating a Thread with a Runnable object as its target.


Creating the Thread
The application in which an applet is running calls the applets start() method when the user
visits the applets page.
The Clock applet creates a Thread, clockThread, in its start() method and starts the thread.

public void start()
{
if (clockThread == null)
{
clockThread = new Thread(this, Clock);
clockThread.start();
}
}

First, the start() method checks to see if clockThread is null. If clockThread is null, then the
applet has just been loaded or has been previously stopped and a new thread must be created.

Otherwise, the applet is already running. The applet creates a new thread with this invocation:
clockThread = new Thread(this, Clock);

Herethe Clock appletis the first argument to the thread constructor. The first argument to
this Thread constructor must implement the Runnable interface and becomes the threads
target. When constructed in this way, the clock thread gets its run() method from its target
Runnable objectin this case, the Clock applet.

The second argument is just a name for the thread.

After a thread has been created and initialized, the runtime system calls its run() method. The
code in the run() method implements the behavior for which the thread was created.


Stopping the Thread
When we leave the page that displays the Clock applet, the application in which the applet is
running calls the applets stop() method.
The Clocks stop() method sets the clockThread to null.
This tells the main loop in the run() method to terminate eventually resulting in the thread
stopping and being garbage collected.
public void stop()
{
clockThread = null;
}

we can use clockThread.stop() instead, which would immediately stop the clock thread.
However, the Thread classs stop() method has a sudden effect, which means that the run()
method might be in the middle of some critical operation when the thread is stopped.
For more complex run() methods, using Threads stop() method might leave the program in
an inconsistent or awkward state. For this reason, its best to avoid using the Thread classs
stop() method when possible.

If we revisit the page, the start() method is called again, and the clock starts up again with a
new thread.


The Run Method
And finally the Clocks run() method implements the heart of the Clock applet and looks like
this:

public void run()

{
// loop terminates when clockThread is set to null in stop()
while (Thread.currentThread() == clockThread)

{
repaint();

try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{

}
}
}



Multithreading

Thread State

Throughout its life, a Java thread is in one of several states. A threads state indicates what
the Thread is doing and what it is capable of doing at that time of its life: is it running? Is it
sleeping? Is it dead?


The above diagram illustrates the various states that a Java thread can be in at any point
during its life. It also illustrates which method calls cause a transition to another State.


New Thread
The following statement creates a new thread but does not start it, thereby leaving the thread
in the New Thread state.
Thread myThread = new MyThreadClass();


When a thread is in the New Thread state, it is merely an empty Thread object. No
system resources have been allocated for it yet. Thus when a thread is in this state, we can
only start the thread or stop it. Calling any method besides start() or stop() when a thread is
in this state makes no sense and causes an IllegalThreadstateException.


Runnable
Now consider these two lines of code:
Thread myThread = new MyThreadClass();
myThread.start();

The start() method creates the system resources necessary to run the thread, schedules the
thread to run, and calls the threads run() method. At this point the thread is in the
Runnable state. This state is called Runnable rather than Running because the
thread might not actually be running when it is in this state. Many computers have a single
processor, making it impossible to run all Runnable threads at the same time. So, the Java
runtime system must implement a scheduling scheme that shares the processor between all
Runnable threads.


Not Runnable
A thread enters the Not Runnable state when one of these four events occurs:
1. Someone invokes its sleep() method.
2. Someone invokes its suspend() method.
3. The thread uses its wait() method to wait on a condition variable.
4. The thread is blocking on I/O.

For example,
The bold line in the following code snippet puts the current thread to sleep for 10 seconds
(10,000 milliseconds):

try
{
Thread.sleep(10000);
} catch (InterruptedException e)
{
}

During the 10 seconds that myThread is asleep; even if the processor becomes available
myThread does not run. After the 10 seconds are up, myThread becomes Runnable again
and, if the processor becomes available, runs.

If a thread has been put to sleep, then the specified number of milliseconds must elapse
before the thread becomes Runnable again. Calling resume() on a sleeping thread has no
effect.

The following indicates the escape route for every entrance into the Not Runnable state.
1. If a thread has been put to sleep, then the specified number of milliseconds must elapse.
2. If a thread has been suspended, then someone must call its resume() method.
3. If a thread is waiting on a condition variable, whatever object owns the variable must
relinquish it by calling either notify() or notifyAll().
4. If a thread is blocked on I/O, then the I/O must complete.


Dead
A thread can die in two ways: either from natural causes, or by being killed (stopped). A
thread dies naturally when its run() method exits normally.
For example,
The while loop in this method is a finite loopit will iterate 100 times and then exit.

public void run()
{
int i = 0;
while (i < 100)
{
i++;
System.out.println(i = + i);
} }

A thread with this run() method will die naturally after the loop and the run() method
completes.

We can also kill a thread at any time by calling its stop() method.
The following code snippet creates and starts myThread then puts the current thread to sleep
for 10 seconds. When the current thread wakes up, the bold line in the code segment kills
myThread.

Thread myThread = new MyThreadClass();
myThread.start();
try
{
Thread.sleep(10000);
} catch (InterruptedException e)
{
}
myThread.stop();

The stop() method throws a ThreadDeath object at the thread to kill it. Thus when a thread is
killed in this manner it dies asynchronously. The thread will die when it actually receives the
ThreadDeath exception.


IllegalThreadStateException
The runtime system throws an IllegalThreadStateException when we call a method on a
thread and that threads state does not allow for that method call.

For example,
IllegalThreadStateException is thrown when we invoke suspend() on a thread that is not
Runnable.


The isAlive() Method
A final word about thread state: the programming interface for the Thread class includes a
method called isAlive().
The isAlive() method returns true if the thread has been started and not stopped.
Thus, if the isAlive() method returns false we know that the thread is either a New
Thread or Dead.
If the isAlive() method returns true, we know that the thread is either Runnable or Not
Runnable.
We cannot differentiate between a New Thread and a Dead thread; nor can we
differentiate between a Runnable thread and a Not Runnable thread.


Thread Priority
A threads priority tells the Java thread scheduler when this thread should run in relation to
other threads.

Some points
1. Most computers have only one CPU, thus threads must share the CPU with other threads.
The execution of multiple threads on a single CPU, in some order, is called scheduling.
The Java runtime supports a very simple, deterministic scheduling algorithm known as
fixed priority scheduling.

2. Each Java thread is given a numeric priority between MIN_PRIORITY and
MAX_PRIORITY (constants defined in class Thread). At any given time, when multiple
threads are ready to be executed, the thread with the highest priority will be chosen for
execution. Only when that thread stops, or is suspended for some reason, will a lower
priority thread start executing.

3. Scheduling of the CPU is fully preemptive. If a thread with a higher priority than the
currently executing thread needs to execute, the higher priority thread is immediately
scheduled.

4. The Java runtime will not preempt the currently running thread for another thread of the
same priority. In other words, the Java runtime does not time-slice. However, the system
implementation of threads underlying the Java Thread class may support time-slicing.
Do not write code that relies on time-slicing.

5. In addition, a given thread may, at any time, give up its right to execute by calling the
yield() method. Threads can only yield the CPU to other threads of the same
priorityattempts to yield to a lower priority thread are ignored.

6. When all the Runnable threads in the system have the same priority, the scheduler
chooses the next thread to run in a simple, non-preemptive, round-robin scheduling
order.


Daemon Threads
Daemon threads are those that provide a service for other threads in the system. Any Java
thread can be a daemon thread.



Multithreading

Thread Group

All threads belong to a thread group. ThreadGroup, a java.lang class, defines and
implements the capabilities of a group of related threads.

The ThreadGroup class manages groups of threads for Java applications. A ThreadGroup
can contain any number of threads. The threads in a group are generally related in some
way, such as who created them, what function they perform, or when they should be
started and stopped.

ThreadGroups can contain not only threads but also other ThreadGroups. The top most
thread group in a Java application is the thread group named main. We can create
threads and thread groups in the main group. We can also create threads and thread
groups in subgroups of main and so on.

The result is a root-like hierarchy of threads and thread groups.


The ThreadGroup class has methods that can be categorized as follows:

1. Collection management Method:-methods that manage the collection of threads and
subgroups contained in the thread group.

2. Method that Operate on the Group:-these methods set or get attributes of the
ThreadGroup object.

3. Method that Operate on All Threads within a Group this is a set of methods that
perform some operation, such as start or resume, on all the threads and subgroups
within the ThreadGroup.

4. Access Restriction Methods:-ThreadGroup and Thread allow the security manager to
restrict access to threads based on group membership.


Collection Management Methods
The ThreadGroup provides a set of methods that manage the threads and subgroups
within the group and allow other objects to query the ThreadGroup for information about
its contents.

For example,
You can call ThreadGroups activeCount() method to find out the number of active
threads currently in the group. The activeCount() method is often used with the
enumerate() method to get an array filled with references to all the active threads in a
ThreadGroup.

For example,
The listCurrentThreads() method in the following example fills an array with all of the
active threads in the current thread group and prints their names:

class EnumerateTest
{
void listCurrentThreads()
{
ThreadGroup currentGroup = Thread.currentThread().getThreadGroup();
int numThreads;
Thread[] listOfThreads;

numThreads = currentGroup.activeCount();
listOfThreads = new Thread[numThreads];
currentGroup.enumerate(listOfThreads);
for (int i = 0; i < numThreads; i++) {
System.out.println(Thread # + i + = + listOfThreads[i].getName());
}
}
}

Other collection management methods provided by the ThreadGroup class include
activeGroupCount() and list().



Multithreading

Methods that Operate on the Group

The ThreadGroup class supports several attributes that are set and retrieved from the
group as a whole. These attributes include the maximum priority that any thread within
the group can have, whether the group is a daemon group, the name of the group, and
the parent of the group.

The methods that get and set ThreadGroup attributes operate at the group level. That is,
they inspect or change the attribute on the ThreadGroup object, but do not affect any of
the threads within the group.


The following is a list of ThreadGroup methods that operate at the group level:
1. getMaxPriority(), and setMaxPriority()
2. getDaemon(), and setDaemon()
3. getName()
4. getParent(), and parentOf()
5. toString()

So for example, when we use setMaxPriority() to change a groups maximum priority, we
are only changing the attribute on the group object; we are not changing the priority of
any of the threads within the group.


Consider this small program that creates a group and a thread within that group:

class MaxPriorityTest
{
public static void main(String[] args)
{

ThreadGroup groupNORM = new ThreadGroup(


A group with normal priority);
Thread priorityMAX = new Thread(groupNORM,
A thread with maximum priority);

// set Threads priority to max (10)


priorityMAX.setPriority(Thread.MAX_PRIORITY);

// set ThreadGroups max priority to normal (5)


groupNORM.setMaxPriority(Thread.NORM_PRIORITY);

System.out.println(Groups maximum priority = +


groupNORM.getMaxPriority());
System.out.println(Threads priority = +
priorityMAX.getPriority());
}
}

When the ThreadGroup groupNORM is created, it inherits its maximum priority attribute
from its parent thread group.
In this case, the parent group priority is the maximum (MAX_PRIORITY) allowed by the
Java runtime system.
Next the program sets the priority of the priorityMAX thread to the maximum allowed by
the Java runtime system. Then the program lowers the groups maximum to the normal
priority (NORM_PRIORITY).
The setMaxPriority() method does not affect the priority of the priorityMAX thread, so
that at this point, the priorityMAX thread has a priority of 10 which is greater than the
maximum priority of its group groupNORM. This is the output from the program:
Groups maximum priority = 5
Threads priority = 10


As we can see a thread can have a higher priority than the maximum allowed by its group
as long as the threads priority is set before the groups maximum priority is lowered. A
thread groups maximum priority is used to limit a threads priority when the thread is
first created within a group or when you use setPriority() to change the threads priority.
Note that setMaxPriority() does change the maximum priority of all of its sub-
threadgroups.


Methods that Operate on All Threads within a Group
The ThreadGroup class has three methods that allow us to modify the current state of
all the threads within that group:
1. resume()
2. stop()
3. suspend()

These methods apply the appropriate state change to every thread in the thread group
and its subgroups.



Multithreading

Access Restriction Methods

The ThreadGroup class itself does not impose any access restrictions, such as allowing
threads from one group to inspect or modify threads in a different group. Rather the
Thread and ThreadGroup classes cooperate with security managers (subclasses of the
java.lang.SecurityManager class), which can impose access restrictions based on thread
group membership.

The Thread and ThreadGroup class both have a method, checkAccess(), which calls the
current security managers checkAccess() method. The security manager decides whether
to allow the access based on the group membership of the threads involved. If access is
not allowed, the checkAccess() method throws a SecurityException. Otherwise,
checkAccess() simply returns.

The following is a list of ThreadGroup methods that call ThreadGroups checkAccess()
before performing the action of the method. These are what are known as regulated
accesses, that is, accesses that must be approved by the security manager before they can
be completed.
1. ThreadGroup(ThreadGroup parent, String name)
2. setDaemon(boolean isDaemon)
3. setMaxPriority(int maxPriority)
4. stop()
5. suspend()
6. resume()
7. destroy()


This is a list of the methods in the Thread class that call checkAccess() before
proceeding:
1. constructors that specify a thread group
2. stop()
3. suspend()
4. resume()
5. setPriority(int priority)
6. setName(String name)
7. setDaemon(boolean isDaemon)

We can define and implement our own access restrictions for thread groups by
subclassing SecurityManager, overriding the appropriate methods, and installing it as the
current security manager in our application.



Multithreading

The notifyAll() and wait() Methods

The notifyAll() and wait() methods can be invoked only by the thread that holds the lock.


The notifyAll() method
The notifyAll() method notifies all the threads waiting on the monitor held by the current
thread and wakes them up.
Example:
public synchronized int get()
{
while (available == false)
{
try
{
wait();

}
catch (InterruptedException e)
{
}
}
available = false;
notifyAll(); // notifies Producer
return contents;
}

In the above example the Producer/Consumer example, the Consumer thread calls the
get() method, so the Consumer thread holds the monitor during the execution of get(). At
the end of the get() method, the call to notifyAll() wakes up the Producer thread that is
waiting to get the monitor. Now, the Producer thread can get the monitor and proceed.

If multiple threads are waiting for a monitor, the Java runtime system chooses one of the
waiting threads to run, making no commitments or guarantees about which thread will be
chosen.


The wait() method
The wait() method causes the current thread to wait (possibly forever) until another
thread notifies it of a condition change.
We use wait() in conjunction with notify() or notifyAll() to coordinate the activities of
multiple threads using the same resources.

Example:
public synchronized int get()
{
while (available == false)
{
try
{
wait(); // waits for notifyAll() call from Producer
} catch (InterruptedException e)
{
}
}
available = false;
notifyAll();
return contents;
}

The Object class contains two other versions of the wait() method:
wait(long timeout)

waits for notification or until the timeout period has elapsedtimeout is measured in
milliseconds.
wait(long timeout, int nanos)

waits for notification or until timeout milliseconds plus nanos nanoseconds have elapsed.

It has a number of methods, some of which are static.



Multithreading

Frequently used Method

The most frequently used methods are the following:



Exception Handling

Introduction

Errors are a normal part of programming.

Some of these errors are flaws in a programs basic design or implementationthese are
called bugs.

Other types of errors are not really bugs; rather, they are the result of situations like low
memory or invalid filenames.

The way we handle the second type of error determines whether they become bugs.

Javas exception-handling mechanism lets us handle errors without forcing us to spend
most of our energy worrying about them.



Exception Handling

What Is an Exception?

As the name implies, an exception is an exceptional condition. An exception is something
out of the ordinary. Most often, exceptions are used as a way to report error conditions.

Exceptions provide notification of errors and a way to handle them. This control structure
allows us to specify exactly where to handle specific types of errors.







Exception Handling

If Exceptions than?

The most common means of error checking is the functions return value.
Consider the problem of calculating the retail cost of an item and displaying it. For this
example, the retail cost is twice the wholesale cost:

int retailCost( int wholesale )
{
if ( wholesale <= 0 )
{
return 0 ;
}
return (wholesale * 2 ) ;
}

The retailCost() method takes the wholesale price of an item and doubles it. If the
wholesale price is negative or zero, the function returns zero to indicate that an error has
occurred.



Exception Handling

The exception handling technique

Involves the use of the try, catch, and finally Java keywords
Consists of several steps


1. try a block of code that may result in an exception being thrown

2. Code one or more blocks designed to automatically catch and handle a specific type of
exception if one occurs. At most, only one of these blocks can be called in a single pass
through the code. If none of the specified exceptions occurs, none of the blocks will be
executed.

3. Optionally code a block that will finally be run in ALL situations, whether an
exception occurs or not


Has the following general syntax:

try
{
statements that may result in an exception being thrown;
}

catch (exception-type1 reference)


{
statements to handle the exception;
}

catch (exception-type2 reference)


{
statements to handle the exception;
}

other catch blocks

finally
{
statements to be ALWAYS executed;
}

Example:
The following is a modified version of the shell game program presented earlier. It
contains a try block and two catch blocks. One handles a NullPointerException and the
other handles an ArrayIndexOutOfBoundsException. No finally block is specified, so
logic comes together after the end of the last catch block.

public class AppExcep
{
public static void main(String[] args)
{

// Loop control variable.

char again = y;

// Main loop. One array is entered and processed in each iteration.

while (again == Y || again ==y)


{

// Instantiate a three element array and load a single String


// at a random element location.

String[] strings = new String[3];


int randomIndex = (int) ((Math.random()*100) % strings.length);
strings[randomIndex] = Lucky;

// Ask the user to guess the element location.

Utility.separator(50, >);
System.out.print(Pick a number from 1 to + strings.length + : );

// Try to read their response, use it to index into the array,


// and see if they win.

try
{

// If they guess the element location, display a message. Otherwise,


// an exception will occur.

if (strings[Keyboard.readInt() - 1].equals(Lucky))
{
Utility.skip();
System.out.println( YOU WIN!!!);
}
}

// This block is automatically executed if a NullPointerException


// occurs to indicate a null array location.

catch (NullPointerException err)


{
Utility.skip();
System.out.println( Empty shell - YOU LOSE!!!);
}

// If an ArrayIndexOutOfBoundsException occurs, this block is


// automatically executed. It indicates an invalid number was
// used as an index.

catch (ArrayIndexOutOfBoundsException err)


{
Utility.skip();
System.out.println( Invalid number - YOU LOSE);
}

// Ask the user if they want to do it again and repeat the loop as
// requested.

Utility.separator(40, =);
System.out.print(Again? (Y/N): );
again = Keyboard.readChar();
}
}
}



Exception Handling

Some Terminology

Exception handling can be viewed as a nonlocal control structure.
When a method throws an exception, its caller must determine whether it can catch the
exception.
If the calling method can catch the exception, it takes over and execution continues in the
caller.

Java exceptions are class objects subclassed from java.lang.Throwable. Because
exceptions are class objects, they can contain both data and methods. In fact, the base class
Throwable implements a method that returns a String describing the error that caused the
exception. This is useful for debugging and if we want to give users a meaningful error
message.



Exception Handling

Throw an Exception

The method instantiates an object of type Exception. The Exception constructor takes a
String parameter. The string contains a message that can be retrieved when the exception is
caught.

The throw statement terminates the method and gives its caller the opportunity to catch it:

if( correct > total )
{
throw new Exception( Invalid values ) ;
}
if ( (float)correct / (float)total > 0.70 )
{
returnCode = true ;
}
return returnCode ;
}



Exception Handling

Throw, try, and catch Blocks

To respond to an exception, the call to the method that produces it must be placed within
a try block.

A try block is a block of code beginning with the try keyword followed by a left and right
curly brace. Every try block is associated with one or more catch blocks. Here is a try
block:

try
{
// method calls go here
}

If a method is to catch exceptions thrown by the methods it calls, the calls must be placed
within a try block.

If an exception is thrown, it is handled in a catch block. Different catch blocks handle
different types of exceptions.

This is a try block and a catch block set up to handle exceptions of type Exception:

try
{
// method calls go here
}
catch( Exception e )
{
// handle exceptons here
}

When any method in the try block throws any type of exception, execution of the try
block ceases. Program control passes immediately to the associated catch block.
If the catch block can handle the given exception type, it takes over.

If it cannot handle the exception, the exception is passed to the methods caller. In an
application, this process goes on until a catch block catches the exception or the
exception reaches the main() method uncaught and causes the application to terminate.


An Exceptional Example
The gradeTest application.

import java.io.* ;
import java.lang.Exception ;
public class gradeTest
{
public static void main( String[] args )
{
try
{
// the second call to passingGrade throws
// an excption so the third call never
// gets executed
System.out.println( passingGrade( 60, 80 ) ) ;
System.out.println( passingGrade( 75, 0 ) ) ;
System.out.println( passingGrade( 90, 100 ) ) ;
}
catch( Exception e )
{
System.out.println( Caught exception +
e.getMessage() ) ;
}
}
static boolean passingGrade( int correct, int total )
throws Exception
{
boolean returnCode = false ;
if( correct > total ) {
throw new Exception( Invalid values ) ;
}
if ( (float)correct / (float)total > 0.70 )
{
returnCode = true ;
}
return returnCode ;
}
}
The second call to passingGrade() fails in this case because the method checks to see
whether the number of correct responses is less than the total responses.


When passingGrade() throws an exception, control passes to the main() method. In the
example, the catch block in main() catches the exception and prints Caught exception -
Invalid values.



Exception Handling

Multiple catch Blocks

In some cases, a method may have to catch different types of exceptions. Java supports
multiple catch blocks. Each catch block must specify a different type of exception:

try
{
// method calls go here
}
catch( SomeExceptionClass e )
{
// handle SomeExceptionClass exceptions here
}
catch( SomeOtherExceptionClass e )
{
// handle SomeOtherExceptionClass exceptions here
}

When an exception is thrown in the try block, it is caught by the first catch block of the
appropriate type.
A method that ignores exceptions thrown by the method it calls.

import java.io.* ;
import java.lang.Exception ;
public class MultiThrow
{
public static void main( String[] args )
{
try
{
fool() ;
}
catch( Exception e )
{
System.out.println( Caught exception +
e.getMessage() ) ;
}
}
static void fool() throws Exception
{
bar() ;
}
static void bar() throws Exception
{
throw new Exception( Who cares ) ;
}
}
In the example main() calls fool() which calls bar(). Because bar() throws an exception
and doesnt catch it, fool() has the opportunity to catch it. The fool() method has no catch
block, so it cannot catch the exception. In this case, the exception propagates up the call
stack to fool()s caller, main().

A method that catches and rethrows an exception.

import java.io.* ;
import java.lang.Exception ;
public class MultiThrowA
{
public static void main( String[] args )
{
try
{
fool() ;
}
catch( Exception e )
{
System.out.println( Caught exception +
e.getMessage() ) ;
}
} static void fool() throws Exception
{
try
{
bar() ;
}
catch( Exception e )
{
System.out.println( Re throw exception +
e.getMessage() ) ;
throw e ;
}
}
static void bar() throws Exception
{
throw new Exception( Who cares ) ;
}
}
The fool() method calls bar(). The bar() method throws an exception and fool() catches it.
In the example, fool() simply rethrows the exception, which is ultimately caught in the
applications main() method. In a real application, fool() could do some processing and
then rethrow the exception. This arrangement allows both fool() and main() to handle the
exception.



Exception Handling

The finally Clause

Java introduces a new concept in exception handling: the finally clause. The finally
clause sets apart a block of code that is always executed.

Example of a finally clause:

import java.io.* ;
import java.lang.Exception ;
public class MultiThrowFin
{
public static void main( String[] args )
{
try
{
alpha() ;
}
catch( Exception e )}
{
System.out.println( Caught exception ) ;
}
finally()
{
System.out.println( Finally. ) ;
}
}
}

In normal execution (that is, when no exceptions are thrown), the finally block is
executed immediately after the try block.
When an exception is thrown, the finally block is executed before control passes to the
caller.
If alpha() throws an exception, it is caught in the catch block and then the finally block is
executed.
If alpha() does not throw an exception, the finally block is executed after the try block. If
any code in a try block is executed, the finally block is executed as well.



Exception Handling

The Throwable Class

All exceptions in Java are subclassed from the class Throwable.

If we want to create your own exception classes, we must subclass Throwable. Most Java
programs do not have to subclass their own exception classes.

Following is the public portion of the class definition of Throwable:

public class Throwable
{
public Throwable() ;
public Throwable(String message) ;
public String getMessage()
public String toString() ;
public void printStackTrace() ;
public void printStackTrace(
java.io.PrintStream s) ;
private native void printStackTrace0(
java.io.PrintStream s);
public native Throwable fillInStackTrace();
}

Java exceptions are Throwable objects (they are instances of Throwable or a subclass of
Throwable). The Java packages contain numerous classes that derive from Throwable and
thus, build a hierarchy of Throwable classes.



Exception Handling

Types of Exceptions

The methods of the Java API and the language itself also throw exceptions.

These exceptions can be divided into two classes: Exception and Error.

Both the Exception and Error classes are derived from Throwable.
Exception and its subclasses are used to indicate conditions that may be recoverable.
Error and its subclasses indicate conditions that are generally not recoverable and that
should cause your applet to terminate.

The various packages included in the Java Development Kit throw different kinds of
Exception and Error exceptions, as described in the following sections.




Exception Handling

Different List of Exception

java.awt Exceptions
The AWT classes have members that throw one error and two exceptions:
AWTException (exception in AWT)
llegalComponentStateException (a component is not in the proper state for a requested
operation)
AWTErr (error in AWT)


java.awt.datatransfer Exception
Classes of the AWT data transfer package may throw this exception:
UnsupportedFlavorException (data in improper format)


java.beans Exceptions
The classes of the java.beans package throw the following exceptions:
IntrospectionException (unable to resolve object during introspection)
PropertyVetoException (illegal property change)


java.io Exceptions
The classes in the java.io package throw a variety of exceptions, Any classes that work with I/O
are good candidates to throw recoverable exceptions. For example, activities such as opening
files or writing to files are likely to fail from time to time. The classes of the java.io package do
not throw errors at all.


The java.io exceptions.


The java.io exception hierarchy.




java.lang Exceptions
The java.lang package contains much of the core Java language. The exceptions subclassed
from RuntimeException do not have to be declared in a methods throws clause. These
exceptions are considered normal because nearly any method can throw them.


The java.lang exceptions.



The java.lang errors.


The java.lang exception hierarchy.



java.lang.reflect Exception
The classes of java.lang.reflect throw the following exception:
InvocationTargetException (invoked method has thrown an exception)


java.net Exceptions
The java.net package handles network communications. Its classes most often throw exceptions
to indicate connect failures and the like.


The java.net exceptions.


The java.net exception hierarchy.


java.rmi Error
The Java Remote Method Invocation classes allow Java objects to exist on remote machines.
These classes throw the following error:
ServerError (remote server indicates error)


java.rmi Exceptions
Java objects whose methods are invoked remotely through RMI may throw exceptions.


The java.rmi exceptions.


The java.rmi exception hierarchy.


java.rmi.server Exceptions
RMI servers throw exceptions.


The java.rmi.server exceptions.



java.security Exceptions
The Java security API allows users to implement security features in Java. The API includes
support for digital signatures, data encryption, key management, and access control.


The java.security exceptions.

The java.security exception hierarchy.




java.security.acl Exceptions
The Java security access control list API allows Java developers to control access to specific
users. The classes of java.security.acl throw the following exceptions:
ACLNotFoundException (unable to find access control list)
LastOwnerExcepti (attempt to delete last owner of ACL)
NotOwnerExcepti (only the owner may modify)


java.sql Exceptions
The Java SQL API throws the following exceptions:
DataTruncation (unexpected data truncation)
SQLException (SQL errorcontains detailed SQL information)
SQLWarning (SQL warning)


java.text Exception
The Java text API throws the following exception:
FormatException (format or parsing error)


java.util Exceptions
The classes of the java.util package throw the following exceptions:
EmptyStackException (no objects on stack)
MissingResourceException (resource missing)
NoSuchElementException (no more objects in collection)
TooManyListenersException (thrown by unicast event listeners)

NOTE:
Unicast is Java terminology for a singleton server object. Singletons are objects that can be
instantiated only once.


java.utils.zip Exceptions
The Java utilities zip API throws the following exceptions:
DataFormatException (format error)
ZipException (Zip error)



Exception Handling

Built-In Exceptions

Here application creates a method and forces it to divide by zero. The method does not
have to explicitly throw an exception because the division operator throws an exception
when required.

An example of a built-in exception.

import java.io.* ;
import java.lang.Exception ;
public class DivideBy0
{
public static void main( String[] args )
{
int a = 2 ;
int b = 3 ;
int c = 5 ;
int d = 0 ;
int e = 1 ;
int f = 3 ;
try
{
System.out.println( a+/+b+ = +div( a, b ) ) ;
System.out.println( c+/+d+ = +div( c, d ) ) ;
System.out.println( e+/+f+ = +div( e, f ) ) ;
}
catch( Exception except )
{
System.out.println( Caught exception +
except.getMessage() ) ;
}
}
static int div( int a, int b ) {
return (a/b) ;
}
}
The output of this application is shown here:
2/3 = 0
Caught exception / by zero

The first call to div() works fine. The second call fails because of the divide-by-zero
error. Even though the application did not specify it, an exception was thrownand
caught.



Applet

Introduction

An applet is a small program that is intended not to be run on its own, but rather to be
embedded inside or launched from within another application. The most common use of
applets are the small programs launched from Web pages.

Applets are graphical and event driven but, for security reasons, may never access the file
system of the platform on which they are run.






Applet

How Applets and Applications Are Different

Java applications are standalone Java programs that can be run by using just the Java
interpreter, for example, from a command line.

Java applets are run from inside a World Wide Web browser.

A reference to an applet is embedded in a Web page using a special HTML tag. When a
reader, using a Java-enabled browser, loads a Web page with an applet in it, the browser
downloads that applet from a Web server and executes it on the local system (the one the
browser is running on).

(The Java interpreter is built into the browser and runs the compiled Java class file from
there.)

Because Java applets run inside a Java browser, they have access to the structure the
browser provides: an existing window, an event-handling and graphics context, and the
surrounding user interface.

A single Java program can be written to operate as both a Java application and a Java
applet. While we use different procedures and rules to create applets and applications,
none of those procedures or rules conflict with each other.

The features specific to applets are ignored when the program runs as an application,
and vice versa.



Applet

Limitation of Applet

The restrictions on applets include the following:
Applets cant read or write to the readers file system, which means they cannot delete
files or test to see what programs you have installed on the hard drive.

Applets cant communicate with any network server other than the one that had
originally stored the applet, to prevent the applet from attacking another system from the
readers system.

Applets cant run any programs on the readers system. For UNIX systems, this includes
forking a process.

Applets cant load programs native to the local platform, including shared libraries such
as DLLs.



Applet

The Applet class

Is part of the java.applet package
Is an extension of the Panel class


Must be the superclass of any applet that is to be embedded in a Web page or viewed by a
Java applet viewer. The Applet class provides a standard interface between applets and
their environment.
Is subclassed to define an applet.

For example,
public class MyApplet extends Applet
begins the definition of a class named MyApplet that inherits all the features of the Object,
Component, Container, Panel, and Applet classes. All that remains is to supply custom
features that define application processing.

Has many methods. The most frequently used are:


Other methods exist for retrieving image files, retrieving and playing audio clips, and more.



Applet

Major Applet Activities

Applets have many different activities that correspond to various major events in the
life cycle of the applet-for example,
1. initialization,
2. painting,
3. and mouse events.

Each activity has a corresponding method, so when an event occurs, the browser or other
Java-enabled tool calls those specific methods.

There are five of the most important methods in an applets execution:
1. initialization,
2. starting,
3. stopping,
4. destroying,
5. and painting.


Initialization
Initialization occurs when the applet is first loaded (or reloaded), similarly to the main()
method in applications. The initialization of an applet might include reading and parsing
any parameters to the applet, creating any helper objects it needs, setting up an initial
state, or loading images or fonts.

To provide behavior for the initialization of our applet, override the init() method in
our applet class:
public void init()
{

} Starting

After an applet is initialized, it is started. Starting is different from initialization because it
can happen many different times during an applets lifetime, whereas initialization
happens only once. Starting can also occur if the applet was previously stopped.

For example,
An applet is stopped if the reader follows a link to a different page, and it is started again
when the reader returns to this page.
To provide startup behavior for your applet, override the start() method:
public void start()
{

} Stopping


Stopping
Stopping occurs when the reader leaves the page that contains a currently running applet,
or you can stop the applet yourself by calling stop(). By default, when the reader leaves a
page, any threads the applet had s tarted will continue running.

By overriding stop(), you can suspend execution of these threads and then restart them if
the applet is viewed again:
public void stop()
{

}


Destroying
Destroying enables the applet to clean up after itself just before it is freed or the browser
exits-for example, to stop and remove any running threads, close any open network
connections, or release any other running objects. Generally, we wont want to override
destroy() unless we have specific resources that need to be released-for example, threads
that the applet has created.

To provide clean-up behavior for your applet, override the destroy() method:
public void destroy()
{

}


Painting
Painting is how an applet actually draws something on the screen, be it text, a line, a
colored background, or an image. Painting can occur many thousands of times during an
applets life . We override the paint() method if your applet needs to have an actual
appearance on the screen (that is, most of the time).

The paint() method looks like this:
public void paint(Graphics g)
{

}

paint() takes an argument, an instance of the class Graphics. This object is created and
passed to paint by the browser,


import java.awt.Graphics;
A Simple Applet
The Hello Again applet

import java.awt.Graphics;

import java.awt.Font;
import java.awt.Color;
public class Aman extends java.applet.Applet
{
Font f = new Font(TimesRoman, Font.BOLD, 36);
public void paint(Graphics g)
{
g.setFont(f);
g.setColor(Color.red);
g.drawString(Hello again!, 5, 40);
}
}
This applet implements the paint() method, one of the major methods described in the
previous section (actually, it overrides the default implementation of paint(), which does
nothing). Because the applet doesnt actually do much (all it does is print a couple words
to the screen), and theres not really anything to initialize, you dont need a start(), stop(),
init(), or destroy() method.



Applet

The life cycle of a Web page applet

The processing of all Web page applets is as follows:
1. The applets init() method is automatically called when the page containing the applet
is loaded by the browser or applet viewer. Its purpose is to perform applet
initialization and it is called only once during the life of the applet. For a simple
applet, this is the only required method. An applet does not need a main() method. If
one exists, it is ignored.

2. The applets start() method is automatically called after init() and every time the page
is revisited. Its purpose is to resume suspended applet processing when the user
returns to the page after surfing.

3. The applets stop() method is automatically called when the user surfs to another page
or when the browser is being shut down. Its purpose is to suspend applet processing.

4. The applets destroy() method is automatically called after stop() when the browser or
applet viewer is being shut down. Its purpose is to release all applet resources and it is
called only once during the life of the applet.


A sample applet
The following statements define a small applet that displays a message when the user
clicks a button. The button may then be re-clicked to clear the message:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Aman extends Applet implements ActionListener


{

Button b = new Button(Show message);


TextField message = new TextField(, 15);

public void init()


{
resize(300, 100);
setBackground(Color.lightGray);
b.addActionListener(this);
add(b);
message.setFont(new Font(Serif, Font.ITALIC, 24));
message.setForeground(Color.red);
message.setEditable(false);
add(message);
}

public void actionPerformed(ActionEvent e)


{
if (message.getText().length() == 0)
{
message.setText(Hello world!);
b.setLabel(Clear message);
}
else {
message.setText();
b.setLabel(Show message);
}
}
}

Notes:
1. By importing the java.applet package, accessing the Applet class is made easier

2. The applet needs no WindowListener interface because it isnt a window

3. Processing begins with the init() method. It immediately resizes the window to
override any size specified by the applets HTML tag .Everything else about the applet
is virtually the same as we would see in an equivalent windows program (with the
exception of window event handling which isnt needed).

4. NOTE: The procedure for compiling a Java applet is identical to the procedure for
compiling a Java program. To test the applets bytecode, however,we must launch the
applet viewer program. This is done by entering the command: appletviewer App.html



Applet

Including an Applet on a Web Page

After we create a class or classes that contain our applet and compile them into class files
as we would any other Java program, we have to create a Web page that will hold that
applet by using the HTML language.

There is a special HTML tag for including applets in Web pages; Java-enabled browsers
use the information contained in that tag to locate the compiled class files and execute the
applet itself.


The <APPLET> Tag
To include an applet on a Web page, use the <APPLET> tag. <APPLET> is a special
extension to HTML for including applets in Web pages.

A simple HTML page.
<HTML>
<HEAD>
<TITLE>This page has an applet on it</TITLE>
</HEAD>
<BODY>
<P>My second Java applet says:
<BR><APPLET CODE=HelloAgainApplet.class WIDTH=200 HEIGHT=50>
Hello Again!
</APPLET>
</BODY>
</HTML>

1. The CODE attribute indicates the name of the class file that contains this applet,
including the .class extension. In this case, the class file must be in the same directory as
this HTML file. To indicate applets are in a specific directory, use CODEBASE,
described later today.

2. WIDTH and HEIGHT are required and are used to indicate the bounding box of the
applet-that is, how big a box to draw for the applet on the Web page. Be sure we set
WIDTH and HEIGHT to be an appropriate size for the applet; depending on the browser,
if our applet draws outside the boundaries of the space youve given it, we may not be
able to see or get to those parts of the applet outside the bounding box.

3. The text between the <APPLET> and </APPLET> tags is displayed by browsers that
do not understand the <APPLET> tag (which includes most browsers that are not Java
aware). Because our page may be viewed in many different kinds of browsers, it is a very
good idea to include some sort of alternate text or HTML tags here so that readers of your
page who dont have Java will see something other than a blank line.

4. Note that the <APPLET> tag, like the <IMG> tag itself, is not a paragraph, so it should
be enclosed inside a more general text tag, such as <P> or one of the heading tags (<H1>,
<H2>, and so on).



Applet

Essential HTML to launch an applet and pass it parameters

Web pages
Are really text documents, in spite of their fancy features
Can be created or viewed with any text editor, such as Microsoft Word or Windows
Notepad
Have a file extension of either .htm or .html


HyperText Markup Language (HTML)
Enhances the capabilities of text-only documents to permit special text formatting, links
to other pages, and the insertion of objects (audio files, graphic files, etc.). It also
provides for the insertion and execution of Java applets.

Uses special tags (codes) within the text file. The tags are acted upon by the browser
when the Web page is loaded.

Example:
Displaying text in bold
This will be bold

Notes:
1. The tag marks the beginning of bold text and the marks the end of bold text
2. HTML tags are not case sensitive. These tags could have been coded and.
3. Most text formatting tags are paired. There is a beginning tag and ending tag.
4. Other HTML tags exist to specify text size, italicizing, centering, the start of a new
paragraph, etc.

Example:
Inserting a graphic image
<<IMG SRC=myPhoto.jpg WIDTH=100 HEIGHT=150>>

Notes:
1. This tag has attributes (parameters) that specify the file name as well as the image
width and height in pixels
2. Unlike HTML tags and attributes, file names are case sensitive. Proper capitalization
must be maintained.
3. The image will appear at the location of the tag within the document
4. Tags that insert image objects and audio clips are not paired


Is simplified by the use of various software products. In fact, a detailed understanding of
HTML is no longer required to create sophisticated Web pages.

The only HTML you are required to learn in this course involves launching a Java applet.
While some packages help generate the tags, it is sometimes necessary to code or edit the
tags manually.



Applet

Launching an applet in an HTML document

Requires a pair of tags. The first tag must have attributes that identify the class name of
the applet and its size in pixels. The second tag marks the end of the pair.
Example:
Launching a simple applet

Notes:
1. In order to launch the applet, the browser must be Java enabled. Otherwise, the tags are
ignored.

2. The attributes can be coded in any order

3. The CODE= attribute specifies the name of the applets .class file (the bytecode file
generated by the Java compiler). If no path is specified, the file is assumed to exist in the
same server directory as the web page. This attribute may optionally be coded within
quotes. For example, CODE=MyApplet.class

4. The initial pixel width and height of the screen area used by the applet are specified by
the WIDTH= and HEIGHT= attributes. A call to the resize() method within the applet
class can modify this size. A maximum size of 600 x 400 is recommended for proper
display regardless of the graphics resolution.

5. There are many other attributes for an applet.

Notes:
1. The tag is required for each parameter that is passed to an applet.

2. The NAME= attribute specifies the case-sensitive identifier of the parameter. The
VALUE= attribute specifies a case-sensitive string value associated with the parameter. It
must be coded in quotes if it contains any spaces. For example, a tag to pass a message to
an applet might contain the attribute VALUE=Hello world!

3. To retrieve the parameters value from the browser, the following expression must be
coded within the applet:
getParameter(taxRate)

In this example, the value received from the browser would be a String object having the
value .06

4. There is no restriction on the number of parameters. Simply code a PARAM tag for
each one and place them between the and tags.



Applet

A sample applet that receives a parameter

This applet is a very slightly modified version of the applet from the previous lesson. The
message it displays when the user clicks the button is received as a parameter from the
HTML within the Web page.

To test the applet, a tag such as <applet> must exist in the App.html source file between
the > and tags. To verify that the tag exists, or to add it if it doesnt, use any text editor
(such as Notepad ).

The applets code is as follows:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class App extends Applet implements ActionListener


{

Button b = new Button(Show message);


TextField message = new TextField(, 15);

public void init()


{
resize(300, 100);
setBackground(Color.lightGray);
b.addActionListener(this);
add(b);
message.setFont(new Font(Serif, Font.ITALIC, 24));
message.setForeground(Color.red);
message.setEditable(false);
add(message);
}

public void actionPerformed(ActionEvent e)


{
if (message.getText().length() == 0)
{
message.setText(getParameter(message));
b.setLabel(Clear message);
}
else
{
message.setText();
b.setLabel(Show message);
}
}
}



Applet

Posting a Web page that launches a custom applet

All we must do is upload the .html file of the Web page and the .class file of the applet to
your Web server. The rest is automatic!









Managing Input/Output Files in Java

Introduction

Nearly all programs require the ability to transfer data in and out of memory.
For example, data may be stored on disk or sent over a network.

The package java.io, part of the standard Java class library, provides a large number of
classes designed for handling input and output to files, network connections, and other
sources. These I/O classes are known as streams, and provide functionality for reading and
writing data in various ways.

Javas input and output classes:
Input streams-and how to create, use, and detect the end of them-and filtered input
streams, which can be nested to great effect.

Output streams, which are mostly analogous to (but the inverse of) input streams.



Managing Input/Output Files in Java

Streams

A stream is a path of communication between the source of some information and its
destination. This information can come from a file, the computers memory, or even
from the Internet.

A stream is a path of communication between a source of information and its
destination.
For example,
An input stream allows us to read data from a source, and an output stream allows us to
write data to a destination.


The java.io Package
To import each individual class or to import the entire java.io package, like this:
import java.io.InputStream;
import java.io.FilteredInputStream;
import java.io.FileOutputStream;

import java.io.*;

The foundations of this stream framework in the Java class hierarchy are the two abstract
classes, InputStream and OutputStream.
Inheriting from these classes is a virtual cornucopia of categorized subclasses,
demonstrating the wide range of streams in the system, but also demonstrating an
extremely well-designed hierarchy of relationships between these streams-one well worth
learning from.



Managing Input/Output Files in Java

Input Streams

Input streams are streams that allow us to read data from a source. These include the root
abstract class InputStream, filtered streams, buffered streams, and streams that read from
files, strings, and byte arrays.








Managing Input/Output Files in Java

The Abstract Class InputStream

InputStream is an abstract class that defines the fundamental ways in which a destination
(consumer) reads a stream of bytes from some source.
read()

The most important method to the consumer of an input stream is the one that reads bytes
from the source.

Heres the first form of read():
InputStream s = getAnInputStreamFromSomewhere();
byte[] buffer = new byte[1024]; // any size will do

if (s.read(buffer) != buffer.length)
System.out.println(I got less than I expected.);

This form of read() attempts to fill the entire buffer given. If it cannot (usually due to
reaching the end of the input stream), it returns the actual number of bytes that were read
into the buffer. After that, any further calls to read() return -1, indicating that you are at
the end of the stream. Note that the if statement still works even in this case, because -1
!= 1024 (this corresponds to an input stream with no bytes in it at all).
skip()

What if you want to skip over some of the bytes in a stream, or start reading a stream
from other than its beginning? A method similar to read() does the trick:
if (s.skip(1024) != 1024)
System.out.println(I skipped less than I expected.);

This example skips over the next 1024 bytes in the input stream. However, the
implementation of skip() in InputStream may skip fewer bytes than the given argument,
and so it returns a long integer representing the number of bytes it actually skipped. In
this example, therefore, a message is printed if the actual number of bytes skipped is less
than 1024.
available()

If for some reason we would like to know how many bytes are in the stream right now,
we can ask the following:
if (s.available() < 1024)
System.out.println(Too little is available right now.);

This tells us the number of bytes that we can read without blocking.
mark() and reset()
InputStream s = getAnInputStreamFromSomewhere();

if (s.markSupported()) { // does s support the notion?


// read the stream for a while
s.mark(1024);
// read less than 1024 more bytes
s.reset();
// we can now re-read those bytes
} else {
// no, perform some alternative
}

When marking a stream, we specify the maximum number of bytes you intend to allow to
pass before resetting it. This allows the stream to limit the size of its byte memory. If
this number of bytes goes by and you have not yet used reset(), the mark becomes
invalid, and attempting to use reset() will throw an exception.



Marking and resetting a stream is most valuable when you are attempting to identify the
type of the stream (or the next part of the stream), but to do so, you must consume a
significant piece of it in the process. Often, this is because you have several black-box
parsers that you can hand the stream to, but they will consume some (unknown to you)
number of bytes before making up their mind about whether the stream is of their type.
Set a large size for the limit in mark(), and let each parser run until it either throws an
error or completes a successful parse. If an error is thrown, use reset() and try the next
parser.

close()
InputStream s = alwaysMakesANewInputStream();

try {
// use s to your hearts content
} finally {
s.close(); }

Get used to this idiom (using finally); its a useful way to be sure something (such as
closing the stream) always gets done.

Java provides many classes that make I/O (input and output) relatively easy and
platform independent. In this lesson, we will learn how to act upon the file structure of a
platform from inside a Java application. Note that Java applets are NOT permitted to
access a platforms file structure for security reasons.



Managing Input/Output Files in Java

The File class

Is part of the java.io package. You will need an import statement to access the class with its
abbreviated name.

Is an extension of the Object class


Encapsulates platform-independent information about a file or a directory.

While all platforms use pathname strings to name files and directories, they do not agree on
the format of these strings. For example, the Windows pathname string is not the same as the
Unix pathname string.

Regardless of platform, however, all pathnames have two components:
1. An optional system-dependent prefix string (such as the disk-drive specifier followed by
/ for the Unix root directory or for the Windows root directory)

2. A sequence of zero or more string names where each name, except for the last, denotes a
directory. The the last name may denote either a directory or a file. A system-dependent
separator (/ for Unix or for Windows) is used between each name.

For example,
The following represents a valid Windows pathname:
C:My DocumentsNew CoursesLesson55.htm

Regardless of the platform, the File class presents a system-independent view of pathnames.


Has a number of static fields. These two are the most useful:


For example,
The following program can determine if it is running under Windows:
import java.io.*;
public class AppFile
{
public static void main(String[] args)
{
if (File.separator.equals())
System.out.println(Windows);
else
System.out.println(Not Windows);
}
}
Has several constructors. The most frequently used constructs a File object from a pathname
string as shown by the following statement:
File f = new File(C:My DocumentsMiscResume.doc);

NOTE:
Constructing a File object does NOT create a disk file. It only constructs a platform-
independent object for referencing the file.


Has a number of methods, some of which WILL create, rename, and delete a disk file or a
directory. They should obviously be used with caution. The most frequently used methods
are:

Consult the Java API documentation for more details.





Managing Input/Output Files in Java

The FileDialog class

Is part of the java.awt package. You will need an import statement to access the class
with its abbreviated name.

Has many ancestor classes as indicated by the following diagram:



Can be instantiated to display a modal dialog window from which the user can select a
file. Because it is a modal dialog, once an application makes a FileDialog object visible, it
cannot continue until the user has chosen a file.

Has two static fields that indicate whether the file will be used for input or output.
They are



Has several constructors. The most frequently used is demonstrated by
FileDialog fd = new FileDialog(this, Choose file, FileDialog.LOAD);
fd.setVisible(true);

which constructs and displays a file dialog window for the current (this) object. The title
of the file dialog window will be Choose file, and the chosen file will be used for input.


Has a number of methods. The most frequently used are as follows:

Consult the Java API documentation for more details.


Sample program
The following menu-driven, Windows program uses the File and FileDialog classes to
display a variety of information about a file selected by the user:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;

public class App extends Frame implements ActionListener


{

// Object references.

MenuBar mainBar;
Menu fileMenu;
MenuItem find;
MenuItem exit;
TextArea fileInfo;

// Processing starts here.

public static void main(String[] args)


{
App myWindow = new App(File Analyzer);
myWindow.setSize(500, 200);
myWindow.setVisible(true);
}

// Class constructor.

public App(String title)


{

// Usual boilerplate for startup and shutdown.

super(title);
//addWindowListener();
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
}
);

// Create the frames menu bar.

mainBar = new MenuBar();


setMenuBar(mainBar);
fileMenu = new Menu(File);
find = new MenuItem(Find);
find.addActionListener(this);
fileMenu.add(find);
fileMenu.addSeparator();
exit = new MenuItem(Exit);
exit.addActionListener(this);
fileMenu.add(exit);
mainBar.add(fileMenu);

// Create the file information text area.

fileInfo = new TextArea( Use the menu bar to find a file,


10, 40, TextArea.SCROLLBARS_NONE);
fileInfo.setFont(new Font(Monospaced, Font.PLAIN, 12));
fileInfo.setEditable(false);
add(fileInfo);
}

// Required method of the ActionListener interface.

public void actionPerformed(ActionEvent e)


{

// If the user selected File | Find from the menu, let them
// choose a file to be analyzed. Then, display its information.

if (e.getSource().equals(find))
{

// Create and display a modal file dialog box through which the
// user can choose the file they want to analyze.

FileDialog fd = new FileDialog(this, Choose file, FileDialog.LOAD);


fd.setVisible(true);

// Construct a File object for the file the user selected.

File f = new File(fd.getDirectory() + File.separator + fd.getFile());

// Display information about the file the user selected.

fileInfo.setText( FILE INFORMATION );


fileInfo.append( Path: + f.getPath());
fileInfo.append( Directory: + fd.getDirectory());
fileInfo.append( File: + f.getName());
fileInfo.append( Last modified: +
new Date(f.lastModified()).toString());
fileInfo.append( Byte length: + f.length());
fileInfo.setCaretPosition(0);
}

// If the user selected File | Exit from the menu, terminate


// the application.

if (e.getSource().equals(exit)) {
dispose();
System.exit(0);
}
}
}



Managing Input/Output Files in Java

Low-level and high-level stream classes

At its lowest level, all Java I/O involves a stream of bytes either entering or leaving memory as
shown by this diagram:


Obviously, sending and receiving individual bytes would be tedious and difficult for an
application to manage. For that reason, several packaged classes exist that make it easy for a
program to read and write larger units of data.

Most programs use a high-level stream class object that is chained (connected) to a low-
level stream class object. While the low-level stream class object handles byte I/O, the high-
level stream class object will allow the program to read and write primitive data values and even
objects (as shown below).


There are many stream classes, but you will only be required to know the ones that are
highlighted below:



Managing Input/Output Files in Java

The FileOutputStream class

Is part of the java.io package

Is an extension of the OutputStream class, an abstract class that describes the behavior of an
output stream


Is a low-level class that can be used to send individual, 8-bit bytes to a stream

Has several constructors. The most frequently used constructs a FileOutputStream object
from a File object that encapsulates the files pathname. For example, if fd is the reference of
a File object


FileOutputStream out = new FileOutputStream(fd);
It will construct a FileOutputStream object for sending bytes to the file. If the file already
exists, its contents will be replaced (there is an overloaded constructor to specify appending).
Because a checked, IOException may occur, the statement should be enclosed in a try block
with an appropriate catch.

Has a few useful methods. The two most used are:


Because a checked, IOException may occur, calls to these methods should be enclosed in a
try block with an appropriate catch. Consult the Java API documentation for more details.


Example. The following program can be used to create a file of numbers on disk.
// Open an output stream for the file.
out = new FileOutputStream(fd);

// This loop asks the user to enter a import

java.io.*;
public class AppFOS
{
public static void main(String[] args)
{

// Local variables and object references.

char again = y;
File fd;
FileOutputStream out;

// Get the path name from the user.

System.out.print(Enter the files complete path name: );


fd = new File(Keyboard.readString());

// Try to write data to the output stream.

try
{
//number and writes it to the
// stream. The user is then asked if they want to enter another.

while (again == Y || again == y)


{
System.out.print(Enter a number: );
int theNumber = Keyboard.readInt();
out.write(theNumber);
System.out.print(Again? (Y/N): );
again = Keyboard.readChar();
}

// Close the stream.

out.close();
System.out.println(Closed - + fd.getPath());
}

// Catch an IOException if one is thrown.

catch (IOException e)
{
System.out.println(e.getMessage());
}
}
}



Managing Input/Output Files in Java

The FileInputStream class

Is part of the java.io package

Is an extension of the InputStream class, an abstract class that describes the behavior of an
input stream


Is a low-level class that can be used to read individual, 8-bit bytes from a stream

Has several constructors. The most frequently used constructs a FileInputStream object
from a File object that encapsulates the files pathname. For example, if fd is the reference of
a File object
FileInputStream in = new FileInputStream(fd);

It will construct a FileInputStream object for reading bytes from the file. Because a checked,
IOException may occur, the statement should be enclosed in a try block with an appropriate
catch.

it has a few useful methods. The most used are:


Because a checked, IOException may occur, calls to these methods should be enclosed in a
try block with an appropriate catch. Consult the Java API documentation for more details.

Example. The following program can be used to read byte values from a disk file. It can be
used to display the values stored by the previous sample program.

import java.io.*;
public class AppFIS
{
public static void main(String[] args)
{

// Local variables and object references.


File fd;
FileInputStream in;

// Get the path name from the user.

System.out.print(Enter the files complete path name: );


fd = new File(Keyboard.readString());

// Try to read data from the input stream.

try
{

// Open an input stream for the file.

in = new FileInputStream(fd);

// This loop reads a byte from the stream and displays


// its value. The loop ends when no more bytes are available.

while (in.available() > 0)


{
System.out.println(in.read());
}

// Close the stream.

in.close();
System.out.println(Closed - + fd.getPath());
}

// Catch an IOException if one is thrown.

catch (IOException e)
{
System.out.println(e.getMessage());
}
}
}



Managing Input/Output Files in Java

The DataOutputStream class

Is part of the java.io package

Is an extension of the FilterOutputStream class (a superclass that facilitates the chaining of
high-level and low-level output streams)


Is a high-level class that can be used to send primitive values and UTF (Unicode
Transformation Format) strings to a stream

Has a single constructor that chains to an object that descends from the OutputStream
class (such as a FileOutputStream object). For example, if fd is the reference of a File
object
DataOutputStream out = new DataOutputStream(new FileOutputStream(fd));

It will construct a DataOutputStream object chained to a FileOutputStream object for
sending primitive values and UTF strings to the file. Because a checked, IOException may
occur, the statement should be enclosed in a try block with an appropriate catch.

Has many useful methods as follows:


Because a checked, IOException may occur, calls to these methods should be enclosed in a
try block with an appropriate catch. Consult the Java API documentation for more details.

Example. The following program can be used to create a file of double values on disk.

import java.io.*;
public class App
{
public static void main(String[] args)
{

// Local variables and object references.

char again = y;
File fd;
DataOutputStream out;

// Get the path name from the user.

System.out.print(Enter the files complete path name: );


fd = new File(Keyboard.readString());

// Try to write data to the output stream.

try
{

// Open an output stream for the file.

out = new DataOutputStream(new FileOutputStream(fd));

// This loop asks the user to enter a number and writes it to the
// stream. The user is then asked if they want to enter another.

while (again == Y || again == y) {


System.out.print(Enter any real number (n.n): );
double theNumber = Keyboard.readDouble();
out.writeDouble(theNumber);
System.out.print(Again? (Y/N): );
again = Keyboard.readChar();
}

// Close the stream.

out.close();
System.out.println(Closed - + fd.getPath());
}

// Catch an IOException if one is thrown.

catch (IOException e)
{
System.out.println(e.getMessage());
}
}
}



Managing Input/Output Files in Java

The DataInputStream class

Is part of the java.io package

Is an extension of the FilterInputStream class (a superclass that facilitates the chaining of
high-level and low-level input streams)

Is a high-level class that can be used to read primitive values and UTF (Unicode
Transformation Format) strings from a stream

Has a single constructor that chains to an object that descends from the InputStream
class (such as a FileInputStream object). For example, if fd is the reference of a File object
DataInputStream in = new DataInputStream(new FileInputStream(fd));

It will construct a DataInputStream object chained to a FileInputStream object for reading
primitive values and UTF strings from the file. Because a checked, IOException may
occur, the statement should be enclosed in a try block with an appropriate catch.

Has many useful methods as follows:


Because a checked, IOException may occur, calls to these methods should be enclosed in a
try block with an appropriate catch. Consult the Java API documentation for more details.

Example. The following program can be used to read a file of double values from disk. It
can be used to display the values stored by the previous sample program.

import java.io.*;
public class App
{
public static void main(String[] args)
{

// Local variables and object references.

File fd;
DataInputStream in;

// Get the path name from the user.


System.out.print(Enter the files complete path name: );
fd = new File(Keyboard.readString());

// Try to read data from the input stream.

try {

// Open an input stream for the file.

in = new DataInputStream(new FileInputStream(fd));

// This loop reads a double value from the stream and displays
// it. The loop ends when end of file is reached.

try
{
while (true)
{
System.out.println(in.readDouble());
}
}
catch (EOFException e)
{
System.out.println();
}

// Close the stream.

in.close();
System.out.println(Closed - + fd.getPath());
}

// Catch an IOException if one is thrown.

catch (IOException e)
{
System.out.println(e.getMessage());
}
}
}



Managing Input/Output Files in Java

The ObjectOutputStream class

Is part of the java.io package

Is an extension of the OutputStream class, an abstract class that describes the behavior of an
output stream


Is a high-level class that can be used to send primitive values and serializable objects to a
stream. All that is needed for an object to be serializable, is that its class must implement the
Serializable interface. For example, if a Customer class is to be serializable, its header may be
coded
public class Customer implements Serializable

The interface requires no methods.
Many packaged classes are serializable including all wrapper classes, String and Stringbuffer
classes, Vector and Array classes, and the collection classes. In other words, an entire
collection, such as a SortedMap, can be stored as an object on disk!

Has overloaded constructors but the most useful chains to an object that descends from the
OutputStream class (such as a FileOutputStream object). For example, if fd is the reference of
a File object
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fd));

It will construct a ObjectOutputStream object chained to a FileOutputStream object for
sending primitive values and serializable objects to the file. Because a checked, IOException
may occur, the statement should be enclosed in a try block with an appropriate catch.

Has many useful methods as follows:


Because a checked, IOException may occur, calls to these methods should be enclosed in a try
block with an appropriate catch. Consult the Java API documentation for more details.

Example. The following program can be used to create a file of multiple array objects on
disk.

import java.io.*;
public class App
{
public static void main(String[] args)
{

// Local variables and object references.

char again = y;
String[] array;
File fd;
ObjectOutputStream out;

// Get the path name from the user.

System.out.print(Enter the files complete path name: );


fd = new File(Keyboard.readString());

// Try to write data to the output stream.

try
{

// Open an output stream for the file.

out = new ObjectOutputStream(new FileOutputStream(fd));

// This loop constructs an array with values entered by the user


// and writes the array to the file. The user is given the option
// of repeating the loop to construct and store another array.

while (again == Y || again == y)


{
System.out.print(How many strings will you enter? );
array = new String[Keyboard.readInt()];
for (int i = 0; i < array.length; i++) {
System.out.print(Enter a string: );
array[i] = Keyboard.readString();
}
out.writeObject(array);
System.out.print(Again? (Y/N) );
again = Keyboard.readChar();
}

// Close the stream.

out.close();
System.out.println(Closed - + fd.getPath());
}

// Catch an IOException if one is thrown.

catch (IOException e) {
System.out.println(e.getMessage());
}
}
}



Managing Input/Output Files in Java

The ObjectInputStream class

Is part of the java.io package

Is an extension of the InputStream class, an abstract class that describes the behavior of an
input stream


Is a high-level class that can be used to read primitive values and serializable objects from a
stream

Has overloaded constructors but the most useful chains to an object that descends from the
InputStream class (such as a FileInputStream object). For example, if fd is the reference of a
File object
ObjectInputStream out = new ObjectInputStream(new FileInputStream(fd));

will construct a ObjectInputStream object chained to a FileInputStream object for reading
primitive values and serializable objects from the file. Because a checked, IOException may
occur, the statement should be enclosed in a try block with an appropriate catch.

Has many useful methods as follows:


Because a checked, IOException may occur, calls to these methods should be enclosed in a try
block with an appropriate catch. Consult the Java API documentation for more details.

Example. The following program can be used to read a file of array objects from disk. It can be
used to display the array values stored by the previous sample program.

import java.io.*;
public class App
{
public static void main(String[] args)
{

// Local variables and object references.

File fd;
ObjectInputStream in;

// Get the path name from the user.

System.out.print(Enter the files complete path name: );


fd = new File(Keyboard.readString());

// Try to read data from the input stream.

try
{

// Open an input stream for the file.

in = new ObjectInputStream(new FileInputStream(fd));

// This loop reads a array objects from the stream and displays
// their contents. The loop ends when end of file is reached.

try
{
while (true)
{
String[] array = (String[]) in.readObject();
for (int i = 0; i < array.length; i++)
{
System.out.println(array[i]);
}
System.out.println(<< END OF ARRAY >>);
}
}
catch (EOFException e) {
System.out.println(<< END OF FILE >>);
}

// Close the stream.

in.close();
System.out.println(Closed - + fd.getPath());
}

// Catch an IOException if one is thrown.

catch (IOException e) {
System.out.println(e.getMessage());
}

// Catch an ClassNotFoundException if one is thrown.

catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
}
}



Plz Click above menu items.

You might also like