Thursday, January 29, 2009

Web-Tools Cource Material

Lecture Notes – 1
Topic: Unit -I
Unit -II
Unit -III


Unit – I
Introduction to HTML/DHTML/XML/Java scripting.
Active Server Pages (ASP)
Introduction to Java- Java vs. C++, Byte code.

Unit – II
Tokens, Data Types, Variables, operators, Control Statements, Staring Handling, Arrays and Vectors.
Methods and Classes, Inheritance, Packages.
I.O, Exception Handling.

Unit – III
Multithreading, Applet and Graphics Programming.
Introduction to Electronic Commerce.
Introduction to Electronic Commerce.


Suggested books:
1. Patrick Naughton : Java 1.1 : The Complete Reference, TMH & Herbert Schidt 2. Hatman & Eden : ASP with VB Script, SQL and HTML Programming Reference
3. Jackson, J : Java by example, Sunsoft Pre,

Unit – II
Introduction of Java Programming:
About: Java Vs C
Java C
1. Java does not include the c uniqe statement 1. C include these keywords.
Keyword goto, sizeof and typedef
2. Java does not contain the data types 2. But C include these things.
Structs unions and enum.
3. Java doesnot support explict pointer. 3. But c includes.
4. Java does not have a preprocessor 4. But c have
So we cannot use # define, # include
5. Java does not support any Mechanism 5. C support these.
6. Java add many features required for 6. But C does not support.
Object oriented.

















Lecture Notes - 2



Topic: 1 Introduction to Java
2. History of java
3. Java vs. C++

1. Introduction to Java
Java is an object-oriented programming language with a built-in application programming interface (API) that can handle graphics and user interfaces and that can be used to create applications or applets.
Because of its rich set of API's, similar to Macintosh and Windows, and its platform independence, Java can also be thought of as a platform in itself. Java also has standard libraries for doing mathematics.
Much of the syntax of Java is the same as C and C++. One major difference is that Java does not have pointers. However, the biggest difference is that you must write object oriented code in Java. Procedural pieces of code can only be embedded in objects. In the following we assume that the reader has some familiarity with a programming language. In particular, some familiarity with the syntax of C/C++ is useful.
In Java we distinguish between applications, which are programs that perform the same functions as those written in other programming languages, and applets, which are programs that can be embedded in a Web page and accessed over the Internet. Our initial focus will be on writing applications. When a program is compiled, a byte code is produced that can be read and executed by any platform that can run Java.
To use this tutorial you should run and study each program as you read along, so that you can see how added features affect the programs

2. History of java

Invented and first implemented by Dennis Ritchie on the UNIX operating system, C was the result of a development process that started with an older language called BCPL, developed by Martin Richards. BCPL influenced a language called B, invented by Ken Thompson, which led to the development of C in the 1970s. For many years, the de facto standard for C was the one supplied with the UNIX operating system and described in The C Programming Language by Brian Kernighan and Dennis Ritchie (Prentice-Hall, 1978). C was formally standardized in December 1989, when the American National Standards Institute (ANSI) standard for C was adopted.
The creation of C is considered by many to have marked the beginning of the modern age of computer languages. It successfully synthesized the conflicting attributes that had so troubled earlier languages. The result was a powerful, efficient, structured language that was relatively easy to learn. It also included one other, nearly intangible aspect: it was a programmer’s language. Prior to the invention of C, computer languages were generally designed either as academic exercises or by bureaucratic committees. C is different. It was designed, implemented, and developed by real, The 1960s gave birth to structured programming. This is the method of programming championed by languages such as C. The use of structured
languages enabled programmers to write, for the first time, moderately complex programs fairly easily. However, even with structured programming methods, once a project reaches a certain size, its complexity exceeds what a programmer can manage. By the early 1980s, many projects were pushing the structured approach past its limits.
To solve this problem, a new way to program was invented, called object-oriented programming (OOP). OOP is a programming methodology that helps organize complex programs through the use of inheritance, encapsulation, and polymorphism.

C++ was invented by Bjarne Stroustrup in 1979, while he was working at Bell Laboratories in Murray Hill, New Jersey. Stroustrup initially called the new language
“C with Classes.” However, in 1983, the name was changed to C++.

Java was conceived by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan at Sun Microsystems, Inc. in 1991. It took 18 months to develop the first working version. This language was initially called “Oak” but was renamed “Java” in 1995. Between the initial implementation of Oak in the fall of 1992 and the public announcement of Java in the spring of 1995, many more people contributed to the design.

.
3. Java versus C++

C++ has added support for statically-typed object-oriented programming, exception handling, scoped resource management, and generic programming, in particular. It also added a standard library that includes generic containers and algorithms.
Java was created initially to support network computing. It relies on a virtual machine to be secure and highly portable. It is bundled with an extensive library designed to provide a complete abstraction of the underlying platform. Java is a statically typed object-oriented language that uses a syntax similar to C, but is not compatible with it. It was designed from scratch, with the goal of being easy to use and accessible to a wider audience.
Java versus C++: General programming issues
Java C++
1. No preprocessor. 1. Heavy reliance on preprocessor.
2. No comma operator 2. Comma operator
3. No optional method parameters. 3. optional method parameters
4. No const reference parameters. 4. const reference parameters
5. No goto 5. goto
6. Main method cannot return a value. 6. Main method return a value
7. No global variables. 7. global variables
Java versus C++: Object-oriented programming
Java C++
8. Pure object-oriented language. 8. Hybrid between procedural and object-oriented.
9. All functions (methods) are 9. Can have stand-along functions.
part of a class.
10. No multiple inheritance. 10. Multiple inheritance.
11. Formal interface specifications. 11. No formal interface specifications.
12. No parameterized type. 12. Templates as parameterized type.
13. No operator overloading. 13. Operator overloading.
14. All methods (except final 14. Virtual functions are dynamically bound.
methods) are dynamically bound.
Java versus C++: Primitive types
Java C++
15. All nonprimitive types are objects. 15. Separate types for structs, unions, enums, and arrays.
16. All numeric types are signed. 16. Signed and unsigned numeric types.
17. All primitive types are a fixed size 17. for all platforms. Primitive type size varies by platform.
18. 16-bit Unicode characters. 18. 8-bit ASCII characters.
19. Boolean data type primitive. 19. No explicit boolean data type.
20. Variables are automatically 20. No automatic initialization of variables.
initialized.
Java versus C++: Pointers and data structures
Java C++
21. References, with no explicit pointer 21. Manipulation and no pointer arithmetic. Pointers, with dereferencing (* or ->) . address (&) operators
22. Array references are not translated 22. Array references translate to pointer
To pointer arithmetic. . arithmetic.
23. Arrays automatically check index 23. No automatically array bounds checking
limits. .
24. Strings are objects. 24. Strings are null-terminated character arrays.
25. Built-in string concatenation 25. String concatenation through a library function
operator(+).
26. Specifically attuned to network 26. No relationship to networks or the Web
and Web processing. .
27. Automatic garbage collection. 27. No automatic garbage collection.
28. Combination of compiled and 28. Compiled.
interpreted.
29. Slower execution when interpreted. 29. Fast execution.
30. Supports multithreading. 30. No multithreading.

JAVA C++
class Foo // Defines class Foo{ private int x = 0; // Member variable, // with initializer public Foo() // Constructor for Foo { } public int bar(int i) // Member method bar() { return 3*i + x; }
}
class Foo // Declares class Foo{ int x; // Member variable public: Foo(): x(0) // Constructor for Foo, { // initializes x } int bar(int i) // Member function bar() { return 3*i + x; }
};













































Lecture Notes - 3
Topic
1. Byte code,
2. Machine code (JVM)


1. Byte code

This article gives you an understanding of Java bytecode that will enable you to be a better programmer. Like a C or C++ compiler translates source code into assembler code, Java compilers translate Java source code into bytecode. Java programmers should take the time to understand what the bytecode is, how it works, and most importantly, what bytecode is being generated by the Java compiler. In some cases, the bytecode generated is not what you expect.
An often overlooked aspect of Java is the bytecode that is generated by the javac compiler. Understanding bytecode and what bytecode is likely to be generated by a Java compiler helps the Java programmer in the same way that knowledge of assembler helps the C or C++ programmer.
The bytecode is your program. Regardless of a JIT or Hotspot runtime, the bytecode is an important part of the size and execution speed of your code. Consider that the more bytecode you have, the bigger the .class file is and the more code that has to be compiled by a JIT or Hotspot runtime. The remainder of this article gives you an in depth look at Java bytecode.



2. Java Virtual Machine (JVM) Machine code
A Java Virtual Machine (JVM) is a set of computer software programs and data structures which use a virtual machine model for the execution of other computer programs and scripts. The model used by a JVM accepts a form of computer intermediate language commonly referred to as Java bytecode. This language conceptually represents the instruction set of a stack-oriented, capability architecture. As of 2006, there are an estimated 4 billion JVM-enabled devices worldwide.
Java Virtual Machines operate on Java bytecode, which is normally (but not necessarily) generated from Java source code; a JVM can also be used to implement programming languages other than Java. For example, Ada source code can be compiled to Java bytecode, which may then be executed by a JVM. JVMs can also be released by other companies besides Sun (the developer of Java) — JVMs using the "Java" trademark may be developed by other companies as long as they adhere to the JVM specification published by Sun (and related contractual obligations).






Lecture Notes - 4
Topic: Characteristics of Java
CHARACTERISTICS OF JAVA/ buzzwords
In spite of that paper is organized along eleven buzzwords, we make reference here to only six of that buzzwords. Here you have the relaction of them
1. Simple 6. Architecture Neutral
2. Object Oriented 7. Portable
3. Distributed 8. Interpreted
4. Robust 9.. High Performance
5. Secure 10. Multithreaded
11. Dynamic

DESCRIPTION OF FEATURES
1. Java is simple
If you already understand the basic concepts of object-oriented programming, learning Java will be even easier. Best of all, if you are an experienced C++ programmer, moving to Java will require very little effort. Because Java inherits the C/C++ syntax and many of the object-oriented features of C++, most programmers have little trouble learning Java. Also, some of the more confusing concepts from C++ are either left out of Java or implemented in a cleaner, more approachable manner. Beyond its similarities with C/C++, Java has another attribute that makes it easy to learn: it makes an effort not to have surprising features.

2. Object-Oriented Programming Language
Simply stated, object-oriented design is a technique for programming that focuses on the data (=objects) and on the interfaces to that object. To make an analogy with carpentry, an "object-oriented" carpenter would be mostly concerned with the chair he was building, and secondarily with the tools used to make it; a "non-object-oriented" carpenter would think primarily of his tools. The object-oriented facilities of Java are essentially those of C++.
You probably know already the structured programming. If you think a bit about the model of structured programming, you'll see that it's not the same model that we use naturally to organize the information in our sense. The OOP (Object-Oriented Programming) tries to use in programming, the same model which our sense uses. So, you will learn rapidly how to use this way of programming.
The OOP has only one difficulty: you have to learn some new concept based in a special object-oriented terminology. So, you will ear words like object, class, instance... and you will ear that OOP has based on three major properties: encapsulation, polymorphism and inheritance.

3. Distributed

Java is designed for the distributed environment of the Internet, because it handles TCP/IP protocols. In fact, accessing a resource using a URL is not much different from accessing a file. The original version of Java (Oak) included features for intraaddress- space messaging. This allowed objects on two different computers to execute procedures remotely. Java revived these interfaces in a package called Remote Method Invocation (RMI). This feature brings an unparalleled level of abstraction to client/ server programming.

4. Java is robust
Java is intended for writing programs that must be reliable in a variety of ways. Java puts a lot of emphasis on early checking for possible problems, later dynamic (run-time) checking, and eliminating situations that is error-prone.... The single biggest difference between Java and C/C++ is that Java has a pointer model that eliminates the possibility of overwriting memory and corrupting data. Some of these differences make Java a very robust programming language. For example, Java doesn’t allow multiple inheritances, it hasn’t pointers which are a source of continuous mistakes and it's a strongly-tiped programming language. For these and too many others things programers from all around the world can say that Java™ is a very robust language.
5. Java is secure
Java is intended to be used in networked/distributed enviroments. Toward that end, a lot of emphasis has been placed on security. Java enables the construction of virus-free, tamper-free systems.
Here you have some points which tries to guarantee the applets security in Java:
Language and compiler : All the references to memory are symbolic references (you can't know where in the memory will stay your program, it only depends of the JVM and of each machine).
Bytecodes checker : On the Java Virtual Machine the bytecodes code is checked to confirm that it was compiled on an standard compiler.
Classes loader : Each applet is loaded on its own memory space, to make impossible the information interchange between applets.
Security on applets : The programmer can configure his communications with a certain degree of security
6. Architecture-Neutral
A central issue for the Java designers was that of code longevity and portability. One of the main problems facing programmers is that no guarantee exists that if you write a program today, it will run tomorrow—even on the same machine. Operating system upgrades, processor upgrades, and changes in core system resources can all combine to make a program malfunction. The Java designers made several hard decisions in the Java language and the Java Virtual Machine in an attempt to alter this situation. Their goal was “write once; run anywhere, any time, forever.” To a great extent, this goal was accomplished.


7. Java is portable
(because is both compiled and interpreted)
Unlike C and C++, there are no "implementation-dependent" aspects of the specification. The sizes of the primitive data types are specified, as is the behavior of arithmetic on them...
The Java interpreter can execute Java bytecodes directly on any machine to which the interpreter has been ported. Since linking is a more incremental and lightweight process, the development process can be much more rapid and exploratory.
While most programming languages you have only to compile or interpret a program to can run it on your computer, the Java™ way to do this is very different. Java™ is both compiled and interpreted and this is the difference that allows that portability.
You need two steps to execute a Java program:
Using the compiler, you translate the source code of a program into an interpreted language called Java bytecodes.
Using the interpreter of the Java platform, you can finally execute this program.
8. Interpreted and High Performance

As described earlier, Java enables the creation of cross-platform programs by compiling into an intermediate representation called Java byte code. This code can be interpreted on any system that provides a Java Virtual Machine. Most previous attempts at cross platform solutions have done so at the expense of performance. Other interpreted systems, such as BASIC, Tcl, and PERL, suffer from almost insurmountable performance deficits. Java, however, was designed to perform well on very low-power CPUs. As explained earlier, while it is true that Java was engineered for interpretation, the Java bytecode was carefully designed so that it would be easy to translate directly into native machine code for very high performance by using a just-in-time compiler. Java run-time systems that provide this feature lose none of the benefits of the platform-independent code. “High-performance cross-platform” is no longer an oxymoron.
9. Java is multithreaded
(the) benefits of multithreading are better interactive responsiveness and real-time behavior.
A good way to prevent your Java programs from locking up is to use multithreading, which simply means you write a program that enables multiple execution streams to occur simultaneously within the same program
Threads are rapidly becoming an important programming facility, particularly for Internet-related applications. They permit a form of parallelism within a single program that can greatly improve program performance. For instance, the Netscape 3.0 browser and its posterior versions permits the user to download a file while continuing to browse the WWW, an example of multiple threads within a program.
Using multiple threads in Java programs is far easier than in C or C++ because of the language-level support offered by the Java programming language.
10. Dynamic
Java programs carry with them substantial amounts of run-time type information that is used to verify and resolve accesses to objects at run time. This makes it possible to dynamically link code in a safe and expedient manner. This is crucial to the robustness of the applet environment, in which small fragments of bytecode may be dynamically updated on a running system.























Lecture Notes – 5

Topic: 1. Oop Principles:
2. Tokens
Data Types


The Three OOP Principles
1. Encapsulation
2. Inheritance
3. Polymorphism
All object-oriented programming languages provide mechanisms that help you implement the object-oriented model. They are encapsulation, inheritance, and polymorphism. Let’s take a look at these concepts now. Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse. One way to think about encapsulation is as a protective wrapper that prevents the code and data from being
arbitrarily accessed by other code defined outside the wrapper. Access to the code and data inside the wrapper is tightly controlled through a well-defined interface. To relate this to the real world, consider the automatic transmission on an automobile. It encapsulates hundreds of bits of information about your engine, such as how much you are accelerating, the pitch of the surface you are on, and the position of the shift lever. You, as the user, have only one method of affecting this complex encapsulation:

In Java the basis of encapsulation is the class. Although the class will be examined in great detail later in this book, the following brief discussion will be helpful now. A class defines the structure and behavior (data and code) that will be shared by a set of objects. Each object of a given class contains the structure and behavior defined by the class, as if it were stamped out by a mold in the shape of the class. For this reason, objects are sometimes referred to as instances of a class. Thus, a class is a logical construct; an
object has physical reality. When you create a class, you will specify the code and data that constitute that class. Collectively, these elements are called members of the class. Specifically, the data
defined by the class are referred to as member variables or instance variables. Since the purpose of a class is to encapsulate complexity, there are mechanisms for hiding the complexity of the implementation inside the class. Each method or variable in a class may be marked private or public. The public interface of a class represents everything that external users of the class need to know, or may know. The private methods and data can only be accessed by code that is a member of the class. Therefore, any other code that is not a member of the class cannot access a private method or variable. Since the private members of a class may only be accessed by other parts of your program through the class’ public methods, you can ensure that no improper actions take place.
Of course, this means that the public interface should be carefully designed not to expose
too much of the inner workings of a class

Inheritance is the process by which one object acquires the properties of another object.
This is important because it supports the concept of hierarchical classification. As
mentioned earlier, most knowledge is made manageable by hierarchical (that is, top-down)
classifications. For example, a Golden Retriever is part of the classification dog, which in turn is part of the mammal class, which is under the larger class animal. Without the use of hierarchies, each object would need to define all of its characteristics explicitly.
However, by use of inheritance, an object need only define those qualities that make it unique within its class. It can inherit its general attributes from its parent. Thus, it is the inheritance mechanism that makes it possible for one object to be a specific instance of a more general case. Let’s take a closer look at this process.

THE
JAVA
LANGUAGE
Polymorphism
Polymorphism (from the Greek, meaning “many forms”) is a feature that allows one
interface to be used for a general class of actions. The specific action is determined by the exact nature of the situation. Consider a stack (which is a last-in, first-out list). You might have a program that requires three types of stacks. One stack is used for integer values, one for floating-point values, and one for characters. The algorithm that implements each stack is the same, even though the data being stored differs. In a non– object-oriented language, you would be required to create three different sets of stack routines, with each set using different names. However, because of polymorphism, in Java you can specify a general set of stack routines that all share the same names. More generally, the concept of polymorphism is often expressed by the phrase “one interface, multiple methods.” This means that it is possible to design a generic interface
to a group of related activities. This helps reduce complexity by allowing the same interface to be used to specify a general class of action. It is the compiler’s job to select the specific action (that is, method) as it applies to each situation. You, the programmer, do not need to make this selection manually. You need only remember and utilize the general interface.
LANGUAGE
Extending the dog analogy, a dog’s sense of smell is polymorphic. If the dog smells a cat, it will bark and run after it. If the dog smells its food, it will salivate and run to its bowl. The same sense of smell is at work in both situations. The difference is what is being smelled, that is, the type of data being operated upon by the dog’s nose! This same general concept can be implemented in Java as it applies to methods within a Java program.
Polymorphism, Encapsulation, and Inheritance
Work Together
When properly applied, polymorphism, encapsulation, and inheritance combine to produce a programming environment that supports the development of far more robust and scaleable programs than does the process-oriented model. A well-designed hierarchy of classes is the basis for reusing the code in which you have invested time and effort developing and testing. Encapsulation allows you to migrate your implementations over time without breaking the code that depends on the public interface of your classes. Polymorphism allows you to create clean, sensible, readable, and resilient code. Of the two real-world examples, the automobile more completely illustrates the power
of object-oriented design. Dogs are fun to think about from an inheritance standpoint, but cars are more like programs. All drivers rely on inheritance to drive different types (subclasses) of vehicles. Whether the vehicle is a school bus, a Mercedes sedan, a Porsche, or the family minivan, drivers can all more or less find and operate the steering wheel, the brakes, and the accelerator. After a bit of gear grinding, most people can even manage the difference between a stick shift and an automatic, because they fundamentally understand their common superclass, the transmission.
People interface with encapsulated features on cars all the time. The brake and gas pedals hide an incredible array of complexity with an interface so simple you can operate them with your feet! The implementation of the engine, the style of brakes,
and the size of the tires have no effect on how you interface with the class definition
of the pedals.
The final attribute, polymorphism, is clearly reflected in the ability of car manufacturers to offer a wide array of options on basically the same vehicle. For example, you can get an antilock braking system or traditional brakes, power or rack-and-pinion steering, 4-, 6-, or 8-cylinder engines. Either way, you will still press the break pedal to stop, turn the steering wheel to change direction, and press the accelerator when you want to move. The same interface can be used to control a number of different implementations. As you can see, it is through the application of encapsulation, inheritance, and polymorphism that the individual parts are transformed into the object known as a car. The same is also true of computer programs. By the application of object-oriented principles, the various parts of a complex program can be brought together to form
a cohesive, robust, maintainable whole.


1. Tokens
In a Java program, all characters are grouped into Statements called tokens. Larger language features are built from the first five categories of tokens (the sixth kind of token is recognized, but is then discarded by the Java compiler from further processing). We must learn how to identify all six kind of tokens that can appear in Java programs.
token = identifier keyword separator operator literal comment
We will examine each of these kinds of tokens in more detail below, again using . For now, we briefly describe in English each token type.
Identifiers: names the programmer chooses
Keywords: names already in the programming language
Separators (also known as punctuators): punctuation characters and paired-delimiters
Operators: symbols that operate on arguments and produce results
Literals (specified by their type)
Numeric: int and double
Logical: boolean
Textual: char and String
Reference: null
Comments
Line
Block
Finally, we will also examine the concept of white space which is crucial to understanding how the Java compiler separates the characters in a program into a list of tokens; it sometimes helps decide where one token ends and where the next token starts.
The Java Character Set
The full Java character set includes all the Unicode characters; there are 216 = 65,536 unicode characters. Since this character set is very large and its structure very complex, in this class we will use only the subset of unicode that includes all the ASCII (pronounced "Ask E") characters; there are 28 = 256 ASCII characters, of which we will still use a small subset containing alphabetic, numeric, and some special characters.
lower-case <= abcdefghijklmnopqrstuvwxyzupper-case <= ABCDEFGHIJKLMNOPQRSTUVWXYZalphabetic <= lower-case upper-casenumeric <= 0123456789alphanumeric <= alphabetic numericspecial <= !%^&*()-+={}~[]\;':"<>?,./#@`_graphic <= alphanumeric special
1. Identifiers
The first category of token is an Identifier. Identifiers are used by programmers to name things in Java: things such as variables, methods, fields, classes, interfaces, exceptions, packages, etc.
id-start <= alphabetic $ _identifier <= id-start{id-start numeric }
2. Keywords
The second category of token is a Keyword, sometimes called a reserved word. Keywords are identifiers that Java reserves for its own use. These identifiers have built-in meanings that cannot change. Thus, programmers cannot use these identifiers for anything other than their built-in meanings. Technically, Java classifies identifiers
and keywords as separate categories of tokens.
The following is a list of all 49 Java keywords we will learn the meaning of many, but not all,of them in this course. It would be an excellent idea to print this table, and then check off the meaning of each keyword when we learn it; some keywords have multiple meanings, determined by the context in which they are used.
abstract
continue
goto
package
switch
assert
default
if
private
this
boolean
do
implements
protected
throw
break
double
import
public
throws
byte
else
instanceof
return
transient
case
extends
int
short
try
catch
final
interface
static
void
char
finally
long
strictfp
volatile
class
float
native
super
while
const
for
new
synchronized

Notice that all Java keywords contain only lower-case letters
3. Separators
The third category of token is a Separator (also known as a punctuator). There are exactly nine, single character separators in Java.
separator <= ; , . ( ) { } [ ]
correct Java program, its matching right delimiter appears soon afterwards (they always come in matched pairs). Together, these each pair delimits some other entity.
For example the Java code Math.max(count,limit); contains nine tokens
The fourth category of token is an Operator. Java includes 37 operators that are listed in the table below; each of these operators consist of 1, 2, or at most 3 special characters.
=
>
<
!
~
?
:




==
<=
>=
!=
&&

++
--



+
-
*
/
&

^
%
<<
>>
>>>
+=
-=
*=
/=
&=
=
^=
%=
<<=
>>=
>>=

3. Types and Literals
The fifth, and most complicated category of tokens is the Literal. All values that we write in a program are literals: each belongs to one of Java's four primitive types (int, double, boolean, char) or belongs to the special reference type String. All primitive type names are keywords in Java; the String reference type names a class in the standard Java library, which we will learn much more about soon. A value (of any type) written in a Java program is called a literal; and, each written literal belongs in (or is said to have) exactly one type.
literal <= integer-literal floating-point-literal boolean-literal character-literal string-literal null-literal
Here are some examples of literals of each of these types.
Literal
type
1
int
3.14
double (1. is a double too)
true
boolean

int Literals
Literals of the primitive type int represent countable, discrete quantities (values with no fractions nor decimal places possible/necessary). We can specify the EBNF for an int literal in Java as
non-zero-digit <= 123456789digit <= 0 non-zero-digitdecimal-numeral <= 0 non-zero-digit[digits]floating-point-literal <= digits exponent-part digits.[digits][exponent-part] .digits[exponent-part]
boolean Literals
The type name boolean honors George Boole, a 19th century English mathematician who revolutionized the study of logic by making it more like arithmetic. He invented a method for
boolean-literal <= true false
char Literals
character-literal <= 'graphic' 'space' 'escape-sequence'
where the middle option is a space between single quotes. Examples are 'X', or 'x', or '?', or ' ', or '\n', etc. (see below for a list of some useful escape sequences).
String Literals
The second type of text literal is a String. Literals of this reference type (the only one in this bunch; it is not a primitive type) represent zero, one, or more characters: Its EBNF is written
string-literal <= "{graphic space escape-sequence}"
Examples are: "\n\nEnter your SSN:", or "" (the empty String), or "X" (a one character String, which is different from a char).
Note that "CMU" is classified just as a literal token (of the reference type String); it is NOT classified as an identifier token inside two separator tokens!




2. Data Types

Name Width value Range
Integer
byte 8 -27 –128 to 127
short 16 -215 –32,768 to 32,767
int 32 -231 –2,147,483,648 to 2,147,483,647
Long 64 -263 –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Char 16 \u000 to \ufffff
float 32 1.4e045 to 3.4e+038
double 64 4.9e–324 to 1.8e+308
boolean 1 bit (True of False)















Java Data Types



Primitive Non Primitive


1. Array 2. Class 3. Interface
1. Numeric 2. Non-Numeric


Floating Boolean
Integral

Float Double


Integer Character

byte char
short
int
long





boolean
1-bit. May take on the values true and false only.
true and false are defined constants of the language and are not the same as True and False, TRUE and FALSE, zero and nonzero, 1 and 0 or any other numeric value. Booleans may not be cast into any other type of variable nor may any other variable be cast into a boolean.
byte
1 signed byte (two's complement). Covers values from -128 to 127.
short
2 bytes, signed (two's complement), -32,768 to 32,767
int
4 bytes, signed (two's complement). -2,147,483,648 to 2,147,483,647. Like all numeric types ints may be cast into other numeric types (byte, short, long, float, double). When lossy casts are done (e.g. int to byte) the conversion is done modulo the length of the smaller type.
long
8 bytes signed (two's complement). Ranges from -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807.
float
4 bytes, IEEE 754. Covers a range from 1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative).
Like all numeric types floats may be cast into other numeric types (byte, short, long, int, double). When lossy casts to integer types are done (e.g. float to short) the fractional part is truncated and the conversion is done modulo the length of the smaller type.
double
Double precision, as denoted by the double keyword, uses 64 bits to store a value. Double
precision is actually faster than single precision on some modern processors that have
been optimized for high-speed mathematical calculations. All transcendental math
functions, such as sin( ), cos( ), and sqrt( ), return double values. When you need to
maintain accuracy over many iterative calculations, or are manipulating large-valued
numbers, double is the best choice.
Here is a short program that uses double variables to compute the area of a circle:
// Compute the area of a circle.
class Area {
public static void main(String args[]) {
double pi, r, a;
r = 10.8; // radius of circle
pi = 3.1416; // pi, approximately
a = pi * r * r; // compute area
C h a p t e r 3 : D a t a T y p e s , V a r i a b l e s , a n d A r r a y s 47
THE
JAVA
System.out.println("Area of circle is " + a);
}
}




















3. Operators

Arithmetic Operators
Arithmetic operators are used in mathematical expressions in the same way that they
are used in algebra. The following table lists the arithmetic operators:
Operator Result
+ Addition
– Subtraction (also unary minus)
* Multiplication
/ Division
% Modulus
++ Increment
+= Addition assignment
–= Subtraction assignment
*= Multiplication assignment
/= Division assignment
%= Modulus assignment
– – Decrement
The operands of the arithmetic operators must be of a numeric type. You cannot
use them on boolean types, but you can use them on char types, since the char type in
Java is, essentially, a subset of int.
The Basic Arithmetic Operators
The basic arithmetic operations—addition, subtraction, multiplication, and division—
all behave as you would expect for all numeric types. The minus operator also has
a unary form which negates its single operand. Remember that when the division
THE
JAVA
LANGUAGE
operator is applied to an integer type, there will be no fractional component attached to
the result.
The following simple example program demonstrates the arithmetic operators. It
also illustrates the difference between floating-point division and integer division.
// Demonstrate the basic arithmetic operators.
class BasicMath {
public static void main(String args[]) {
// arithmetic using integers
System.out.println("Integer Arithmetic");
int a = 1 + 1;
int b = a * 3;
int c = b / 4;
int d = c - a;
int e = -d;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
System.out.println("e = " + e);
// arithmetic using doubles
System.out.println("\nFloating Point Arithmetic");
double da = 1 + 1;
double db = da * 3;
double dc = db / 4;
double dd = dc - a;
double de = -dd;
System.out.println("da = " + da);
System.out.println("db = " + db);
System.out.println("dc = " + dc);
System.out.println("dd = " + dd);
System.out.println("de = " + de);
}
}
When you run this program, you will see the following output:
Integer Arithmetic
a = 2
b = 6
c = 1
d = -1
e = 1
Floating Point Arithmetic
da = 2.0
db = 6.0
dc = 1.5
dd = -0.5
de = 0.5
The Modulus Operator
The modulus operator, %, returns the remainder of a division operation. It can be
applied to floating-point types as well as integer types. (This differs from C/C++, in
which the % can only be applied to integer types.) The following example program
demonstrates the %:
// Demonstrate the % operator.
class Modulus {
public static void main(String args[]) {
int x = 42;
double y = 42.25;
System.out.println("x mod 10 = " + x % 10);
System.out.println("y mod 10 = " + y % 10);
}
}
When you run this program you will get the following output:
x mod 10 = 2
y mod 10 = 2.25
Arithmetic Assignment Operators
Java provides special operators that can be used to combine an arithmetic operation
with an assignment. As you probably know, statements like the following are quite
common in programming:
a = a + 4;
In Java, you can rewrite this statement as shown here:
76 J a v a ™ 2 : T h e C o m p l e t e R e f e r e n c e
C h a p t e r 4 : O p e r a t o r s 77
THE
JAVA
LANGUAGE
a += 4;
This version uses the += assignment operator. Both statements perform the same
action: they increase the value of a by 4.
Here is another example,
a = a % 2;
which can be expressed as
a %= 2;
In this case, the %= obtains the remainder of a/2 and puts that result back into a.
There are assignment operators for all of the arithmetic, binary operators. Thus,
any statement of the form
var = var op expression;
can be rewritten as
var op= expression;
The assignment operators provide two benefits. First, they save you a bit of typing,
because they are “shorthand” for their equivalent long forms. Second, they are
implemented more efficiently by the Java run-time system than are their equivalent
long forms. For these reasons, you will often see the assignment operators used in
professionally written Java programs.
Here is a sample program that shows several op= operator assignments in action:
// Demonstrate several assignment operators.
class OpEquals {
public static void main(String args[]) {
int a = 1;
int b = 2;
int c = 3;
a += 5;
b *= 4;
c += a * b;
c %= 6;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
}
}
The output of this program is shown here:
a = 6
b = 8
c = 3
Increment and Decrement
The ++ and the – – are Java’s increment and decrement operators. They were introduced
in Chapter 2. Here they will be discussed in detail. As you will see, they have some
special properties that make them quite interesting. Let’s begin by reviewing precisely
what the increment and decrement operators do.
The increment operator increases its operand by one. The decrement operator
decreases its operand by one. For example, this statement:
x = x + 1;
can be rewritten like this by use of the increment operator:
x++;
Similarly, this statement:
x = x - 1;
is equivalent to
x--;
THE
JAVA
LANGUAGE
These operators are unique in that they can appear both in postfix form, where
they follow the operand as just shown, and prefix form, where they precede the
operand. In the foregoing examples, there is no difference between the prefix and
postfix forms. However, when the increment and/or decrement operators are part
of a larger expression, then a subtle, yet powerful, difference between these two forms
appears. In the prefix form, the operand is incremented or decremented before the value
is obtained for use in the expression. In postfix form, the previous value is obtained for
use in the expression, and then the operand is modified. For example:
x = 42;
y = ++x;
In this case, y is set to 43 as you would expect, because the increment occurs before x is
assigned to y. Thus, the line y = ++x; is the equivalent of these two statements:
x = x + 1;
y = x;
However, when written like this,
x = 42;
y = x++;
the value of x is obtained before the increment operator is executed, so the value of
y is 42. Of course, in both cases x is set to 43. Here, the line y = x++; is the equivalent
of these two statements:
y = x;
x = x + 1;
The following program demonstrates the increment operator.
// Demonstrate ++.
class IncDec {
public static void main(String args[]) {
int a = 1;
int b = 2;
int c;
80 J a v a ™ 2 : T h e C o m p l e t e R e f e r e n c e
int d;
c = ++b;
d = a++;
c++;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
}
}
The output of this program follows:
a = 2
b = 3
c = 4
d = 1
The Bitwise Operators
Java defines several bitwise operators which can be applied to the integer types, long,
int, short, char, and byte. These operators act upon the individual bits of their operands.
They are summarized in the following table:
Operator Result
~ Bitwise unary NOT
& Bitwise AND
Bitwise OR
^ Bitwise exclusive OR
>> Shift right
>>> Shift right zero fill
<< Shift left
&= Bitwise AND assignment
= Bitwise OR assignment
Operator Result
^= Bitwise exclusive OR assignment
>>= Shift right assignment
>>>= Shift right zero fill assignment
<<= Shift left assignment
THEJAVA
LANGUAGE
The Bitwise Logical Operators
The bitwise logical operators are &, , ^, and ~. The following table shows the outcome
of each operation. In the discussion that follows, keep in mind that the bitwise operators
are applied to each individual bit within each operand.
A B A B A & B A ^ B ~A
0 0 0 0 0 1
1 0 1 0 1 0
0 1 1 0 1 1
1 1 1 1 0 0
The Bitwise NOT
Also called the bitwise complement, the unary NOT operator, ~, inverts all of the bits of
its operand. For example, the number 42, which has the following bit pattern:
00101010
becomes
11010101
after the NOT operator is applied.
The Bitwise AND
The AND operator, &, produces a 1 bit if both operands are also 1. A zero is produced
in all other cases. Here is an example:
00101010 42
&00001111 15
--------------
00001010 10
The Bitwise OR
The OR operator, , combines bits such that if either of the bits in the operands is a 1,
then the resultant bit is a 1, as shown here:
00101010 42
00001111 15
--------------
00101111 47
THE
JAVA
LANGUAGE
The Bitwise XOR
The XOR operator, ^, combines bits such that if exactly one operand is 1, then the result
is 1. Otherwise, the result is zero. The following example shows the effect of the ^. This
example also demonstrates a useful attribute of the XOR operation. Notice how the bit
pattern of 42 is inverted wherever the second operand has a 1 bit. Wherever the second
operand has a 0 bit, the first operand is unchanged. You will find this property useful
when performing some types of bit manipulations.
00101010 42
^00001111 15
-------------
00100101 37
Using the Bitwise Logical Operators
The following program demonstrates the bitwise logical operators:
// Demonstrate the bitwise logical operators.
class BitLogic {
public static void main(String args[]) {
String binary[] = {
"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
};
int a = 3; // 0 + 2 + 1 or 0011 in binary
int b = 6; // 4 + 2 + 0 or 0110 in binary
int c = a b;
int d = a & b;
int e = a ^ b;
int f = (~a & b) (a & ~b);
int g = ~a & 0x0f;
System.out.println(" a = " + binary[a]);
System.out.println(" b = " + binary[b]);
System.out.println(" ab = " + binary[c]);
System.out.println(" a&b = " + binary[d]);
System.out.println(" a^b = " + binary[e]);
System.out.println("~a&ba&~b = " + binary[f]);
System.out.println(" ~a = " + binary[g]);
}
}
a = 0011
b = 0110
ab = 0111
a&b = 0010
a^b = 0101
~a&ba&~b = 0101
~a = 1100
The Left Shift
The left shift operator, <<, shifts all of the bits in a value to the left a specified number
of times. It has this general form:
value << num
Here, num specifies the number of positions to left-shift the value in value. That is, the
<< moves all of the bits in the specified value to the left by the number of bit positions
specified by num. For each shift left, the high-order bit is shifted out (and lost), and a
zero is brought in on the right. This means that when a left shift is applied to an int
operand, bits are lost once they are shifted past bit position 31. If the operand is a long,
then bits are lost after bit position 63.
LANGUAGE
// Left shifting a byte value.
class ByteShift {
public static void main(String args[]) {
byte a = 64, b;
int i;
i = a << 2;
b = (byte) (a << 2);
System.out.println("Original value of a: " + a);
System.out.println("i and b: " + i + " " + b);
}
}
The output generated by this program is shown here:
Original value of a: 64
i and b: 256 0
Since a is promoted to int for the purposes of evaluation, left-shifting the value 64
The Right Shift
The right shift operator, >>, shifts all of the bits in a value to the right a specified number
of times. Its general form is shown here:
value >> num
Here, num specifies the number of positions to right-shift the value in value. That is, the
>> moves all of the bits in the specified value to the right the number of bit positions
specified by num.
The following code fragment shifts the value 32 to the right by two positions,
resulting in a being set to 8:
int a = 32;
a = a >> 2; // a now contains 8
When a value has bits that are “shifted off,” those bits are lost. For example, the
next code fragment shifts the value 35 to the right two positions, which causes the two
low-order bits to be lost, resulting again in a being set to 8.
int a = 35;
a = a >> 2; // a still contains 8
Looking at the same operation in binary shows more clearly how this happens:
00100011 35
>> 2
00001000 8
JAVA
LANGUAGE
Relational Operators
The relational operators determine the relationship that one operand has to the other.
Specifically, they determine equality and ordering. The relational operators are
shown here:
Operator Result
== Equal to
!= Not equal to
> Greater than
Operator Result
< Less than
>= Greater than or equal to
<= Less than or equal to
The outcome of these operations is a boolean value. The relational operators are
most frequently used in the expressions that control the if statement and the various
loop statements.
Any type in Java, including integers, floating-point numbers, characters, and Booleans
can be compared using the equality test, ==, and the inequality test, !=. Notice that in
Java (as in C/C++/C#) equality is denoted with two equal signs, not one. (Remember:
a single equal sign is the assignment operator.) Only numeric types can be compared
using the ordering operators. That is, only integer, floating-point, and character operands
may be compared to see which is greater or less than the other.
As stated, the result produced by a relational operator is a boolean value. For
example, the following code fragment is perfectly valid:
int a = 4;
int b = 1;
boolean c = a < b;
In this case, the result of aIf you are coming from a C/C++ background, please note the following. In C/C++,
these types of statements are very common:
int done;
// ...
if(!done) ... // Valid in C/C++
if(done) ... // but not in Java.
In Java, these statements must be written like this:
if(done == 0)) ... // This is Java-style.
if(done != 0) ...
The reason is that Java does not define true and false in the same way as C/C++.
In C/C++, true is any nonzero value and false is zero. In Java, true and false are
nonnumeric values which do not relate to zero or nonzero. Therefore, to test for zero
or nonzero, you must explicitly employ one or more of the relational operators.
JAVA
LANGUAGE
Boolean Logical Operators
The Boolean logical operators shown here operate only on boolean operands. All
of the binary logical operators combine two boolean values to form a resultant
boolean value.
Operator Result
& Logical AND
Logical OR
^ Logical XOR (exclusive OR)
Short-circuit OR
&& Short-circuit AND
! Logical unary NOT
&= AND assignment
= OR assignment
^= XOR assignment
== Equal to
!= Not equal to
?: Ternary if-then-else
The logical Boolean operators, &, , and ^, operate on boolean values in the same
way that they operate on the bits of an integer. The logical ! operator inverts the
Boolean state: !true == false and !false == true. The following table shows the effect
of each logical operation:
A B A B A & B A ^ B !A
False False False False False True
True False True False True False
False True True False True True
True True True True False False
Here is a program that is almost the same as the BitLogic example shown earlier,
but it operates on boolean logical values instead of binary bits:
THE
JAVA
LANGUAGE
// Demonstrate the boolean logical operators.
class BoolLogic {
public static void main(String args[]) {
boolean a = true;
boolean b = false;
boolean c = a b;
boolean d = a & b;
boolean e = a ^ b;
boolean f = (!a & b) (a & !b);
boolean g = !a;
System.out.println(" a = " + a);
System.out.println(" b = " + b);
System.out.println(" ab = " + c);
System.out.println(" a&b = " + d);
System.out.println(" a^b = " + e);
System.out.println("!a&ba&!b = " + f);
System.out.println(" !a = " + g);
}
}
After running this program, you will see that the same logical rules apply to
boolean values as they did to bits. As you can see from the following output, the
string representation of a Java boolean value is one of the literal values true or false:
a = true
b = false
ab = true
a&b = false
a^b = true
a&ba&!b = true
!a = false














Lecture Notes - 6
Topic: 1. Variables
1. Variables

A variable is a storage location and has an associated type, sometimes called its compile-time type, that is either a primitive type or a reference type. A variable always contains a value that is assignment compatible with its type. A variable's value is changed by an assignment or by a prefix or postfix ++ (increment) or -- (decrement) operator
Compatibility of the value of a variable with its type is guaranteed by the design of the Java programming language. Default values are compatible and all assignments to a variable are checked for assignment compatibility, usually at compile time, but, in a single case involving arrays, a run-time check is made
Declaring a Variable

In Java, all variables must be declared before they can be used. The basic form of
a variable declaration is shown here:
type identifier [ = value][, identifier [= value] ...] ;


int a, b, c; // declares three ints, a, b, and c.
int d = 3, e, f = 5; // declares three more ints, initializing
// d and f.
byte z = 22; // initializes z.
double pi = 3.14159; // declares an approximation of pi.
char x = 'x'; // the variable x has the value 'x'.
Variables of Primitive Type
A variable of a primitive type always holds a value of that exact primitive type.
Variables of Reference Type
A variable of reference type can hold either of the following:
A null reference
A reference to any object whose class is assignment compatible with the type of the variable
Kinds of Variables
There are six kinds of variables:
A class variable is a field declared using the keyword static within a class declaration or with or without the keyword static within an interface declaration. A class variable is created when its class or interface is prepared and is initialized to a default value. The class variable effectively ceases to exist when its class or interface is unloaded.
An instance variable is a field declared within a class declaration without using the keyword static. If a class T has a field a that is an instance variable, then a new instance variable a is created and initialized to a default value as part of each newly created object of class T or of any class that is a subclass of T. The instance variable effectively ceases to exist when the object of which it is a field is no longer referenced, after any necessary finalization of the object has been completed.
Array components are unnamed variables that are created and initialized to default values whenever a new object that is an array is created. The array components effectively cease to exist when the array is no longer referenced. See for a description of arrays.
Method parameters name argument values passed to a method. For every parameter declared in a method declaration, a new parameter variable is created each time that method is invoked. The new variable is initialized with the corresponding argument value from the method invocation. The method parameter effectively ceases to exist when the execution of the body of the method is complete.
Constructor parameters name argument values passed to a constructor. For every parameter declared in a constructor declaration, a new parameter variable is created each time a class instance creation expression or explicit constructor invocation invokes that constructor. The new variable is initialized with the corresponding argument value from the creation expression or constructor invocation. The constructor parameter effectively ceases to exist when the execution of the body of the constructor is complete.
An exception-handler parameter is created each time an exception is caught by a catch clause of a try statement The new variable is initialized with the actual object associated with the exception The exception-handler parameter effectively ceases to exist when execution of the block associated with the catch clause is complete.
Initial Values of Variables
A variable can be declared final. A final variable may only be assigned to once. It is a compile time error if a final variable is assigned to unless it is definitely unassigned immediately prior to the assignment.
A blank final is a final variable whose declaration lacks an initializer
Every variable in a program must have a value before its value is used:
Each class variable, instance variable, or array component is initialized with a default value when it is created
For type byte, the default value is zero, that is, the value of (byte)0.
For type short, the default value is zero, that is, the value of (short)0.
For type int, the default value is zero, that is, 0.
For type long, the default value is zero, that is, 0L.
For type float, the default value is positive zero, that is, 0.0f.
For type double, the default value is positive zero, that is, 0.0d.
For type char, the default value is the null character, that is, '\u0000'.
For type boolean, the default value is false.
For all reference types, the default value is null
THE
JAVA
LANGUAGE
Type Conversion and Casting
A value of one type to a variable of another type. If the two types are compatible, then Java will perform the conversion automatically. For example, it is always possible to assign an int value to a long variable. However, not all types are compatible, and thus, not all type conversions are implicitly allowed. For instance, there is no conversion defined from double to byte. Fortunately, it is still possible to obtain a conversion between incompatible types. To do so, you must use a cast, which performs an explicit conversion between incompatible types. Let’s look at both automatic
type conversions and casting.
Java’s Automatic Conversions
When one type of data is assigned to another type of variable, an automatic type
conversion will take place if the following two conditions are met:

1. The two types are compatible.
2. The destination type is larger than the source type.

When these two conditions are met, a widening conversion takes place. For example, the int type is always large enough to hold all valid byte values, so no explicit cast statement is required. For widening conversions, the numeric types, including integer and floating-point types, are compatible with each other. However, the numeric types are not compatible with char or boolean. Also, char and boolean are not compatible with each other. As mentioned earlier, Java also performs an automatic type conversion when storing a literal integer constant into variables of type byte, short, or long.

Casting Incompatible Types
Although the automatic type conversions are helpful, they will not fulfill all needs. For example, what if you want to assign an int value to a byte variable? This conversion will not be performed automatically, because a byte is smaller than an int. This kind of conversion is sometimes called a narrowing conversion, since you are explicitly making the value narrower so that it will fit into the target type.
To create a conversion between two incompatible types, you must use a cast. A cast is simply an explicit type conversion. It has this general form:
(target-type) value
Here, target-type specifies the desired type to convert the specified value to. For example, the following fragment casts an int to a byte. If the integer’s value is larger than the range of a byte, it will be reduced modulo (the remainder of an integer
division by the) byte’s range.
int a;
byte b;
// ...
b = (byte) a;
A different type of conversion will occur when a floating-point value is assigned to an integer type: truncation. As you know, integers do not have fractional components. Thus, when a floating-point value is assigned to an integer type, the fractional component is lost. For example, if the value 1.23 is assigned to an integer, the resulting value will simply be 1. The 0.23 will have been truncated. Of course, if the size of the whole number component is too large to fit into the target integer type, then that value will be reduced modulo the target type’s range. The following program demonstrates some type conversions that require casts:
// Demonstrate casts.
class Conversion {
public static void main(String args[]) {
byte b;
int i = 257;
double d = 323.142;
System.out.println("\nConversion of int to byte.");
b = (byte) i;
System.out.println("i and b " + i + " " + b);
System.out.println("\nConversion of double to int.");
i = (int) d;
System.out.println("d and i " + d + " " + i);
System.out.println("\nConversion of double to byte.");
b = (byte) d;
System.out.println("d and b " + d + " " + b);
}
}
This program generates the following output:
VA
LANGUAGE
Conversion of int to byte.
i and b 257 1
Conversion of double to int.
d and i 323.142 323
Conversion of double to byte.
d and b 323.142 67
Let’s look at each conversion. When the value 257 is cast into a byte variable, the result is the remainder of the division of 257 by 256 (the range of a byte), which is 1 in this case. When the d is converted to an int, its fractional component is lost. When d is converted to a byte, its fractional component is lost, and the value is reduced modulo 256, which in this case is 67. Automatic Type Promotion in Expressions In addition to assignments, there is another place where certain type conversions may occur: in expressions. To see why, consider the following. In an expression, the
precision required of an intermediate value will sometimes exceed the range of either
operand. For example, examine the following expression:
byte a = 40;
byte b = 50;
byte c = 100;
int d = a * b / c;
The result of the intermediate term a * b easily exceeds the range of either of its byte operands. To handle this kind of problem, Java automatically promotes each byte or short operand to int when evaluating an expression. This means that the subexpression a * b is performed using integers—not bytes. Thus, 2,000, the result of the intermediate expression, 50 * 40, is legal even though a and b are both specified as
type byte.
As useful as the automatic promotions are, they can cause confusing compile-time errors. For example, this seemingly correct code causes a problem:
byte b = 50;
b = b * 2; // Error! Cannot assign an int to a byte!
60
byte b = 50;
b = (byte)(b * 2);
which yields the correct value of 100.
The Type Promotion Rules
In addition to the elevation of bytes and shorts to int, Java defines several type promotion
rules that apply to expressions. They are as follows. First, all byte and short values are promoted to int, as just described. Then, if one operand is a long, the whole expression is promoted to long. If one operand is a float, the entire expression is promoted to float. If any of the operands is double, the result is double. The following program demonstrates how each value in the expression gets promoted to match the second argument to each binary operator:

class Promote {
public static void main(String args[]) {
byte b = 42;
char c = 'a';
short s = 1024;
int i = 50000;
float f = 5.67f;
double d = .1234;
double result = (f * b) + (i / c) - (d * s);
System.out.println((f * b) + " + " + (i / c) + " - " + (d * s));
System.out.println("result = " + result);
}
}
Let’s look closely at the type promotions that occur in this line from the program:
LANGUAGE
double result = (f * b) + (i / c) - (d * s);
In the first subexpression, f * b, b is promoted to a float and the result of the subexpression is float. Next, in the subexpression i / c, c is promoted to int, and the result is of type int. Then, in d * s, the value of s is promoted to double, and the type of the subexpression is double. Finally, these three intermediate values, float, int, and double, are considered. The outcome of float plus an int is a float. Then the resultant float minus the last double is promoted to double, which is the type for the





















Lecture Notes - 7
Topic:1. Control Statements

A control statement can be put into the following categories: selection, iteration,
and jump. Selection statements allow your program to choose different paths of execution based upon the outcome of an expression or the state of a variable. Iteration statements enable program execution to repeat one or more statements (that is, iteration statements form loops). Jump statements allow your program to execute in a nonlinear fashion. All of Java’s control statements are examined here.
If you know C/C++ then Java’s control statements will be familiar territory. In fact, Java’s control statements are nearly identical to those in those languages. However, there are a few differences—especially in the break and continue statements.
Java’s Selection Statements
Java supports two selection statements: if and switch. These statements allow you to control the flow of your program’s execution based upon conditions known only during run time. You will be pleasantly surprised by the power and flexibility contained in these two statements.
if
. It is examined in detail here. The if statement is Java’s conditional branch statement. It can be used to route program execution through two different paths. Here is the general form of the if statement:
if (condition) statement1;
else statement2;
Here, each statement may be a single statement or a compound statement enclosed in curly braces (that is, a block). The condition is any expression that returns a boolean value.
The else clause is optional.
The if works like this: If the condition is true, then statement1 is executed. Otherwise, statement2 (if it exists) is executed. In no case will both statements be executed. For example, consider the following:
int a, b;
// ...
if(a < b) a = 0;
else b = 0;
Here, if a is less than b, then a is set to zero. Otherwise, b is set to zero. In no case are they both set to zero.
Most often, the expression used to control the if will involve the relational operators. However, this is not technically necessary. It is possible to control the if using a single boolean variable, as shown in this code fragment:
boolean dataAvailable;
// ...
if (dataAvailable)
ProcessData();
else
waitForMoreData();
Remember, only one statement can appear directly after the if or the else. If you want to include more statements, you’ll need to create a block, as in this fragment:
int bytesAvailable;
// ...
if (bytesAvailable > 0) {
ProcessData();
bytesAvailable -= n;
} else
waitForMoreData();
Here, both statements within the if block will execute if bytesAvailable is greater than zero. Some programmers find it convenient to include the curly braces when using the if, even when there is only one statement in each clause. This makes it easy to add another statement at a later date, and you don’t have to worry about forgetting the braces. In fact, forgetting to define a block when one is needed is a common cause of errors. For
example, consider the following code fragment:
int bytesAvailable;
// ...
if (bytesAvailable > 0) {
ProcessData();
bytesAvailable -= n;
} else
waitForMoreData();
bytesAvailable = n;
JAVA
LANGUAGE
It seems clear that the statement bytesAvailable = n; was intended to be executed inside the else clause, because of the indentation level. However, as you recall, whitespace is insignificant to Java, and there is no way for the compiler to know what was intended. This code will compile without complaint, but it will behave incorrectly when run. The preceding example is fixed in the code that follows:
int bytesAvailable;
// ...
if (bytesAvailable > 0) {
ProcessData();
bytesAvailable -= n;
} else {
waitForMoreData();
bytesAvailable = n;
}
Nested ifs
A nested if is an if statement that is the target of another if or else. Nested ifs are very common in programming. When you nest ifs, the main thing to remember is that an else statement always refers to the nearest if statement that is within the same block
as the else and that is not already associated with an else. Here is an example:
if(i == 10) {
if(j < 20) a = b;
if(k > 100) c = d; // this if is
else a = c; // associated with this else
}
else a = d; // this else refers to if(i == 10)
As the comments indicate, the final else is not associated with if(j<20), because it is not
in the same block (even though it is the nearest if without an else). Rather, the final else
is associated with if(i==10). The inner else refers to if(k>100), because it is the closest if
within the same block.
The if-else-if Ladder
A common programming construct that is based upon a sequence of nested ifs is the
if-else-if ladder. It looks like this:
if(condition)
statement;
else if(condition)
statement;
else if(condition)
statement;
...
else
statement;
The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed. The final else acts as a default condition; that is, if all other conditional tests fail, then the last else statement is performed. If there is no final else and all other conditions are false, then no action will take place. Here is a program that uses an if-else-if ladder to determine which season a particular
month is in.
// Demonstrate if-else-if statements.
class IfElse {
public static void main(String args[]) {
int month = 4; // April
String season;
if(month == 12 month == 1 month == 2)
season = "Winter";
else if(month == 3 month == 4 month == 5)
season = "Spring";
else if(month == 6 month == 7 month == 8)
season = "Summer";
else if(month == 9 month == 10 month == 11)
season = "Autumn";
else
season = "Bogus Month";
System.out.println("April is in the " + season + ".");
}
}
Here is the output produced by the program:
April is in the Spring.
You might want to experiment with this program before moving on. As you will find, no matter what value you give month, one and only one assignment statement within the ladder will be executed.
switch
The switch statement is Java’s multiway branch statement. It provides an easy way to dispatch execution to different parts of your code based on the value of an expression. As such, it often provides a better alternative than a large series of if-else-if statements. Here is the general form of a switch statement:
switch (expression) {
case value1:
// statement sequence
break;
case value2:
// statement sequence
break;
...
case valueN:
// statement sequence
break;
default:
// default statement sequence
}
The expression must be of type byte, short, int, or char; each of the values specified in the case statements must be of a type compatible with the expression. Each case value must be a unique literal (that is, it must be a constant, not a variable). Duplicate case values are not allowed.
The switch statement works like this: The value of the expression is compared with each of the literal values in the case statements. If a match is found, the code sequence following that case statement is executed. If none of the constants matches the value of the expression, then the default statement is executed. However, the default statement is optional. If no case matches and no default is present, then no further action is taken.
The break statement is used inside the switch to terminate a statement sequence. When a break statement is encountered, execution branches to the first line of code that follows the entire switch statement. This has the effect of “jumping out” of the switch.
Here is a simple example that uses a switch statement:

// A simple example of the switch.
class SampleSwitch {
public static void main(String args[]) {
for(int i=0; i<6; i++)
switch(i) {
case 0:
System.out.println("i is zero.");
break;
case 1:
System.out.println("i is one.");
break;
case 2:
System.out.println("i is two.");
break;
case 3:
System.out.println("i is three.");
break;
default:
System.out.println("i is greater than 3.");
}
}
}
The output produced by this program is shown here:
i is zero.
i is one.
i is two.
i is three.
i is greater than 3.
i is greater than 3.
As you can see, each time through the loop, the statements associated with the case constant that matches i are executed. All others are bypassed. After i is greater than 3, no case statements match, so the default statement is executed. The break statement is optional. If you omit the break, execution will continue on into the next case. It is sometimes desirable to have multiple cases without break statements between them. For example, consider the following program:
// In a switch, break statements are optional.
class MissingBreak {
public static void main(String args[]) {
for(int i=0; i<12; i++)
switch(i) {
case 0:
case 1:
case 2:
case 3:
case 4:
System.out.println("i is less than 5");
break;
case 5:
case 6:
case 7:
case 8:
case 9:
System.out.println("i is less than 10");
break;
default:
System.out.println("i is 10 or more");
}
}
}
This program generates the following output:
i is less than 5
i is less than 5
i is less than 5
i is less than 5
i is less than 5
i is less than 10
i is less than 10
i is less than 10
i is less than 10
i is less than 10
i is 10 or more
i is 10 or more
As you can see, execution falls through each case until a break statement (or the end of
the switch) is reached.
While the preceding example is, of course, contrived for the sake of illustration, omitting the break statement has many practical applications in real programs. To sample its more realistic usage, consider the following rewrite of the season example shown earlier. This version uses a switch to provide a more efficient implementation.
// An improved version of the season program.
class Switch {
public static void main(String args[]) {
int month = 4;
String season;
switch (month) {
case 12:
case 1:
case 2:
season = "Winter";
break;
case 3:
case 4:
case 5:
season = "Spring";
break;
case 6:
case 7:
case 8:
season = "Summer";
break;
case 9:
case 10:
case 11:
season = "Autumn";
break;
default:
season = "Bogus Month";
}
System.out.println("April is in the " + season + ".");
}
}
LANGUAGE
Nested switch Statements
You can use a switch as part of the statement sequence of an outer switch. This is called a nested switch. Since a switch statement defines its own block, no conflicts arise between the case constants in the inner switch and those in the outer switch. For example, the following fragment is perfectly valid:
switch(count) {
case 1:
switch(target) { // nested switch
case 0:
System.out.println("target is zero");
break;
case 1: // no conflicts with outer switch
System.out.println("target is one");
break;
}
break;
case 2: // ...

















Lecture Notes – 8

Topic:1. Iteration Statements
2. Constant

LANGUAGE
Iteration Statements
Java’s iteration statements are for, while, and do-while. These statements create what we commonly call loops. As you probably know, a loop repeatedly executes the same set of instructions until a termination condition is met. As you will see, Java has a loop to fit any programming need.
while
The while loop is Java’s most fundamental looping statement. It repeats a statement or block while its controlling expression is true. Here is its general form:
while(condition) {
// body of loop
}
The condition can be any Boolean expression. The body of the loop will be executed as
long as the conditional expression is true. When condition becomes false, control passes
to the next line of code immediately following the loop. The curly braces are unnecessary
if only a single statement is being repeated.
Here is a while loop that counts down from 10, printing exactly ten lines of “tick”:
// Demonstrate the while loop.
class While {
public static void main(String args[]) {
int n = 10;
while(n > 0) {
System.out.println("tick " + n);
n--;
}
}
}
When you run this program, it will “tick” ten times:
tick 10
tick 9
tick 8
tick 7
tick 6
tick 5
tick 4
tick 3
tick 2
tick 1
Since the while loop evaluates its conditional expression at the top of the loop, the body of the loop will not execute even once if the condition is false to begin with. For example, in the following fragment, the call to println( ) is never executed:
int a = 10, b = 20;
while(a > b)
System.out.println("This will not be displayed");
The body of the while (or any other of Java’s loops) can be empty. This is because
a null statement (one that consists only of a semicolon) is syntactically valid in Java. For
example, consider the following program:
// The target of a loop can be empty.
class NoBody {
public static void main(String args[]) {
int i, j;
i = 100;
j = 200;
// find midpoint between i and j
while(++i < --j) ; // no body in this loop
System.out.println("Midpoint is " + i);
}
}
This program finds the midpoint between i and j. It generates the following output: Midpoint is 150
Here is how the while loop works. The value of i is incremented, and the value of j is decremented. These values are then compared with one another. If the new value of i is still less than the new value of j, then the loop repeats. If i is equal to or greater than j, the loop stops. Upon exit from the loop, i will hold a value that is midway between the original values of i and j. (Of course, this procedure only works when i is less than j to begin with.) As you can see, there is no need for a loop body; all of the action occurs within the conditional expression, itself. In professionally written Java code, short loops are frequently coded without bodies when the controlling expression can handle all of
the details itself.
do-while
As you just saw, if the conditional expression controlling a while loop is initially false, then the body of the loop will not be executed at all. However, sometimes it is desirable to execute the body of a while loop at least once, even if the conditional expression is false to begin with. In other words, there are times when you would like to test the termination expression at the end of the loop rather than at the beginning. Fortunately, Java supplies a loop that does just that: the do-while. The do-while loop always executes its body at least once, because its conditional expression is at the bottom of the loop. Its
general form is
do {
// body of loop
} while (condition);
Each iteration of the do-while loop first executes the body of the loop and then evaluates the conditional expression. If this expression is true, the loop will repeat. Otherwise, the loop terminates. As with all of Java’s loops, condition must be a Boolean expression.
Here is a reworked version of the “tick” program that demonstrates the do-while
loop. It generates the same output as before.
// Demonstrate the do-while loop.
class DoWhile {
public static void main(String args[]) {
int n = 10;
do {
System.out.println("tick " + n);
n--;
} while(n > 0);
}
}
The loop in the preceding program, while technically correct, can be written more
efficiently as follows:
do {
System.out.println("tick " + n);
} while(--n > 0);
JAVA
LANGUAGE
In this example, the expression (– –n > 0) combines the decrement of n and the test for zero into one expression. Here is how it works. First, the – –n statement executes, decrementing n and returning the new value of n. This value is then compared with zero. If it is greater than zero, the loop continues; otherwise it terminates.
The do-while loop is especially useful when you process a menu selection, because you will usually want the body of a menu loop to execute at least once. Consider the following program which implements a very simple help system for Java’s selection and iteration statements:
// Using a do-while to process a menu selection
class Menu {
public static void main(String args[])
throws java.io.IOException {
char choice;
do {
System.out.println("Help on:");
System.out.println(" 1. if");
System.out.println(" 2. switch");
System.out.println(" 3. while");
System.out.println(" 4. do-while");
System.out.println(" 5. for\n");
System.out.println("Choose one:");
choice = (char) System.in.read();
} while( choice < '1' choice > '5');
System.out.println("\n");
switch(choice) {
case '1':
System.out.println("The if:\n");
System.out.println("if(condition) statement;");
System.out.println("else statement;");
break;
case '2':
System.out.println("The switch:\n");
System.out.println("switch(expression) {");
System.out.println(" case constant:");
System.out.println(" statement sequence");
System.out.println(" break;");
System.out.println(" // ...");
System.out.println("}");
break;
case '3':
System.out.println("The while:\n");
System.out.println("while(condition) statement;");
break;
case '4':
System.out.println("The do-while:\n");
System.out.println("do {");
System.out.println(" statement;");
System.out.println("} while (condition);");
break;
case '5':
System.out.println("The for:\n");
System.out.print("for(init; condition; iteration)");
System.out.println(" statement;");
break;
}
}
}
Here is a sample run produced by this program:
Help on:
1. if
2. switch
3. while
4. do-while
5. for
Choose one:
4
The do-while:
do {
statement;
} while (condition);

In the program, the do-while loop is used to verify that the user has entered a valid choice. If not, then the user is reprompted. Since the menu must be displayed at least once, the do-while is the perfect loop to accomplish this. A few other points about this example: Notice that characters are read from the
keyboard by calling System.in.read( ). This is one of Java’s console input functions. Although Java’s console I/O methods won’t be discussed in detail until Chapter 12, System.in.read( ) is used here to obtain the user’s choice. It reads characters from standard input (returned as integers, which is why the return value was cast to char).
By default, standard input is line buffered, so you must press ENTER before any characters that you type will be sent to your program. Java’s console input is quite limited and awkward to work with. Further, most real-world Java programs and applets will be graphical and window-based. For these
reasons, not much use of console input has been made in this book. However, it is useful in this context. One other point: Because System.in.read( ) is being used, the program must specify the throws java.io.IOException clause. This line is necessary to handle input errors. It is part of.



for
You were introduced to a simple form of the for loop in Chapter 2. As you will see, it is a powerful and versatile construct. Here is the general form of the for statement:
for(initialization; condition; iteration) {
// body
}
If only one statement is being repeated, there is no need for the curly braces. The for loop operates as follows. When the loop first starts, the initialization portion of the loop is executed. Generally, this is an expression that sets the value of the loop control variable, which acts as a counter that controls the loop. It is important to understand that the initialization expression is only executed once. Next, condition is evaluated.
Here is a version of the “tick” program that uses a for loop:
JAVA
LANGUAGE
// Demonstrate the for loop.
class ForTick {
public static void main(String args[]) {
int n;
for(n=10; n>0; n--)
System.out.println("tick " + n);
}
}
Declaring Loop Control Variables Inside the for Loop
:
// Declare a loop control variable inside the for.
class ForTick {
public static void main(String args[]) {
// here, n is declared inside of the for loop
for(int n=10; n>0; n--)
System.out.println("tick " + n);
}
}
In a for loop, there is one important point to remember: the scope of that variable ends when the for statement does. (That is, the scope of the variable is limited to the for loop.) Outside the for loop, the variable will cease to exist. If you need to use the loop control variable elsewhere in your program, you will not be able to declare it inside the for loop.
When the loop control variable will not be needed elsewhere, most Java programmers declare it inside the for. For example, here is a simple program that tests for prime numbers. Notice that the loop control variable, i, is declared inside the for since it is not needed elsewhere.
// Test for primes.
class FindPrime {
public static void main(String args[]) {
int num;
boolean isPrime = true;
num = 14;
for(int i=2; i <= num/2; i++) {
if((num % i) == 0) {
isPrime = false;
break;
}
}
if(isPrime) System.out.println("Prime");
else System.out.println("Not Prime");
}
}
Using the Comma
There will be times when you will want to include more than one statement in the initialization and iteration portions of the for loop. For example, consider the loop in the following program:
class Sample {
public static void main(String args[]) {
int a, b;
b = 4;
for(a=1; aSystem.out.println("a = " + a);
System.out.println("b = " + b);
b--;
}
}
}
As you can see, the loop is controlled by the interaction of two variables. Since the loop is governed by two variables, it would be useful if both could be included in the for statement, itself, instead of b being handled manually. Fortunately, Java provides a way to accomplish this. To allow two or more variables to control a for loop, Java permits you to include multiple statements in both the initialization and iteration portions of the for. Each statement is separated from the next by a comma.
Using the comma, the preceding for loop can be more efficiently coded as shown here:
// Using the comma.
class Comma {
public static void main(String args[]) {
int a, b;
for(a=1, b=4; aSystem.out.println("a = " + a);
System.out.println("b = " + b);
}
}
}
In this example, the initialization portion sets the values of both a and b. The two comma-separated statements in the iteration portion are executed each time the loop repeats. The program generates the following output:
a = 1
b = 4
a = 2
b = 3
If you are familiar with C/C++, then you know that in those languages the comma is an operator that can be used in any valid expression. However, this is not the case with Java. In Java, the comma is a separator that applies only to the for loop.Some for Loop Variations The for loop supports a number of variations that increase its power and applicability. The reason it is so flexible is that its three parts, the initialization, the conditional test, and the iteration, do not need to be used for only those purposes. In fact, the three sections
of the for can be used for any purpose you desire. Let’s look at some examples. One of the most common variations involves the conditional expression. Specifically, this expression does not need to test the loop control variable against some target value. In fact, the condition controlling the for can be any Boolean expression. For example,
consider the following fragment:
boolean done = false;
for(int i=1; !done; i++) {
LANGUAGE
// ...
if(interrupted()) done = true;
}
In this example, the for loop continues to run until the boolean variable done is set to true. It does not test the value of i. Here is another interesting for loop variation. Either the initialization or the iteration expression or both may be absent, as in this next program:
// Parts of the for loop can be empty.
class ForVar {
public static void main(String args[]) {
int i;
boolean done = false;
i = 0;
for( ; !done; ) {
System.out.println("i is " + i);
if(i == 10) done = true;
i++;
}
}
}
Here, the initialization and iteration expressions have been moved out of the for. Thus, parts of the for are empty. While this is of no value in this simple example—indeed, it would be considered quite poor style—there can be times when this type of approach makes sense. For example, if the initial condition is set through a complex expression elsewhere in the program or if the loop control variable changes in a nonsequential manner determined by actions that occur within the body of the loop, it may be
appropriate to leave these parts of the for empty. Here is one more for loop variation. You can intentionally create an infinite loop (a loop that never terminates) if you leave all three parts of the for empty. For example:
for( ; ; ) {
// ...
}
This loop will run forever, because there is no condition under which it will terminate. Although there are some programs, such as operating system command processors, that require an infinite loop, most “infinite loops” are really just loops with special termination requirements. As you will soon see, there is a way to terminate a loop— even an infinite loop like the one shown—that does not make use of the normal loop conditional expression.
Nested Loops
Like all other programming languages, Java allows loops to be nested. That is, one loop may be inside another. For example, here is a program that nests for loops: // Loops may be nested.
class Nested {
public static void main(String args[]) {
int i, j;
for(i=0; i<10; i++) {
for(j=i; j<10; j++)
System.out.print(".");
System.out.println();
}
}
}
The output produced by this program is shown here:
..........
.........
........
.......
......
.....
....
...
..
.
Jump Statements
Java supports three jump statements: break, continue, and return. These statements transfer control to another part of your program. Each is examined here. THE
JAVA
LANGUAGE
In addition to the jump statements discussed here, Java supports one other way that you can change your program’s flow of execution: through exception handling. Exception handling provides a structured method by which run-time errors can be trapped and handled by your program. It is supported by the keywords try, catch, throw, throws, and finally. In essence, the exception handling mechanism allows your program to
perform a nonlocal branch. Since exception handling is a large topic, it is discussed in its own chapter, Chapter 10. Using break
In Java, the break statement has three uses. First, as you have seen, it terminates a statement sequence in a switch statement. Second, it can be used to exit a loop. Third, it can be used as a “civilized” form of goto. The last two uses are explained here.
Using break to Exit a Loop By using break, you can force immediate termination of a loop, bypassing the
conditional expression and any remaining code in the body of the loop. When a break statement is encountered inside a loop, the loop is terminated and program control resumes at the next statement following the loop. Here is a simple example:
// Using break to exit a loop.
class BreakLoop {
public static void main(String args[]) {
for(int i=0; i<100; i++) {
if(i == 10) break; // terminate loop if i is 10
System.out.println("i: " + i);
}
System.out.println("Loop complete.");
}
}
This program generates the following output:
i: 0
i: 1
i: 2
i: 3
i: 4
i: 5
i: 6
i: 7
i: 8
E
i: 9
Loop complete.
As you can see, although the for loop is designed to run from 0 to 99, the break statement causes it to terminate early, when i equals 10. The break statement can be used with any of Java’s loops, including intentionally infinite loops. For example, here is the preceding program coded by use of a while
loop. The output from this program is the same as just shown.
// Using break to exit a while loop.
class BreakLoop2 {
public static void main(String args[]) {
int i = 0;
while(i < 100) {
if(i == 10) break; // terminate loop if i is 10
System.out.println("i: " + i);
i++;
}
System.out.println("Loop complete.");
}
}
When used inside a set of nested loops, the break statement will only break out of the innermost loop. For example:
// Using break with nested loops.
class BreakLoop3 {
public static void main(String args[]) {
for(int i=0; i<3; i++) {
System.out.print("Pass " + i + ": ");
for(int j=0; j<100; j++) {
if(j == 10) break; // terminate loop if j is 10
System.out.print(j + " ");
}
System.out.println();
}
System.out.println("Loops complete.");
}
}
This program generates the following output:
Pass 0: 0 1 2 3 4 5 6 7 8 9
Pass 1: 0 1 2 3 4 5 6 7 8 9
Pass 2: 0 1 2 3 4 5 6 7 8 9
Loops complete.
// Using break as a civilized form of goto.
class Break {
public static void main(String args[]) {
boolean t = true;
first: {
second: {
third: {
System.out.println("Before the break.");
if(t) break second; // break out of second block
System.out.println("This won't execute");
}
System.out.println("This won't execute");
}
System.out.println("This is after second block.");
}
}
}
Running this program generates the following output:
Before the break.
This is after second block.
One of the most common uses for a labeled break statement is to exit from nested
loops. For example, in the following program, the outer loop executes only once:
// Using break to exit from nested loops
class BreakLoop4 {
public static void main(String args[]) {
outer: for(int i=0; i<3; i++) {
System.out.print("Pass " + i + ": ");
for(int j=0; j<100; j++) {
if(j == 10) break outer; // exit both loops
System.out.print(j + " ");
}
System.out.println("This will not print");
}
System.out.println("Loops complete.");
}
}
This program generates the following output:
Pass 0: 0 1 2 3 4 5 6 7 8 9 Loops complete.
As you can see, when the inner loop breaks to the outer loop, both loops have been
terminated.
Keep in mind that you cannot break to any label which is not defined for an enclosing
block. For example, the following program is invalid and will not compile:
// This program contains an error.
class BreakErr {
public static void main(String args[]) {
one: for(int i=0; i<3; i++) {
System.out.print("Pass " + i + ": ");
}
for(int j=0; j<100; j++) {
if(j == 10) break one; // WRONG
System.out.print(j + " ");
}
}
}
Since the loop labeled one does not enclose the break statement, it is not possible to
transfer control to that block.
Using continue
Sometimes it is useful to force an early iteration of a loop. That is, you might want to continue running the loop, but stop processing the remainder of the code in its body for this particular iteration. This is, in effect, a goto just past the body of the loop, to the loop’s end. The continue statement performs such an action. In while and do-while loops, a continue statement causes control to be transferred directly to the conditional expression that controls the loop. In a for loop, control goes first to the iteration portion
JAVA
LANGUAGE
of the for statement and then to the conditional expression. For all three loops, any intermediate code is bypassed. Here is an example program that uses continue to cause two numbers to be printed
on each line:
// Demonstrate continue.
class Continue {
public static void main(String args[]) {
for(int i=0; i<10; i++) {
System.out.print(i + " ");
if (i%2 == 0) continue;
System.out.println("");
}
}
}
This code uses the % operator to check if i is even. If it is, the loop continues without printing a newline. Here is the output from this program:
0 1
2 3
4 5
6 7
8 9
As with the break statement, continue may specify a label to describe which enclosing loop to continue. Here is an example program that uses continue to print a triangular multiplication table for 0 through 9.
// Using continue with a label.
class ContinueLabel {
public static void main(String args[]) {
outer: for (int i=0; i<10; i++) {
for(int j=0; j<10; j++) {
if(j > i) {
System.out.println();
continue outer;
}
System.out.print(" " + (i * j));
}
}
System.out.println();
}
}
The continue statement in this example terminates the loop counting j and continues with the next iteration of the loop counting i. Here is the output of this program:
0
0 1
0 2 4
0 3 6 9
0 4 8 12 16
0 5 10 15 20 25
0 6 12 18 24 30 36
0 7 14 21 28 35 42 49
0 8 16 24 32 40 48 56 64
0 9 18 27 36 45 54 63 72 81
Good uses of continue are rare. One reason is that Java provides a rich set of loop statements which fit most applications. However, for those special circumstances in which early iteration is needed, the continue statement provides a structured way to accomplish it. Return The last control statement is return. The return statement is used to explicitly return from a method. That is, it causes program control to transfer back to the caller of the method. As such, it is categorized as a jump statement. Although a full discussion of
return must wait until methods are discussed in Chapter 7, a brief look at return is presented here.
At any time in a method the return statement can be used to cause execution to branch back to the caller of the method. Thus, the return statement immediately terminates the method in which it is executed. The following example illustrates this point. Here, return causes execution to return to the Java run-time system, since it is the run-time
system that calls main( ).
// Demonstrate return.
class Return {
public static void main(String args[]) {
boolean t = true;
System.out.println("Before the return.");
if(t) return; // return to caller
System.out.println("This won't execute.");
}
}
The output from this program is shown here:
Before the return.
As you can see, the final println( ) statement is not executed. As soon as return is executed, control passes back to the caller.
One last point: In the preceding program, the if(t) statement is necessary. Without it,
the Java compiler would flag an “unreachable code” error, because the compiler would
know that the last println( ) statement would never be executed. To prevent this error,
the if statement is used here to trick the compiler for the sake of this demonstration.
GE

1. Constant:
Java does not directly support constants. However, a static final variable is effectively a constant.
The static modifier causes the variable to be available without loading an instance of the class where it is defined.
The final modifier causes the variable to be unchangeable. Naming standards for Java constants Java constants are normally declared in ALL CAPS.
Words in Java constants are normally separated by underscores.
Sample Java constant Declarationpublic class MAX_UNITS { public static final int MAX_UNITS = 25;














Lecture Notes – 9
Topic:
Methods
2. Method Overloading and Overriding

Introducing Methods
classes usually consist of two things: instance variables and methods. The topic of methods is a large one because Java gives them so much power and flexibility. In fact, much of the next chapter is devoted to
methods. However, there are some fundamentals that you need to learn now so that you can begin to add methods to your classes.
This is the general form of a method:
type name(parameter-list) {
// body of method
}
Here, type specifies the type of data returned by the method. This can be any valid type, including class types that you create. If the method does not return a value, its return type must be void. The name of the method is specified by name. This can be any legal identifier other than those already used by other items within the current scope. The parameter-list is a sequence of type and identifier pairs separated by commas. Parameters
are essentially variables that receive the value of the arguments passed to the method when it is called. If the method has no parameters, then the parameter list will be empty. Methods that have a return type other than void return a value to the calling routine using the following form of the return statement:
return value;
Here, value is the value returned.
In the next few sections, you will see how to create various types of methods, including those that take parameters and those that return values. Adding a Method to the Box Class Although it is perfectly fine to create a class that contains only data, it rarely happens. Most of the time you will use methods to access the instance variables defined by the class. In fact, methods define the interface to most classes. This allows the class implementor to hide the specific layout of internal data structures behind cleaner method abstractions.
In addition to defining methods that provide access to data, you can also define methods that are used internally by the class itself.
Let’s begin by adding a method to the Box class. It may have occurred to you while looking at the preceding programs that the computation of a box’s volume was something that was best handled by the Box class rather than the BoxDemo class. After all, since the volume of a box is dependent upon the size of the box, it makes sense to have the Box class compute it. To do this, you must add a method to Box, as shown here:
// This program includes a method inside the box class.
class Box {
double width;
double height;
double depth;
// display volume of a box
void volume() {
System.out.print("Volume is ");
System.out.println(width * height * depth);
}
}
class BoxDemo3 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
// assign values to mybox1's instance variables
mybox1.width = 10;
mybox1.height = 20;
mybox1.depth = 15;
/* assign different values to mybox2's
instance variables */
mybox2.width = 3;
mybox2.height = 6;
mybox2.depth = 9;
// display volume of first box
mybox1.volume();
// display volume of second box
mybox2.volume();
}
}
This program generates the following output, which is the same as the previous version.
JAVA
LANGUAGE
Volume is 3000.0
Volume is 162.0
Look closely at the following two lines of code:
mybox1.volume();
mybox2.volume();
The first line here invokes the volume( ) method on mybox1. That is, it calls volume( ) relative to the mybox1 object, using the object’s name followed by the dot operator. Thus, the call to mybox1.volume( ) displays the volume of the box defined by mybox1, and the call to mybox2.volume( ) displays the volume of the box defined by mybox2. Each time volume( ) is invoked, it displays the volume for the specified box. If you are unfamiliar with the concept of calling a method, the following discussion will help clear things up. When mybox1.volume( ) is executed, the Java run-time system transfers control to the code defined inside volume( ). After program wanted to know the volume of a box, but not display its value? A better way to implement volume( ) is to have it compute the volume of the
box and return the result to the caller. The following example, an improved version of the preceding program, does just that:
JAVA
LANGUAGE
// Now, volume() returns the volume of a box.
class Box {
double width;
double height;
double depth;
// compute and return volume
double volume() {
return width * height * depth;
}
}
class BoxDemo4 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
// assign values to mybox1's instance variables
mybox1.width = 10;
mybox1.height = 20;
mybox1.depth = 15;
/* assign different values to mybox2's
instance variables */
mybox2.width = 3;
mybox2.height = 6;
mybox2.depth = 9;
// get volume of first box
vol = mybox1.volume();
System.out.println("Volume is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
}
As you can see, when volume( ) is called, it is put on the right side of an assignment statement. On the left is a variable, in this case vol, that will receive the value returned by volume( ). Thus, after
vol = mybox1.volume();
executes, the value of mybox1.volume( ) is 3,000 and this value then is stored in vol. There are two important things to understand about returning values: ¦ The type of data returned by a method must be compatible with the return type specified by the method. For example, if the return type of some method is
boolean, you could not return an integer. ¦ The variable receiving the value returned by a method (such as vol, in this case)
must also be compatible with the return type specified for the method. One more point: The preceding program can be written a bit more efficiently because there is actually no need for the vol variable. The call to volume( ) could have been used in the println( ) statement directly, as shown here:
// This program uses a parameterized method.
class Box {
double width;
double height;
double depth;
// compute and return volume
double volume() {
return width * height * depth;
}
// values. This version is shown here:
/* Here, Box uses a constructor to initialize the
dimensions of a box.
*/
class Box {
double width;
double height;
THE
JAVA
LANGUAGE
double depth;
// This is the constructor for Box.
Box()
{
System.out.println("Constructing Box");
width = 10;
height = 10;
depth = 10;
}
// compute and return volume
double volume() {
return width * height * depth;
}
}
class BoxDemo6 {
public static void main(String args[]) {
// declare, allocate, and initialize Box objects
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
// get volume of first box
vol = mybox1.volume();
System.out.println("Volume is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
}
When this program is run, it generates the following results:
Constructing Box
Constructing Box
Volume is 1000.0
Volume is 1000.0
2. Overriding Methods
Definitions
In the definition of the class Student, we added a method called writeOutput that has no parameters. But, the class Person also has a method called writeOutput that has no parameters. If the class Student were to inherit the method writeOutput from the base class Person, then Student would contain two methods with the name writeOutput, both of which have no parameters. Java has a rule to avoid this problem. If a derived class defines a method with the same name as a method in the base class and that also has the same number and types of parameters as in the base class, then the definition in the derived class is said to override the definition in the base class, and the definition in the derived class is the one that is used for objects of the derived class.
For example, in Display 3, the following invocation of the method writeOutput for the object s of the class Student will use the definition of writeOutput in the class Student, not the definition of the method writeOutput in the class Person:s.writeOutput();
Although you can change the body of an overridden method definition to anything you wish, you cannot make any changes in the heading of the overridden method. In particular, when overriding a method definition, you cannot change the return type of the method.
Overriding versus Overloading
Do not confuse method overriding with method overloading. When you override a method definition, the new method definition given in the derived class has the exact same number and types of parameters. On the other hand, if the method in the derived class were to have a different number of parameters or a parameter of a different type from the method in the base class, then the derived class would have both methods. That would be overloading. For example, suppose we added the following method to the definition of the class Student:public String getName(String title){ return (title + getName());}
In this case, the class Student would have two methods named getName: It would still inherit the method getName from the base class Person (refer to Display 1), and it would also have the method named getName that we just defined. This is because these two methods called getName have different numbers of parameters.
If you get overloading and overriding confused, you do have one small consolation. They are both legal. So, it is more important to learn how to use them than it is to learn to distinguish between them. Nonetheless, you should learn the difference between them














Lecture Notes –10
Topic:
1. Classes
2. Objects

2. What Is a Class?
The class forms the basis for object-oriented programming in Java. Any concept you wish to implement in a Java program must be encapsulated within a class. Because the class is so fundamental to Java, this and the next few chapters will be devoted to it. Here, you will be introduced to the basic elements of a class and learn how a class can be used to create objects. You will also learn about methods, constructors,
and the this keyword.
Class Fundamentals
Classes have been used since the beginning of this book. However, until now, only the most rudimentary form of a class has been used. The classes created in the preceding chapters primarily exist simply to encapsulate the main( ) method, which has been used to demonstrate the basics of the Java syntax. As you will see, classes are substantially more powerful than the limited ones presented so far. Perhaps the most important thing to understand about a class is that it defines a new data type. Once defined, this new type can be used to create objects of that type. Thus, a class is a template for an object, and an object is an instance of a class. Because an object is an instance of a class, you will often see the two words object and instance used interchangeably.
The General Form of a Class
When you define a class, you declare its exact form and nature. You do this by specifying the data that it contains and the code that operates on that data. While very simple classes may contain only code or only data, most real-world classes contain both. As you will see, a class’ code defines the interface to its data.
A class is declared by use of the class keyword. The classes that have been used up to this point are actually very limited examples of its complete form. Classes can (and usually do) get much more complex. The general form of a class definition is shown here:
class classname {
type instance-variable1;
type instance-variable2;
// ...
type instance-variableN;
type methodname1(parameter-list) {
// body of method
}
type methodname2(parameter-list) {
// body of method
NGUAGE
}
// ...
type methodnameN(parameter-list) {
// body of method
}
}
The data, or variables, defined within a class are called instance variables. The code is contained within methods. Collectively, the methods and variables defined within a class are called members of the class. In most classes, the instance variables are acted upon and accessed by the methods defined for that class. Thus, it is the methods that determine how a class’ data can be used. Variables defined within a class are called instance variables because each instance of the class (that is, each object of the class) contains its own copy of these variables. Thus, the data for one object is separate and unique from the data for another. We will come back to this point shortly, but it is an important concept to learn early.
All methods have the same general form as main( ), which we have been using thus far. However, most methods will not be specified as static or public. Notice that the general form of a class does not specify a main( ) method. Java classes do not need to have a main( ) method. You only specify one if that class is the starting point for your program. Further, applets don’t require a main( ) method at all. C++ programmers will notice that the class declaration and the implementation of the methods are stored in the same place and not defined separately. This sometimes makes
for very large .java files, since any class must be entirely defined in a single source file. This design feature was built into Java because it was felt that in the long run, having specification, declaration, and implementation all in one place makes for code that is easier to maintain.
A Simple Class
Let’s begin our study of the class with a simple example. Here is a class called Box that
defines three instance variables: width, height, and depth. Currently, Box does not contain
any methods (but some will be added soon).
class Box {
double width;
double height;
double depth;
}
Object:
Object is a run time entity which used to acces the class Instance members, and has a physical existence.
As stated, a class defines a new type of data. In this case, the new data type is called Box. You will use this name to declare objects of type Box. It is important to remember that a class declaration only creates a template; it does not create an actual object. Thus, the preceding code does not cause any objects of type Box to come into existence. To actually create a Box object, you will use a statement like the following:

Box mybox = new Box(); // create a Box object called mybox

:/* A program that uses the Box class.
Call this file BoxDemo.java
*/
class Box {
double width;
double height;
double depth;
}
// This class declares an object of type Box.
class BoxDemo {
public static void main(String args[]) {
Box mybox = new Box();
double vol;
// assign values to mybox's instance variables
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;
// compute volume of box
vol = mybox.width * mybox.height * mybox.depth;
System.out.println("Volume is " + vol);
}
}
the following output:
Volume is 3000.0


As stated earlier, each object has its own copies of the instance variables. This means that if you have two Box objects, each has its own copy of depth, width, and height. It is important to understand that changes to the instance variables of one object have no effect on the instance variables of another. For example, the following
program declares two Box objects:
// This program declares two Box objects.
class Box {
double width;
double height;
double depth;
}
class BoxDemo2 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();

THE
JAVA
LANGUAGE
double vol;
// assign values to mybox1's instance variables
mybox1.width = 10;
mybox1.height = 20;
mybox1.depth = 15;
/* assign different values to mybox2's
instance variables */
mybox2.width = 3;
mybox2.height = 6;
mybox2.depth = 9;
// compute volume of first box
vol = mybox1.width * mybox1.height * mybox1.depth;
System.out.println("Volume is " + vol);
// compute volume of second box
vol = mybox2.width * mybox2.height * mybox2.depth;
System.out.println("Volume is " + vol);
}
}
The output produced by this program is shown here:
Volume is 3000.0
Volume is 162.0
As you can see, mybox1’s data is completely separate from the data contained
in mybox2

.
Declaring Objects
As just explained, when you create a class, you are creating a new data type. You can use this type to declare objects of that type. However, obtaining objects of a class is a two-step process. First, you must declare a variable of the class type. This variable does not define an object. Instead, it is simply a variable that can refer to an object. Second, you must acquire an actual, physical copy of the object and assign it to that variable. You can do this using the new operator. The new operator dynamically allocates (that is, allocates at run time) memory for an object and returns a reference to it. This
reference is, more or less, the address in memory of the object allocated by new. This reference is then stored in the variable. Thus, in Java, all class objects must be dynamically allocated. Let’s look at the details of this procedure. In the preceding sample programs, a line similar to the following is used to declare
an object of type Box:
Box mybox = new Box();
This statement combines the two steps just described. It can be rewritten like this to show each step more clearly:
Box mybox; // declare reference to object
mybox = new Box(); // allocate a Box object
The first line declares mybox as a reference to an object of type Box. After this line executes, mybox contains the value null, which indicates that it does not yet point to an actual object. Any attempt to use mybox at this point will result in a compile-time error. The next line allocates an actual object and assigns a reference to it to mybox. After the second line executes, you can use mybox as if it were a Box object. But in
reality, mybox simply holds the memory address of the actual Box object. The effect of these two lines of code is depicted in Figure 6-1.
JAVA
LANGUAGE
Assigning Object Reference Variables
Object reference variables act differently than you might expect when an assignment takes place. For example, what do you think the following fragment does?
Box b1 = new Box();
Box b2 = b1;
You might think that b2 is being assigned a reference to a copy of the object referred to by b1. That is, you might think that b1 and b2 refer to separate and distinct objects. However, this would be wrong. Instead, after this fragment executes, b1 and b2 will both refer to the same object. The assignment of b1 to b2 did not allocate any memory or copy any part of the original object. It simply makes b2 refer to the same object as does b1. Thus, any changes made to the object through b2 will affect the object to which b1 is referring, since they are the same object.
This situation is depicted here:
Although b1 and b2 both refer to the same object, they are not linked in any other way. For example, a subsequent assignment to b1 will simply unhook b1 from the original object without affecting the object or affecting b2. For example:
Box b1 = new Box();
Box b2 = b1;
// ...
b1 = null;
Here, b1 has been set to null, but b2 still points to the original object. When you assign one object reference variable to another object reference variable, you are not creating a copy of the object, you are only making a copy of the reference.
LANGUAGE



Lecture Notes –11

Topc: 1. Staring Handling


Staring Handling

Implementing strings as built-in objects allows Java to provide a full complement of features that make string handling convenient. For example, Java has methods to compare two strings, search for a substring, concatenate two strings, and change the case of letters within a string. Also, String objects can be constructed a number of ways, making it easy to obtain a string when needed.
Somewhat unexpectedly, when you create a String object, you are creating a string that cannot be changed. That is, once a String object has been created, you cannot change the characters that comprise that string. At first, this may seem to be a serious restriction. However, such is not the case. You can still perform all types of string operations. The
difference is that each time you need an altered version of an existing string, a new String object is created that contains the modifications. The original string is left unchanged. This approach is used because fixed, immutable strings can be implemented more efficiently than changeable ones. For those cases in which a modifiable string is desired, there is a companion class to String called StringBuffer, whose objects contain strings that can be modified after they are created.
Both the String and StringBuffer classes are defined in java.lang. Thus, they are available to all programs automatically. Both are declared final, which means that neither of these classes may be subclassed. This allows certain optimizations that increase
performance to take place on common string operations. Beginning with Java 2, version 1.4, both String and StringBuffer implement the CharSequence interface. One last point: To say that the strings within objects of type String are unchangeable means that the contents of the String instance cannot be changed after it has been created. However, a variable declared as a String reference can be changed to
point at some other String object at any time. The String Constructors The String class supports several constructors. To create an empty String, you call the default constructor. For example,
String s = new String();
will create an instance of String with no characters in it. Frequently, you will want to create strings that have initial values. The String class provides a variety of constructors to handle this. To create a String initialized by an array of characters, use the constructor shown here:
String(char chars[ ])
Here is an example:
char chars[] = { 'a', 'b', 'c' };
String s = new String(chars);
This constructor initializes s with the string “abc”.
You can specify a subrange of a character array as an initializer using the following constructor:
String(char chars[ ], int startIndex, int numChars) Here, startIndex specifies the index at which the subrange begins, and numChars specifies the number of characters to use. Here is an example:
char chars[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
String s = new String(chars, 2, 3);
This initializes s with the characters cde.
You can construct a String object that contains the same character sequence as another String object using this constructor:
String(String strObj)
Here, strObj is a String object. Consider this example:
// Construct one String from another.
class MakeString {
public static void main(String args[]) {
char c[] = {'J', 'a', 'v', 'a'};
String s1 = new String(c);
String s2 = new String(s1);
System.out.println(s1);
System.out.println(s2);
}
}
The output from this program is as follows:
Java
Java
As you can see, s1 and s2 contain the same string.
Even though Java’s char type uses 16 bits to represent the Unicode character set, the typical format for strings on the Internet uses arrays of 8-bit bytes constructed from the ASCII character set. Because 8-bit ASCII strings are common, the String class provides constructors that initialize a string when given a byte array. Their forms are shown here: String(byte asciiChars[ ])
String(byte asciiChars[ ], int startIndex, int numChars) Here, asciiChars specifies the array of bytes. The second form allows you to specify a subrange. In each of these constructors, the byte-to-character conversion is done by using the default character encoding of the platform. The following program illustrates
these constructors:
// Construct string from subset of char array.
class SubStringCons {
public static void main(String args[]) {
byte ascii[] = {65, 66, 67, 68, 69, 70 };
String s1 = new String(ascii);
System.out.println(s1);
String s2 = new String(ascii, 2, 3);
System.out.println(s2);
}
}
This program generates the following output:
ABCDEF
CDE
Extended versions of the byte-to-string constructors are also defined in which you
can specify the character encoding that determines how bytes are converted to
characters. However, most of the time, you will want to use the default encoding
provided by the platform.
The contents of the array are copied whenever you create a String object from an array.
If you modify the contents of the array after you have created the string, the String will
be unchanged.
String Length
The length of a string is the number of characters that it contains. To obtain this value,
call the length( ) method, shown here:
int length( )
The following fragment prints “3”, since there are three characters in the string s:
char chars[] = { 'a', 'b', 'c' };
String s = new String(chars);
System.out.println(s.length());
Special String Operations
Because strings are a common and important part of programming, Java has added special support for several string operations within the syntax of the language. These operations include the automatic creation of new String instances from string literals, concatenation of multiple String objects by use of the + operator, and the conversion of
other data types to a string representation. There are explicit methods available to perform all of these functions, but Java does them automatically as a convenience for the programmer and to add clarity.
String Literals
The earlier examples showed how to explicitly create a String instance from an array of characters by using the new operator. However, there is an easier way to do this using a string literal. For each string literal in your program, Java automatically constructs a String object. Thus, you can use a string literal to initialize a String object. For example, the following code fragment creates two equivalent strings:
char chars[] = { 'a', 'b', 'c' };
String s1 = new String(chars);
String s2 = "abc"; // use string literal
Because a String object is created for every string literal, you can use a string literal any place you can use a String object. For example, you can call methods directly on a quoted string as if it were an object reference, as the following statement shows. It calls the length( ) method on the string “abc”. As expected, it prints “3”.
System.out.println("abc".length());
String Concatenation
In general, Java does not allow operators to be applied to String objects. The one exception to this rule is the + operator, which concatenates two strings, producing a String object as the result. This allows you to chain together a series of + operations. For example, the following fragment concatenates three strings:
String age = "9";
String s = "He is " + age + " years old.";
System.out.println(s);
This displays the string “He is 9 years old.”
One practical use of string concatenation is found when you are creating very long strings. Instead of letting long strings wrap around within your source code, you can break them into smaller pieces, using the + to concatenate them. Here is an example: // Using concatenation to prevent long lines.
class ConCat {
public static void main(String args[]) {
String longStr = "This could have been " +
"a very long line that would have " +
"wrapped around. But string concatenation " +
"prevents this.";
System.out.println(longStr);
}
}
String Concatenation with Other Data Types
You can concatenate strings with other types of data. For example, consider this slightly different version of the earlier example:
int age = 9;
String s = "He is " + age + " years old.";
System.out.println(s);
In this case, age is an int rather than another String, but the output produced is the same as before. This is because the int value in age is automatically converted into its string representation within a String object. This string is then concatenated as before. The compiler will convert an operand to its string equivalent whenever the other operand of the + is an instance of String. Be careful when you mix other types of operations with string concatenation expressions, however. You might get surprising results. Consider the following:
String s = "four: " + 2 + 2;
System.out.println(s);
This fragment displays
four: 22
rather than the
four: 4
that you probably expected. Here’s why. Operator precedence causes the concatenation of “four” with the string equivalent of 2 to take place first. This result is then concatenated with the string equivalent of 2 a second time. To complete the integer addition first, you must use parentheses, like this:
String s = "four: " + (2 + 2);
Now s contains the string “four: 4”.
String Conversion and toString( )
When Java converts data into its string representation during concatenation, it does so by calling one of the overloaded versions of the string conversion method valueOf( ) defined by String. valueOf( ) is overloaded for all the simple types and for type Object. For the simple types, valueOf( ) returns a string that contains the human-readable equivalent of the value with which it is called. For objects, valueOf( ) calls the toString( ) method on the object. We will look more closely at valueOf( ) later in this
chapter. Here, let’s examine the toString( ) method, because it is the means by which you can determine the string representation for objects of classes that you create. Every class implements toString( ) because it is defined by Object. However, the default implementation of toString( ) is seldom sufficient. For most important classes that you create, you will want to override toString( ) and provide your own string representations. Fortunately, this is easy to do. The toString( ) method has this
general form:
String toString( )
To implement toString( ), simply return a String object that contains the humanreadable string that appropriately describes an object of your class.
By overriding toString( ) for classes that you create, you allow them to be fully integrated into Java’s programming environment. For example, they can be used in print( ) and println( ) statements and in concatenation expressions. The following program demonstrates this by overriding toString( ) for the Box class:
// Override toString() for Box class.
class Box {
double width;
double height;
double depth;
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
public String toString() {
return "Dimensions are " + width + " by " +
depth + " by " + height + ".";
}
}
class toStringDemo {
public static void main(String args[]) {
Box b = new Box(10, 12, 14);
String s = "Box b: " + b; // concatenate Box object
System.out.println(b); // convert Box to string
System.out.println(s);
}
}
The output of this program is shown here:
Dimensions are 10.0 by 14.0 by 12.0
Box b: Dimensions are 10.0 by 14.0 by 12.0
As you can see, Box’s toString( ) method is automatically invoked when a Box object is used in a concatenation expression or in a call to println( ).
The String class provides a number of ways in which characters can be extracted from a String object. Each is examined here. Although the characters that comprise a string within a String object cannot be indexed as if they were a character array, many of the String methods employ an index (or offset) into the string for their operation. Like arrays, the string indexes begin at zero.
charAt( )
To extract a single character from a String, you can refer directly to an individual character via the charAt( ) method. It has this general form: char charAt(int where) Here, where is the index of the character that you want to obtain. The value of where must be nonnegative and specify a location within the string. charAt( ) returns the
character at the specified location. For example,
char ch;
ch = "abc".charAt(1);
assigns the value “b” to ch.
getChars( )
If you need to extract more than one character at a time, you can use the getChars( ) method. It has this general form: void getChars(int sourceStart, int sourceEnd, char target[ ], int targetStart)
Here, sourceStart specifies the index of the beginning of the substring, and sourceEnd specifies an index that is one past the end of the desired substring. Thus, the substring contains the characters from sourceStart through sourceEnd–1. The array that will receive the characters is specified by target. The index within target at which the substring will be copied is passed in targetStart. Care must be taken to assure that the target array is large enough to hold the number of characters in the specified substring.
The following program demonstrates getChars( ):
class getCharsDemo {
public static void main(String args[]) {
String s = "This is a demo of the getChars method.";
int start = 10;
int end = 14;
char buf[] = new char[end - start];
s.getChars(start, end, buf, 0);
System.out.println(buf);
}
}
Here is the output of this program:
demo
getBytes( )
There is an alternative to getChars( ) that stores the characters in an array of bytes. This method is called getBytes( ), and it uses the default character-to-byte conversions provided by the platform. Here is its simplest form:
byte[ ] getBytes( )
Other forms of getBytes( ) are also available. getBytes( ) is most useful when you are exporting a String value into an environment that does not support 16-bit Unicode characters. For example, most Internet protocols and text file formats use 8-bit ASCII for all text interchange. toCharArray( )
If you want to convert all the characters in a String object into a character array, the easiest way is to call toCharArray( ). It returns an array of characters for the entire string. It has this general form:
char[ ] toCharArray( )
This function is provided as a convenience, since it is possible to use getChars( ) to achieve the same result.























Lecture Notes –12

Topc: 1. Staring Comparison

String Comparison
The String class includes several methods that compare strings or substrings within
strings. Each is examined here.
equals( ) and equalsIgnoreCase( )
To compare two strings for equality, use equals( ). It has this general form: boolean equals(Object str)
Here, str is the String object being compared with the invoking String object. It returns true if the strings contain the same characters in the same order, and false otherwise. The comparison is case-sensitive.
To perform a comparison that ignores case differences, call equalsIgnoreCase( ). When it compares two strings, it considers A-Z to be the same as a-z. It has this general form:
boolean equalsIgnoreCase(String str)
Here, str is the String object being compared with the invoking String object. It, too, returns true if the strings contain the same characters in the same order, and false otherwise.
Here is an example that demonstrates equals( ) and equalsIgnoreCase( ):
// Demonstrate equals() and equalsIgnoreCase().
class equalsDemo {
public static void main(String args[]) {
String s1 = "Hello";
String s2 = "Hello";
String s3 = "Good-bye";
String s4 = "HELLO";
System.out.println(s1 + " equals " + s2 + " -> " +
s1.equals(s2));
System.out.println(s1 + " equals " + s3 + " -> " +
s1.equals(s3));
System.out.println(s1 + " equals " + s4 + " -> " +
s1.equals(s4));
System.out.println(s1 + " equalsIgnoreCase " + s4 + " -> " +
s1.equalsIgnoreCase(s4));
}
}
The output from the program is shown here:
Hello equals Hello -> true
Hello equals Good-bye -> false
Hello equals HELLO -> false
Hello equalsIgnoreCase HELLO -> true
regionMatches( )
The regionMatches( ) method compares a specific region inside a string with another specific region in another string. There is an overloaded form that allows you to ignore case in such comparisons. Here are the general forms for these two methods: boolean regionMatches(int startIndex, String str2,
int str2StartIndex, int numChars)
boolean regionMatches(boolean ignoreCase,
int startIndex, String str2,
int str2StartIndex, int numChars)
For both versions, startIndex specifies the index at which the region begins within the invoking String object. The String being compared is specified by str2. The index at which the comparison will start within str2 is specified by str2StartIndex. The length of the substring being compared is passed in numChars. In the second version, if ignoreCase is true, the case of the characters is ignored. Otherwise, case is significant. startsWith( ) and endsWith( ) String defines two routines that are, more or less, specialized forms of regionMatches( ). The startsWith( ) method determines whether a given String begins with a specified string. Conversely, endsWith( ) determines whether the String in
question ends with a specified string. They have the following general forms: boolean startsWith(String str)
boolean endsWith(String str)
Here, str is the String being tested. If the string matches, true is returned. Otherwise, false is returned. For example,
"Foobar".endsWith("bar")
and
"Foobar".startsWith("Foo")
are both true.
A second form of startsWith( ), shown here, lets you specify a starting point: boolean startsWith(String str, int startIndex)
Here, startIndex specifies the index into the invoking string at which point the search will begin. For example,
"Foobar".startsWith("bar", 3)
returns true.
equals( ) Versus ==
It is important to understand that the equals( ) method and the == operator perform two different operations. As just explained, the equals( ) method compares the characters inside a String object. The == operator compares two object references to see whether they refer to the same instance. The following program shows how two different String objects can contain the same characters, but references to these objects
will not compare as equal:
// equals() vs ==
class EqualsNotEqualTo {
public static void main(String args[]) {
String s1 = "Hello";
String s2 = new String(s1);
System.out.println(s1 + " equals " + s2 + " -> " +
s1.equals(s2));
System.out.println(s1 + " == " + s2 + " -> " + (s1 == s2));
}
}
The variable s1 refers to the String instance created by “Hello”. The object referred to by s2 is created with s1 as an initializer. Thus, the contents of the two String objects are identical, but they are distinct objects. This means that s1 and s2 do not refer to the same objects and are, therefore, not ==, as is shown here by the
output of the preceding example:
Hello equals Hello -> true
Hello == Hello -> false
compareTo( )
Often, it is not enough to simply know whether two strings are identical. For sorting applications, you need to know which is less than, equal to, or greater than the next. A string is less than another if it comes before the other in dictionary order. A string is greater than another if it comes after the other in dictionary order. The String method compareTo( ) serves this purpose. It has this general form:
int compareTo(String str)

Here, str is the String being compared with the invoking String. The result of the comparison is returned and is interpreted as shown here: Value Meaning Less than zero The invoking string is less than str.
Greater than zero The invoking string is greater than str. Zero The two strings are equal.
Here is a sample program that sorts an array of strings. The program uses compareTo( ) to determine sort ordering for a bubble sort:
// A bubble sort for Strings.
class SortString {
static String arr[] = {
"Now", "is", "the", "time", "for", "all", "good", "men",
"to", "come", "to", "the", "aid", "of", "their", "country"
};
public static void main(String args[]) {
for(int j = 0; j < arr.length; j++) {
for(int i = j + 1; i < arr.length; i++) {
if(arr[i].compareTo(arr[j]) < 0) {
String t = arr[j];
arr[j] = arr[i];
arr[i] = t;
}
}
System.out.println(arr[j]);
}
}
}
The output of this program is the list of words:
Now
aid
all
come
country
for
good
is
men
of
the
the
their
time
to
to
As you can see from the output of this example, compareTo( ) takes into account uppercase and lowercase letters. The word “Now” came out before all the others because it begins with an uppercase letter, which means it has a lower value in the ASCII character set. If you want to ignore case differences when comparing two strings, use compareToIgnoreCase( ), shown here:
int compareToIgnoreCase(String str)
This method returns the same results as compareTo( ), except that case differences are ignored. This method was added by Java 2. You might want to try substituting it into the previous program. After doing so, “Now” will no longer be first.
Searching Strings
The String class provides two methods that allow you to search a string for a specified character or substring:
¦ indexOf( ) Searches for the first occurrence of a character or substring. ¦ lastIndexOf( ) Searches for the last occurrence of a character or substring. These two methods are overloaded in several different ways. In all cases, the methods return the index at which the character or substring was found, or –1 on failure.
To search for the first occurrence of a character, use
int indexOf(int ch)
To search for the last occurrence of a character, use
int lastIndexOf(int ch)
Here, ch is the character being sought.
To search for the first or last occurrence of a substring, use
int indexOf(String str)
int lastIndexOf(String str)
Here, str specifies the substring.
You can specify a starting point for the search using these forms:
































Lecture Notes –13

Topc: 1. String Buffer


int indexOf(int ch, int startIndex)
int lastIndexOf(int ch, int startIndex)
int indexOf(String str, int startIndex)
int lastIndexOf(String str, int startIndex)
Here, startIndex specifies the index at which point the search begins. For indexOf( ),
the search runs from startIndex to the end of the string. For lastIndexOf( ), the search
runs from startIndex to zero.
The following example shows how to use the various index methods to search
inside of Strings:
// Demonstrate indexOf() and lastIndexOf().
class indexOfDemo {
public static void main(String args[]) {
String s = "Now is the time for all good men " +
"to come to the aid of their country.";
System.out.println(s);
System.out.println("indexOf(t) = " +
s.indexOf('t'));
System.out.println("lastIndexOf(t) = " +
s.lastIndexOf('t'));
System.out.println("indexOf(the) = " +
s.indexOf("the"));
System.out.println("lastIndexOf(the) = " +
s.lastIndexOf("the"));
System.out.println("indexOf(t, 10) = " +
s.indexOf('t', 10));
System.out.println("lastIndexOf(t, 60) = " +
s.lastIndexOf('t', 60));
System.out.println("indexOf(the, 10) = " +
s.indexOf("the", 10));
System.out.println("lastIndexOf(the, 60) = " +
s.lastIndexOf("the", 60));
}
}
Here is the output of this program:
Now is the time for all good men to come to the aid of their country.
indexOf(t) = 7
lastIndexOf(t) = 65
indexOf(the) = 7
lastIndexOf(the) = 55
indexOf(t, 10) = 11
lastIndexOf(t, 60) = 55
indexOf(the, 10) = 44
lastIndexOf(the, 60) = 55
Modifying a String
Because String objects are immutable, whenever you want to modify a String, you must either copy it into a StringBuffer or use one of the following String methods, which will construct a new copy of the string with your modifications complete.
substring( )
You can extract a substring using substring( ). It has two forms. The first is String substring(int startIndex)
Here, startIndex specifies the index at which the substring will begin. This form returns a copy of the substring that begins at startIndex and runs to the end of the invoking string. The second form of substring( ) allows you to specify both the beginning and ending index of the substring: String substring(int startIndex, int endIndex)
Here, startIndex specifies the beginning index, and endIndex specifies the stopping point. The string returned contains all the characters from the beginning index, up to, but not including, the ending index.
The following program uses substring( ) to replace all instances of one substring with another within a string:
// Substring replacement.
class StringReplace {
public static void main(String args[]) {
String org = "This is a test. This is, too.";
String search = "is";
String sub = "was";
String result = "";
int i;
do { // replace all matching substrings
System.out.println(org);
i = org.indexOf(search);
if(i != -1) {
result = org.substring(0, i);
result = result + sub;
result = result + org.substring(i + search.length());
org = result;
}
} while(i != -1);
}
}
The output from this program is shown here:
This is a test. This is, too.
Thwas is a test. This is, too.
Thwas was a test. This is, too.
Thwas was a test. Thwas is, too.
Thwas was a test. Thwas was, too.
concat( )
You can concatenate two strings using concat( ), shown here: String concat(String str) This method creates a new object that contains the invoking string with the contents of str appended to the end. concat( ) performs the same function as +. For example,
String s1 = "one";
String s2 = s1.concat("two");
puts the string “onetwo” into s2. It generates the same result as the following sequence:
String s1 = "one";
String s2 = s1 + "two";
replace( )
The replace( ) method replaces all occurrences of one character in the invoking string with another
String replace(char original, char replacement)
Here, original specifies the character to be replaced by the character specified by replacement. The resulting string is returned. For example,
String s = "Hello".replace('l', 'w');
puts the string “Hewwo” into s.
trim( )
The trim( ) method returns a copy of the invoking string from which any leading and trailing whitespace has been removed. It has this general form:
String trim( )
Here is an example:
String s = " Hello World ".trim();
This puts the string “Hello World” into s.
The trim( ) method is quite useful when you process user commands. For example, the following program prompts the user for the name of a state and then displays that state’s capital. It uses trim( ) to remove any leading or trailing whitespace that may have inadvertently been entered by the user.
// Using trim() to process commands.
import java.io.*;
class UseTrim {
public static void main(String args[])
throws IOException
{
// create a BufferedReader using System.in
BufferedReader br = new
BufferedReader(new InputStreamReader(System.in));
String str;
System.out.println("Enter 'stop' to quit.");
System.out.println("Enter State: ");
do {
str = br.readLine();
str = str.trim(); // remove whitespace
if(str.equals("Illinois"))
System.out.println("Capital is Springfield.");
else if(str.equals("Missouri"))
System.out.println("Capital is Jefferson City.");
else if(str.equals("California"))
System.out.println("Capital is Sacramento.");
else if(str.equals("Washington"))
System.out.println("Capital is Olympia.");
// ...
} while(!str.equals("stop"));
}
}
Data Conversion Using valueOf( )
The valueOf( ) method converts data from its internal format into a human-readable form. It is a static method that is overloaded within String for all of Java’s built-in types, so that each type can be converted properly into a string. valueOf( ) is also overloaded for type Object, so an object of any class type you create can also be used as an argument. (Recall that Object is a superclass for all classes.) Here are a few of its forms:
static String valueOf(double num)
static String valueOf(long num)
static String valueOf(Object ob)
static String valueOf(char chars[ ])
As we discussed earlier, valueOf( ) is called when a string representation of some other type of data is needed—for example, during concatenation operations. You can call this method directly with any data type and get a reasonable String representation. All of the simple types are converted to their common String representation. Any object that you pass to valueOf( ) will return the result of a call to the object’s toString( ) method. In
fact, you could just call toString( ) directly and get the same result. For most arrays, valueOf( ) returns a rather cryptic string, which indicates that it is an array of some type. For arrays of char, however, a String object is created that contains the characters in the char array. There is a special version of valueOf( ) that
allows you to specify a subset of a char array. It has this general form:
static String valueOf(char chars[ ], int startIndex, int numChars)
Here, chars is the array that holds the characters, startIndex is the index into the array of characters at which the desired substring begins, and numChars specifies the length of the substring.
Changing the Case of Characters
Within a String
The method toLowerCase( ) converts all the characters in a string from uppercase to lowercase. The toUpperCase( ) method converts all the characters in a string from lowercase to uppercase. Nonalphabetical characters, such as digits, are unaffected. Here are the general forms of these methods:
String toLowerCase( )
String toUpperCase( )
Both methods return a String object that contains the uppercase or lowercase
equivalent of the invoking String.
Here is an example that uses toLowerCase( ) and toUpperCase( ):
// Demonstrate toUpperCase() and toLowerCase().
class ChangeCase {
public static void main(String args[])
{
String s = "This is a test.";
System.out.println("Original: " + s);
String upper = s.toUpperCase();
String lower = s.toLowerCase();
System.out.println("Uppercase: " + upper);
System.out.println("Lowercase: " + lower);
}
}
The output produced by the program is shown here:
Original: This is a test.
Uppercase: THIS IS A TEST.
Lowercase: this is a test.
String Methods Added by Java 2, Version 1.4
Java 2, version 1.4 adds several methods to the String class. These are summarized in the following table.
Method Description
boolean contentEquals(StringBuffer str) Returns true if the invoking string contains the same string as str. Otherwise, returns
false.
CharSequence
subSequence(int startIndex,
int stopIndex)
Returns a substring of the invoking string,
beginning at startIndex and stopping at
stopIndex. This method is required by the
CharSequence interface, which is now
implemented by String.
boolean matches(string regExp) Returns true if the invoking string matches the regular expression passed in regExp.
Otherwise, returns false.
String
replaceFirst(String regExp,
String newStr)
Returns a string in which the first substring
that matches the regular expression
specified by regExp is replaced by newStr.
String
replaceAll(String regExp,
String newStr)
Returns a string in which all substrings that
match the regular expression specified by
regExp are replaced by newStr.
String[ ] split(String regExp) Decomposes the invoking string into parts and returns an array that contains the
result. Each part is delimited by the regular
expression passed in regExp.
String[ ] split(String regExp, int max) Decomposes the invoking string into parts and returns an array that contains the
result. Each part is delimited by the regular
expression passed in regExp. The number
of pieces is specified by max. If max is
negative, then the invoking string is fully
decomposed. Otherwise, if max contains
a non-zero value, the last entry in the
returned array contains the remainder
of the invoking string. If max is zero, the
invoking string is fully decomposed.
.
StringBuffer.
int indexOf(String str) Searches the invoking StringBuffer for the first occurrence of str. Returns the
index of the match, or –1 if no match is found.
int indexOf(String str, int startIndex) Searches the invoking StringBuffer for
the first occurrence of str, beginning at
startIndex. Returns the index of the
match, or –1 if no match is found.
int lastIndexOf(String str) Searches the invoking StringBuffer for
the last occurrence of str. Returns the
index of the match, or –1 if no match is
found.
int lastIndexOf(String str, int startIndex) Searches the invoking StringBuffer for the last occurrence of str, beginning at
startIndex. Returns the index of the match, or –1 if no match is found. Aside from subSequence( ), which implements a method required by the CharSequence interface, the other methods allow a StringBuffer to be searched for an occurrence of a String. The following program demonstrates indexOf( ) and
lastIndexOf( ).
class IndexOfDemo {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("one two one");
int i;
i = sb.indexOf("one");
System.out.println("First index: " + i);
i = sb.lastIndexOf("one");
System.out.println("Last index: " + i);
}
}
The output is shown here.
First index: 0
Last index: 8
Type Conversion and Casting
If you have previous programming experience, then you already know that it is fairly common to assign a value of one type to a variable of another type. If the two types are compatible, then Java will perform the conversion automatically. For example, it is always possible to assign an int value to a long variable. However, not all types are compatible, and thus, not all type conversions are implicitly allowed. For instance, there is no conversion defined from double to byte. Fortunately, it is still possible to obtain a conversion between incompatible types. To do so, you must use a cast, which performs an explicit conversion between incompatible types. Let’s look at both automatic
type conversions and casting.
Java’s Automatic Conversions
When one type of data is assigned to another type of variable, an automatic type conversion will take place if the following two conditions are met:
¦ The two types are compatible.
¦ The destination type is larger than the source type.
When these two conditions are met, a widening conversion takes place. For example, the int type is always large enough to hold all valid byte values, so no explicit cast statement is required. For widening conversions, the numeric types, including integer and floating-point types, are compatible with each other. However, the numeric types are not compatible with char or boolean. Also, char and boolean are not compatible with each other. As mentioned earlier, Java also performs an automatic type conversion when storing a literal integer constant into variables of type byte, short, or long.
Casting Incompatible Types
Although the automatic type conversions are helpful, they will not fulfill all needs. For example, what if you want to assign an int value to a byte variable? This conversion will not be performed automatically, because a byte is smaller than an int. This kind of conversion is sometimes called a narrowing conversion, since you are explicitly making the value narrower so that it will fit into the target type.
To create a conversion between two incompatible types, you must use a cast. A cast is simply an explicit type conversion. It has this general form:
(target-type) value
Here, target-type specifies the desired type to convert the specified value to. For example, the following fragment casts an int to a byte. If the integer’s value is larger than the range of a byte, it will be reduced modulo (the remainder of an integer division by the) byte’s range.
int a;
byte b;
// ...
b = (byte) a;
A different type of conversion will occur when a floating-point value is assigned to an integer type: truncation. As you know, integers do not have fractional components. Thus, when a floating-point value is assigned to an integer type, the fractional component is lost. For example, if the value 1.23 is assigned to an integer, the resulting value will simply be 1. The 0.23 will have been truncated. Of course, if the size of the whole number component is too large to fit into the target integer type, then that value will be reduced modulo the target type’s range. The following program demonstrates some type conversions that require casts:
// Demonstrate casts.
class Conversion {
public static void main(String args[]) {
byte b;
int i = 257;
double d = 323.142;
System.out.println("\nConversion of int to byte.");
b = (byte) i;
System.out.println("i and b " + i + " " + b);
System.out.println("\nConversion of double to int.");
i = (int) d;
System.out.println("d and i " + d + " " + i);
System.out.println("\nConversion of double to byte.");
b = (byte) d;
System.out.println("d and b " + d + " " + b);
}
}
This program generates the following output:














Lecture Notes –14
Topc: 1. Arrays
2. Single Dimensional
3. Multidimensional
.
1. Arrays
An array is a group of like-typed variables that are referred to by a common name. Arrays of any type can be created and may have one or more dimensions. A specific element in an array is accessed by its index. Arrays offer a convenient means of grouping related information. If you are familiar with C/C++, be careful. Arrays in Java work differently than they do in those languages.

One-Dimensional Arrays

A one-dimensional array is, essentially, a list of like-typed variables. To create an array, you first must create an array variable of the desired type. The general form of a onedimensional array declaration is
type var-name[ ];
Here, type declares the base type of the array. The base type determines the data type of each element that comprises the array. Thus, the base type for the array determines what type of data the array will hold. For example, the following declares an array named month_days with the type “array of int”:
int month_days[];
Although this declaration establishes the fact that month_days is an array variable, no array actually exists. In fact, the value of month_days is set to null, which represents an array with no value. To link month_days with an actual, physical array of integers, you must allocate one using new and assign it to month_days. new is a special operator that allocates memory. You will look more closely at new in a later chapter, but you need to use it now to allocate memory for arrays. The general form of new as it applies to one-dimensional arrays appears as follows:
array-var = new type[size];
Here, type specifies the type of data being allocated, size specifies the number of elements in the array, and array-var is the array variable that is linked to the array. That is, to use new to allocate an array, you must specify the type and number of elements to allocate. The elements in the array allocated by new will automatically be initialized to zero. This example allocates a 12-element array of integers and links them to month_days.
month_days = new int[12];
After this statement executes, month_days will refer to an array of 12 integers. Further, all elements in the array will be initialized to zero. Let’s review: Obtaining an array is a two-step process. First, you must declare a
variable of the desired array type. Second, you must allocate the memory that will hold the array, using new, and assign it to the array variable. Thus, in Java all arrays are dynamically allocated. If the concept of dynamic allocation is unfamiliar to you, don’t worry. It will be described at length later in this book.
Once you have allocated an array, you can access a specific element in the array by specifying its index within square brackets. All array indexes start at zero. For example, this statement assigns the value 28 to the second element of month_days. month_days[1] = 28;
The next line displays the value stored at index 3.
System.out.println(month_days[3]);
Putting together all the pieces, here is a program that creates an array of the
number of days in each month.
// Demonstrate a one-dimensional array.
class Array {
public static void main(String args[]) {
int month_days[];
month_days = new int[12];
month_days[0] = 31;
month_days[1] = 28;
month_days[2] = 31;
month_days[3] = 30;
month_days[4] = 31;
month_days[5] = 30;
month_days[6] = 31;
month_days[7] = 31;
month_days[8] = 30;
month_days[9] = 31;
month_days[10] = 30;
month_days[11] = 31;
System.out.println("April has " + month_days[3] + " days.");
}
}
When you run this program, it prints the number of days in April. As mentioned, Java array indexes start with zero, so the number of days in April is month_days[3] or 30. It is possible to combine the declaration of the array variable with the allocation of the array itself, as shown here:
int month_days[] = new int[12];
This is the way that you will normally see it done in professionally written Java programs. Arrays can be initialized when they are declared. The process is much the same as that used to initialize the simple types. An array initializer is a list of comma-separated expressions surrounded by curly braces. The commas separate the values of the array elements. The array will automatically be created large enough to hold the number of elements you specify in the array initializer. There is no need to use new. For example, to store the number of days in each month, the following code creates an initialized
array of integers:
// An improved version of the previous program.
class AutoArray {
public static void main(String args[]) {
64 J a v a ™ 2 . 0 : T h e C o m p l e t e R e f e r e n c e
int month_days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31,
30, 31 };
System.out.println("April has " + month_days[3] + " days.");
}
}
When you run this program, you see the same output as that generated by the
previous version.
Java strictly checks to make sure you do not accidentally try to store or reference values outside of the range of the array. The Java run-time system will check to be sure that all array indexes are in the correct range. (In this regard, Java is fundamentally different from C/C++, which provide no run-time boundary checks.) For example, the run-time system will check the value of each index into month_days to make sure that it is between 0 and 11 inclusive. If you try to access elements outside the range of the array (negative numbers or numbers greater than the length of the array), you will cause a run-time error. Here is one more example that uses a one-dimensional array. It finds the average of
a set of numbers.
// Average an array of values.
class Average {
public static void main(String args[]) {
double nums[] = {10.1, 11.2, 12.3, 13.4, 14.5};
double result = 0;
int i;
for(i=0; i<5; i++)
result = result + nums[i];
System.out.println("Average is " + result / 5);
}
}

Multidimensional Arrays

In Java, multidimensional arrays are actually arrays of arrays. These, as you might expect, look and act like regular multidimensional arrays. However, as you will see, there are a couple of subtle differences. To declare a multidimensional array variable, specify each additional index using another set of square brackets. For example, the following declares a two-dimensional array variable called twoD.
int twoD[][] = new int[4][5];
This allocates a 4 by 5 array and assigns it to twoD. Internally this matrix is implemented as an array of arrays of int. Conceptually, this array will look like the one shown in. A conceptual view of a 4 by 5, two-dimensional array The following program numbers each element in the array from left to right, top to bottom, and then displays these values:
// Demonstrate a two-dimensional array.
class TwoDArray {
public static void main(String args[]) {
int twoD[][]= new int[4][5];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<5; j++) {
twoD[i][j] = k;
k++;
}
for(i=0; i<4; i++) {
for(j=0; j<5; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}
This program generates the following output:
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
When you allocate memory for a multidimensional array, you need only specify the memory for the first (leftmost) dimension. You can allocate the remaining dimensions separately. For example, this following code allocates memory for the first dimension of twoD when it is declared. It allocates the second dimension manually.
int twoD[][] = new int[4][];
twoD[0] = new int[5];
twoD[1] = new int[5];
twoD[2] = new int[5];
twoD[3] = new int[5];
While there is no advantage to individually allocating the second dimension arrays in this situation, there may be in others. For example, when you allocate dimensions manually, you do not need to allocate the same number of elements for each dimension. As stated earlier, since multidimensional arrays are actually arrays of arrays, the length of each array is under your control. For example, the following program creates a twodimensional array in which the sizes of the second dimension are unequal.
// Manually allocate differing size second dimensions.
class TwoDAgain {
public static void main(String args[]) {
int twoD[][] = new int[4][];
twoD[0] = new int[1];
twoD[1] = new int[2];
twoD[2] = new int[3];
twoD[3] = new int[4];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; jtwoD[i][j] = k;
k++;
}
for(i=0; i<4; i++) {
for(j=0; jSystem.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}
This program generates the following output:
0
1 2
3 4 5
6 7 8 9
The array created by this program looks like this:
The use of uneven (or, irregular) multidimensional arrays is not recommended for most applications, because it runs contrary to what people expect to find when a multidimensional array is encountered. However, it can be used effectively in some situations. For example, if you need a very large two-dimensional array that is sparsely
populated (that is, one in which not all of the elements will be used), then an irregular array might be a perfect solution. It is possible to initialize multidimensional arrays. To do so, simply enclose each dimension’s initializer within its own set of curly braces. The following program creates a matrix where each element contains the product of the row and column indexes. Also notice that you can use expressions as well as literal values inside of array initializers.
// Initialize a two-dimensional array.
class Matrix {
public static void main(String args[]) {
double m[][] = {
{ 0*0, 1*0, 2*0, 3*0 },
{ 0*1, 1*1, 2*1, 3*1 },
{ 0*2, 1*2, 2*2, 3*2 },
{ 0*3, 1*3, 2*3, 3*3 }
};
int i, j;
for(i=0; i<4; i++) {
for(j=0; j<4; j++)
System.out.print(m[i][j] + " ");
System.out.println();
}
}
}
When you run this program, you will get the following output:
0.0 0.0 0.0 0.0
0.0 1.0 2.0 3.0
0.0 2.0 4.0 6.0
0.0 3.0 6.0 9.0
As you can see, each row in the array is initialized as specified in the initialization lists. Let’s look at one more example that uses a multidimensional array. The following program creates a 3 by 4 by 5, three-dimensional array. It then loads each element with the product of its indexes. Finally, it displays these products.
// Demonstrate a three-dimensional array.
class threeDMatrix {
public static void main(String args[]) {
int threeD[][][] = new int[3][4][5];
int i, j, k;
for(i=0; i<3; i++)
for(j=0; j<4; j++)
for(k=0; k<5; k++)
threeD[i][j][k] = i * j * k;
for(i=0; i<3; i++) {
for(j=0; j<4; j++) {
for(k=0; k<5; k++)
System.out.print(threeD[i][j][k] + " ");
System.out.println();
}
System.out.println();
}
}
}
This program generates the following output:
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 1 2 3 4
0 2 4 6 8
0 3 6 9 12
0 0 0 0 0
0 2 4 6 8
0 4 8 12 16
0 6 12 18 24
Alternative Array Declaration Syntax
There is a second form that may be used to declare an array:
type[ ] var-name;
Here, the square brackets follow the type specifier, and not the name of the array variable. For example, the following two declarations are equivalent: int al[] = new int[3];
int[] a2 = new int[3];
The following declarations are also equivalent:
char twod1[][] = new char[3][4];
char[][] twod2 = new char[3][4];
This alternative declaration form is included as a convenience, and is also useful when
specifying an array as a return type for a method.




















Lecture Notes –15
Topc: Vectors.
Vector
Vector implements a dynamic array. It is similar to ArrayList, but with two differences: Vector is synchronized, and it contains many legacy methods that are not part of the collections framework. With the release of Java 2, Vector was reengineered to extend AbstractList and implement the List interface, so it now is fully compatible with collections. Here are the Vector constructors:
Vector( )
Vector(int size)
Vector(int size, int incr)
Vector(Collection c)
The first form creates a default vector, which has an initial size of 10. The second form creates a vector whose initial capacity is specified by size. The third form creates a vector whose initial capacity is specified by size and whose increment is specified by incr. The increment specifies the number of elements to allocate each time that a vector is resized upward. The fourth form creates a vector that contains the elements of collection c. This constructor was added by Java 2.
All vectors start with an initial capacity. After this initial capacity is reached, the next time that you attempt to store an object in the vector, the vector automatically allocates space for that object plus extra room for additional objects. By allocating more than just the required memory, the vector reduces the number of allocations that must take place. This reduction is important, because allocations are costly in terms of time. The amount of extra space allocated during each reallocation is determined by the
increment that you specify when you create the vector. If you don’t specify an increment, the vector’s size is doubled by each allocation cycle. Vector defines these protected data members:
int capacityIncrement;
int elementCount;
Object elementData[ ];
The increment value is stored in capacityIncrement. The number of elements currently in the vector is stored in elementCount. The array that holds the vector is stored in elementData.
In addition to the collections methods defined by List, Vector defines several legacy methods, which are shown in Table 15-10.
Method Description
void addElement(Object element) The object specified by element is
added to the vector.
int capacity( ) Returns the capacity of the vector. Object clone( ) Returns a duplicate of the
invoking vector. boolean contains(Object element) Returns true if element is contained by the vector, and returns false if it is not. void copyInto(Object array[ ]) The elements contained in the
invoking vector are copied into the array specified by array. Object elementAt(int index) Returns the element at the location specified by index.
Enumeration elements( ) Returns an enumeration of the
elements in the vector.
void ensureCapacity(int size) Sets the minimum capacity of the vector to size. Object firstElement( ) Returns the first element in
the vector.
int indexOf(Object element) Returns the index of the first occurrence of element. If the object
is not in the vector, –1 is returned. int indexOf(Object element, int start) Returns the index of the first
occurrence of element at or after start. If the object is not in that
portion of the vector, –1 is
returned.
Table 15-10. The Methods Defined by Vector
THE
JAVA
LIBRARY
Method Description
void insertElementAt(Object element,
int index)
Adds element to the vector at the
location specified by index.
boolean isEmpty( ) Returns true if the vector is empty
and returns false if it contains one
or more elements.
Object lastElement( ) Returns the last element in the
vector.
int lastIndexOf(Object element) Returns the index of the last
occurrence of element. If the object
is not in the vector, –1 is returned.
int lastIndexOf(Object element,
int start)
Returns the index of the last
occurrence of element before start.
If the object is not in that portion
of the vector, –1 is returned.
void removeAllElements( ) Empties the vector. After this
method executes, the size of the
vector is zero.
boolean removeElement(Object element) Removes element from the vector. If more than one instance of the specified object exists in thevector, then it is the first one that is removed. Returns true if
successful and false if the object is
not found.
void removeElementAt(int index) Removes the element at the
location specified by index.
void setElementAt(Object element,
int index)
The location specified by index is
assigned element.
void setSize(int size) Sets the number of elements in the
vector to size. If the new size is
less than the old size, elements are
lost. If the new size is larger than
the old size, null elements are
added.
Table 15-10. The Methods Defined by Vector (continued)
Because Vector implements List, you can use a vector just like you use an ArrayList instance. You can also manipulate one using its legacy methods. For example, after you instantiate a Vector, you can add an element to it by calling addElement( ). To obtain the element at a specific location, call elementAt( ). To obtain the first element in the vector, call firstElement( ). To retrieve the last element, call lastElement( ). You can obtain the index of an element by using indexOf( ) and lastIndexOf( ). To remove an element, call removeElement( ) or removeElementAt( ). The following program uses a vector to store various types of numeric objects. It demonstrates several of the legacy methods defined by Vector. It also demonstrates the
Enumeration interface.
// Demonstrate various Vector operations.
import java.util.*;
class VectorDemo {
public static void main(String args[]) {
// initial size is 3, increment is 2
Vector v = new Vector(3, 2);
System.out.println("Initial size: " + v.size());
System.out.println("Initial capacity: " +
v.capacity());
Method Description
int size( ) Returns the number of elements
currently in the vector.
String toString( ) Returns the string equivalent of
the vector.
void trimToSize( ) Sets the vector’s capacity equal to
the number of elements that it
currently holds.
Table 15-10. The Methods Defined by Vector (continued)
v.addElement(new Integer(1));
v.addElement(new Integer(2));
v.addElement(new Integer(3));
v.addElement(new Integer(4));
System.out.println("Capacity after four additions: " +
v.capacity());
v.addElement(new Double(5.45));
System.out.println("Current capacity: " +
v.capacity());
v.addElement(new Double(6.08));
v.addElement(new Integer(7));
System.out.println("Current capacity: " +
v.capacity());
v.addElement(new Float(9.4));
v.addElement(new Integer(10));
System.out.println("Current capacity: " +
v.capacity());
v.addElement(new Integer(11));
v.addElement(new Integer(12));
System.out.println("First element: " +
(Integer)v.firstElement());
System.out.println("Last element: " +
(Integer)v.lastElement());
if(v.contains(new Integer(3)))
System.out.println("Vector contains 3.");
// enumerate the elements in the vector.
Enumeration vEnum = v.elements();
System.out.println("\nElements in vector:");
while(vEnum.hasMoreElements())
System.out.print(vEnum.nextElement() + " ");
System.out.println();
}
}
The output from this program is shown here:
Initial size: 0
Initial capacity: 3
Capacity after four additions: 5
Current capacity: 5
Current capacity: 7
Current capacity: 9
First element: 1
Last element: 12
Vector contains 3.
Elements in vector:
1 2 3 4 5.45 6.08 7 9.4 10 11 12
With the release of Java 2, Vector added support for iterators. Instead of relying on an enumeration to cycle through the objects (as the preceding program does), you now can use an iterator. For example, the following iterator-based code can be substituted into the program:
// use an iterator to display contents
Iterator vItr = v.iterator();
System.out.println("\nElements in vector:");
while(vItr.hasNext())
System.out.print(vItr.next() + " ");
System.out.println();
Because enumerations are not recommended for new code, you will usually use an iterator to enumerate the contents of a vector. Of course, much legacy code exists that employs enumerations. Fortunately, enumerations and iterators work in nearly the same manner.












Lecture Notes –16

Topc: 1. Inheritance

What Is Inheritance?
Different kinds of objects often have a certain amount in common with each other. Mountain bikes, road bikes, and tandem bikes, for example, all share the characteristics of bicycles (current speed, current pedal cadence, current gear). Yet each also defines additional features that make them different: tandem bicycles have two seats and two sets of handlebars; road bikes have drop handlebars; some mountain bikes have an additional chain ring, giving them a lower gear ratio.
Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. In this example, Bicycle now becomes the superclass of MountainBike, RoadBike, and TandemBike. In the Java programming language, each class is allowed to have one direct superclass, and each superclass has the potential for an unlimited number of subclasses:
A hierarchy of bicycle classes.
The syntax for creating a subclass is simple. At the beginning of your class declaration, use the extends keyword, followed by the name of the class to inherit from: class MountainBike extends Bicycle { // new fields and methods defining a mountain bike would go here }
This gives MountainBike all the same fields and methods as Bicycle, yet allows its code to focus exclusively on the features that make it unique. This makes code for your subclasses easy to read. However, you must take care to properly document the state.

I general class that defines traits common to a set of related items. This class can then
be inherited by other, more specific classes, each adding those things that are unique to
it. In the terminology of Java, a class that is inherited is called a superclass. The class that
does the inheriting is called a subclass. Therefore, a subclass is a specialized version of a
superclass. It inherits all of the instance variables and methods defined by the
superclass and adds its own, unique elements.
Inheritance Basics
To inherit a class, you simply incorporate the definition of one class into another by using the extends keyword. To see how, let’s begin with a short example. The following program creates a superclass called A and a subclass called B. Notice how the keyword extends is used to create a subclass of A.
// A simple example of inheritance.
// Create a superclass.
class A {
int i, j;
void showij() {
System.out.println("i and j: " + i + " " + j);
}
}
// Create a subclass by extending class A.
class B extends A {
int k;
void showk() {
System.out.println("k: " + k);
}
void sum() {
System.out.println("i+j+k: " + (i+j+k));
}
}
class SimpleInheritance {
public static void main(String args[]) {
A superOb = new A();
B subOb = new B();
// The superclass may be used by itself.
superOb.i = 10;
superOb.j = 20;
System.out.println("Contents of superOb: ");
superOb.showij();
System.out.println();
/* The subclass has access to all public members of
its superclass. */
subOb.i = 7;
subOb.j = 8;
subOb.k = 9;
System.out.println("Contents of subOb: ");
subOb.showij();
subOb.showk();
System.out.println();
System.out.println("Sum of i, j and k in subOb:");
subOb.sum();
}
}
The output from this program is shown here:
Contents of superOb:
i and j: 10 20
Contents of subOb:
i and j: 7 8
k: 9
Sum of i, j and k in subOb:
i+j+k: 24
As you can see, the subclass B includes all of the members of its superclass, A. This is why subOb can access i and j and call showij( ). Also, inside sum( ), i and j can be referred to directly, as if they were part of B.
Even though A is a superclass for B, it is also a completely independent, stand-alone class. Being a superclass for a subclass does not mean that the superclass cannot be used by itself. Further, a subclass can be a superclass for another subclass. The general form of a class declaration that inherits a superclass is shown here: class subclass-name extends
superclass-name {
// body of class
}
You can only specify one superclass for any subclass that you create. Java does not support the inheritance of multiple superclasses into a single subclass. (This differs from C++, in which you can inherit multiple base classes.) You can, as stated, create a hierarchy of inheritance in which a subclass becomes a superclass of another subclass. However, no class can be a superclass of itself.
Member Access and Inheritance
Although a subclass includes all of the members of its superclass, it cannot access those members of the superclass that have been declared as private. For example, consider the following simple class hierarchy:
/* In a class hierarchy, private members remain
private to their class.
This program contains an error and will not
compile.
*/
// Create a superclass.
class A {
int i; // public by default
private int j; // private to A
void setij(int x, int y) {
i = x;
j = y;
}
}
// A's j is not accessible here.
class B extends A {
int total;
void sum() {
total = i + j; // ERROR, j is not accessible here
}
}
class Access {
public static void main(String args[]) {
B subOb = new B();
subOb.setij(10, 12);
subOb.sum();
System.out.println("Total is " + subOb.total);
}
}
This program will not compile because the reference to j inside the sum( ) method of B causes an access violation. Since j is declared as private, it is only accessible by other members of its own class. Subclasses have no access to it. A class member that has been declared as private will remain private to its class. It is not accessible by any code outside its class, including subclasses.
A More Practical Example Let’s look at a more practical example that will help illustrate the power of inheritance. Here, the final version of the Box class developed in the preceding chapter will be
extended to include a fourth component called weight. Thus, the new class will contain a box’s width, height, depth, and weight. // This program uses inheritance to extend Box.
class Box {
double width;
double height;
double depth;
// construct clone of an object
Box(Box ob) { // pass object to constructor
width = ob.width;
height = ob.height;
depth = ob.depth;
}
// constructor used when all dimensions specified
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions specified
Box() {
width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box
}
// constructor used when cube is created
Box(double len) {
width = height = depth = len;
}
// compute and return volume
double volume() {
return width * height * depth;
}
}
// Here, Box is extended to include weight.
class BoxWeight extends Box {
double weight; // weight of box
// constructor for BoxWeight
BoxWeight(double w, double h, double d, double m) {
width = w;
height = h;
depth = d;
weight = m;
}
}
class DemoBoxWeight {
public static void main(String args[]) {
BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3);
BoxWeight mybox2 = new BoxWeight(2, 3, 4, 0.076);
double vol;
vol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
System.out.println("Weight of mybox1 is " + mybox1.weight);
System.out.println();
vol = mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);
System.out.println("Weight of mybox2 is " + mybox2.weight);
}
}
The output from this program is shown here:
Volume of mybox1 is 3000.0
Weight of mybox1 is 34.3
Volume of mybox2 is 24.0
Weight of mybox2 is 0.076
BoxWeight inherits all of the characteristics of Box and adds to them the weight
component. It is not necessary for BoxWeight to re-create all of the features found in
Box. It can simply extend Box to meet its own purposes.
A major advantage of inheritance is that once you have created a superclass that
defines the attributes common to a set of objects, it can be used to create any number
of more specific subclasses. Each subclass can precisely tailor its own classification. For
example, the following class inherits Box and adds a color attribute:
// Here, Box is extended to include color.
class ColorBox extends Box {
int color; // color of box
ColorBox(double w, double h, double d, int c) {
width = w;
height = h;
depth = d;
color = c;
}
}
Remember, once you have created a superclass that defines the general aspects of an object, that superclass can be inherited to form specialized classes. Each subclass simply adds its own, unique attributes. This is the essence of inheritance. A Superclass Variable Can Reference a Subclass Object
A reference variable of a superclass can be assigned a reference to any subclass derived from that superclass. You will find this aspect of inheritance quite useful in a variety of situations. For example, consider the following:
class RefDemo {
public static void main(String args[]) {
BoxWeight weightbox = new BoxWeight(3, 5, 7, 8.37);
Box plainbox = new Box();
double vol;
vol = weightbox.volume();
System.out.println("Volume of weightbox is " + vol);
System.out.println("Weight of weightbox is " +
weightbox.weight);
System.out.println();
// assign BoxWeight reference to Box reference
plainbox = weightbox;
vol = plainbox.volume(); // OK, volume() defined in Box
System.out.println("Volume of plainbox is " + vol);
/* The following statement is invalid because plainbox
does not define a weight member. */
// System.out.println("Weight of plainbox is " + plainbox.weight);
}
}
Here, weightbox is a reference to BoxWeight objects, and plainbox is a reference to Box objects. Since BoxWeight is a subclass of Box, it is permissible to assign plainbox a reference to the weightbox object.
It is important to understand that it is the type of the reference variable—not the type of the object that it refers to—that determines what members can be accessed. That is, when a reference to a subclass object is assigned to a superclass reference variable, you will have access only to those parts of the object defined by the superclass. This is why plainbox can’t access weight even when it refers to a BoxWeight object. If you
think about it, this makes sense, because the superclass has no knowledge of what a subclass adds to it. This is why the last line of code in the preceding fragment is commented out. It is not possible for a Box reference to access the weight field, because it does not define one. Although the preceding may seem a bit esoteric, it has some important practical applications—two of which are discussed later in this chapter. Using super In the preceding examples, classes derived from Box were not implemented as efficiently or as robustly as they could have been. For example, the constructor for
BoxWeight explicitly initializes the width, height, and depth fields of Box( ). Not only does this duplicate code found in its superclass, which is inefficient, but it implies that a subclass must be granted access to these members. However, there will be times when you will want to create a superclass that keeps the details of its implementation to itself (that is, that keeps its data members private). In this case, there would be no way for a subclass to directly access or initialize these variables on its own. Since encapsulation is a primary attribute of OOP, it is not surprising that Java provides a solution to this problem. Whenever a subclass needs to refer to its immediate
superclass, it can do so by use of the keyword super. super has two general forms. The first calls the superclass’ constructor. The second
is used to access a member of the superclass that has been hidden by a member of a
subclass. Each use is examined here.
Using super to Call Superclass Constructors
A subclass can call a constructor method defined by its superclass by use of the
following form of super:
super(parameter-list);
Here, parameter-list specifies any parameters needed by the constructor in
the superclass. super( ) must always be the first statement executed inside a
subclass’ constructor.
To see how super( ) is used, consider this improved version of the
BoxWeight( ) class:
// BoxWeight now uses super to initialize its Box attributes.
class BoxWeight extends Box {
double weight; // weight of box
// initialize width, height, and depth using super()
BoxWeight(double w, double h, double d, double m) {
super(w, h, d); // call superclass constructor
weight = m;
}
}
Here, BoxWeight( ) calls super( ) with the parameters w, h, and d. This causes the
Box( ) constructor to be called, which initializes width, height, and depth using these
values. BoxWeight no longer initializes these values itself. It only needs to initialize the
value unique to it: weight. This leaves Box free to make these values private if desired.
In the preceding example, super( ) was called with three arguments. Since
constructors can be overloaded, super( ) can be called using any form defined by the
superclass. The constructor executed will be the one that matches the arguments. For
example, here is a complete implementation of BoxWeight that provides constructors
for the various ways that a box can be constructed. In each case, super( ) is called using
the appropriate arguments. Notice that width, height, and depth have been made
private within Box.
// A complete implementation of BoxWeight.
class Box {
private double width;
private double height;
private double depth;
// construct clone of an object
Box(Box ob) { // pass object to constructor
width = ob.width;
height = ob.height;
depth = ob.depth;
}
// constructor used when all dimensions specified
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions specified
Box() {
width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box
}
// constructor used when cube is created
Box(double len) {
width = height = depth = len;
}
// compute and return volume
double volume() {
return width * height * depth;
}
}
// BoxWeight now fully implements all constructors.
class BoxWeight extends Box {
double weight; // weight of box
// construct clone of an object
BoxWeight(BoxWeight ob) { // pass object to constructor
super(ob);
weight = ob.weight;
}
// constructor when all parameters are specified
BoxWeight(double w, double h, double d, double m) {
super(w, h, d); // call superclass constructor
weight = m;
}
// default constructor
BoxWeight() {
super();
weight = -1;
}
// constructor used when cube is created
BoxWeight(double len, double m) {
super(len);
weight = m;
}
}
class DemoSuper {
public static void main(String args[]) {
BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3);
BoxWeight mybox2 = new BoxWeight(2, 3, 4, 0.076);
BoxWeight mybox3 = new BoxWeight(); // default
BoxWeight mycube = new BoxWeight(3, 2);
BoxWeight myclone = new BoxWeight(mybox1);
double vol;
vol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
System.out.println("Weight of mybox1 is " + mybox1.weight);
System.out.println();
vol = mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);
System.out.println("Weight of mybox2 is " + mybox2.weight);
System.out.println();
vol = mybox3.volume();
System.out.println("Volume of mybox3 is " + vol);
System.out.println("Weight of mybox3 is " + mybox3.weight);
System.out.println();
vol = myclone.volume();
System.out.println("Volume of myclone is " + vol);
System.out.println("Weight of myclone is " + myclone.weight);
System.out.println();
vol = mycube.volume();
System.out.println("Volume of mycube is " + vol);
System.out.println("Weight of mycube is " + mycube.weight);
System.out.println();
}
}
This program generates the following output:
Volume of mybox1 is 3000.0
Weight of mybox1 is 34.3
Volume of mybox2 is 24.0
Weight of mybox2 is 0.076
Volume of mybox3 is -1.0
Weight of mybox3 is -1.0
Volume of myclone is 3000.0
Weight of myclone is 34.3
Volume of mycube is 27.0
Weight of mycube is 2.0
Pay special attention to this constructor in BoxWeight( ):
// construct clone of an object
BoxWeight(BoxWeight ob) { // pass object to constructor
super(ob);
weight = ob.weight;
}
Notice that super( ) is called with an object of type BoxWeight—not of type Box. This still invokes the constructor Box(Box ob). As mentioned earlier, a superclass variable can be used to reference any object derived from that class. Thus, we are able
to pass a BoxWeight object to the Box constructor. Of course, Box only has knowledge
of its own members.
Let’s review the key concepts behind super( ). When a subclass calls super( ), it is
calling the constructor of its immediate superclass. Thus, super( ) always refers to the
superclass immediately above the calling class. This is true even in a multileveled
hierarchy. Also, super( ) must always be the first statement executed inside a subclass
constructor.
A Second Use for super
The second form of super acts somewhat like this, except that it always refers to
the superclass of the subclass in which it is used. This usage has the following
general form:
super.member
Here, member can be either a method or an instance variable.
This second form of super is most applicable to situations in which member names
of a subclass hide members by the same name in the superclass. Consider this simple
class hierarchy:
// Using super to overcome name hiding.
class A {
int i;
}
// Create a subclass by extending class A.
class B extends A {
int i; // this i hides the i in A
B(int a, int b) {
super.i = a; // i in A
i = b; // i in B
}
void show() {
System.out.println("i in superclass: " + super.i);
System.out.println("i in subclass: " + i);
}
}
class UseSuper {
public static void main(String args[]) {
B subOb = new B(1, 2);
subOb.show();
}
}
This program displays the following:
i in superclass: 1
i in subclass: 2
Although the instance variable i in B hides the i in A, super allows access to the I defined in the superclass. As you will see, super can also be used to call methods that are hidden by a subclass.
Creating a Multilevel Hierarchy
Up to this point, we have been using simple class hierarchies that consist of only a superclass and a subclass. However, you can build hierarchies that contain as many layers of inheritance as you like. As mentioned, it is perfectly acceptable to use a subclass as a superclass of another. For example, given three classes called A, B, and C, C can be a subclass of B, which is a subclass of A. When this type of situation occurs, each subclass inherits all of the traits found in all of its superclasses. In this case, C inherits all aspects of B and A. To see how a multilevel hierarchy can be useful,
consider the following program. In it, the subclass BoxWeight is used as a superclass to create the subclass called Shipment. Shipment inherits all of the traits of BoxWeight and Box, and adds a field called cost, which holds the cost of shipping such a parcel. // Extend BoxWeight to include shipping costs.
// Start with Box.
class Box {
private double width;
private double height;
private double depth;
// construct clone of an object
Box(Box ob) { // pass object to constructor
width = ob.width;
height = ob.height;
depth = ob.depth;
}
// constructor used when all dimensions specified
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions specified
Box() {
width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box
}
// constructor used when cube is created
Box(double len) {
width = height = depth = len;
}
// compute and return volume
double volume() {
return width * height * depth;
}
}
// Add weight.
class BoxWeight extends Box {
double weight; // weight of box
// construct clone of an object
BoxWeight(BoxWeight ob) { // pass object to constructor
super(ob);
weight = ob.weight;
}
// constructor when all parameters are specified
BoxWeight(double w, double h, double d, double m) {
super(w, h, d); // call superclass constructor
weight = m;
}
// default constructor
BoxWeight() {
super();
weight = -1;
}
// constructor used when cube is created
BoxWeight(double len, double m) {
super(len);
weight = m;
}
}
// Add shipping costs
class Shipment extends BoxWeight {
double cost;
// construct clone of an object
Shipment(Shipment ob) { // pass object to constructor
super(ob);
cost = ob.cost;
}
// constructor when all parameters are specified
Shipment(double w, double h, double d,
double m, double c) {
super(w, h, d, m); // call superclass constructor
cost = c;
}
// default constructor
Shipment() {
super();
cost = -1;
}
// constructor used when cube is created
Shipment(double len, double m, double c) {
super(len, m);
cost = c;
}
}
class DemoShipment {
public static void main(String args[]) {
Shipment shipment1 =
new Shipment(10, 20, 15, 10, 3.41);
Shipment shipment2 =
new Shipment(2, 3, 4, 0.76, 1.28);
double vol;
vol = shipment1.volume();
System.out.println("Volume of shipment1 is " + vol);
System.out.println("Weight of shipment1 is "
+ shipment1.weight);
System.out.println("Shipping cost: $" + shipment1.cost);
System.out.println();
vol = shipment2.volume();
System.out.println("Volume of shipment2 is " + vol);
System.out.println("Weight of shipment2 is "
+ shipment2.weight);
System.out.println("Shipping cost: $" + shipment2.cost);
}
}
The output of this program is shown here:
Volume of shipment1 is 3000.0
Weight of shipment1 is 10.0
Shipping cost: $3.41
Volume of shipment2 is 24.0
Weight of shipment2 is 0.76
Shipping cost: $1.28
Because of inheritance, Shipment can make use of the previously defined classes of Box and BoxWeight, adding only the extra information it needs for its own, specific application. This is part of the value of inheritance; it allows the reuse of code. This example illustrates one other important point: super( ) always refers to the constructor in the closest superclass. The super( ) in Shipment calls the constructor in BoxWeight. The super( ) in BoxWeight calls the constructor in Box. In a class hierarchy, if a superclass constructor requires parameters, then all subclasses must pass
those parameters “up the line.” This is true whether or not a subclass needs parameters
of its own.
In the preceding program, the entire class hierarchy, including Box, BoxWeight, and Shipment, is shown all in one file. This is for your convenience only. In Java, all three classes could have been placed into their own files and compiled separately. In fact, using separate files is the norm, not the exception, in creating class hierarchies. When Constructors Are Called When a class hierarchy is created, in what order are the constructors for the classes that make up the hierarchy called? For example, given a subclass called B and a superclass called A, is A’s constructor called before B’s, or vice versa? The answer is that in a class hierarchy, constructors are called in order of derivation, from superclass to subclass.
Further, since super( ) must be the first statement executed in a subclass’ constructor, this order is the same whether or not super( ) is used. If super( ) is not used, then the default or parameterless constructor of each superclass will be executed. The following program illustrates when constructors are executed:
// Demonstrate when constructors are called.
// Create a super class.
class A {
A() {
System.out.println("Inside A's constructor.");
}
}
// Create a subclass by extending class A.
class B extends A {
B() {
System.out.println("Inside B's constructor.");
}
}
// Create another subclass by extending B.
class C extends B {
C() {
System.out.println("Inside C's constructor.");
}
}
class CallingCons {
public static void main(String args[]) {
C c = new C();
}
}
The output from this program is shown here:
Inside A’s constructor
Inside B’s constructor
Inside C’s constructor
As you can see, the constructors are called in order of derivation. If you think about it, it makes sense that constructors are executed in order of derivation. Because a superclass has no knowledge of any subclass, any initialization it needs to perform is separate from and possibly prerequisite to any initialization performed by the subclass. Therefore, it must be executed first.

















Lecture Notes –17
Topc: 1.: Types of Inheritance
2. Dynamic dispatch,Method Overloading, Overriding
3. Super, this

1. Types of Inheritance
1. SINGLE INHERITANCE
2. MULTIPLE INHERITANCE
3. MULTIPATH INHERITANCE
4. MULTI LEVEL INHERITANCE
5. HERARCHICAL INHERITANCE
6. HYBRID INHERITANCE


Method Overriding
In a class hierarchy, when a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass. When an overridden method is called from within a subclass, it will always refer to the version of that method defined by the subclass. The version of the method defined by the superclass will be hidden. Consider
the following:
// Method overriding.
class A {
int i, j;
A(int a, int b) {
i = a;
j = b;
}
// display i and j
void show() {
System.out.println("i and j: " + i + " " + j);
}
}
class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
// display k – this overrides show() in A
void show() {
System.out.println("k: " + k);
}
}
class Override {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
subOb.show(); // this calls show() in B
}
}
The output produced by this program is shown here:
k: 3
When show( ) is invoked on an object of type B, the version of show( ) defined within B is used. That is, the version of show( ) inside B overrides the version declared in A. If you wish to access the superclass version of an overridden function, you can do so by using super. For example, in this version of B, the superclass version of show( ) is invoked within the subclass’ version. This allows all instance variables to be displayed.
class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
void show() {
super.show(); // this calls A's show()
System.out.println("k: " + k);
}
}
If you substitute this version of A into the previous program, you will see the following output:
i and j: 1 2
k: 3
Here, super.show( ) calls the superclass version of show( ). Method overriding occurs only when the names and the type signatures of the two
methods are identical. If they are not, then the two methods are simply overloaded. For example, consider this modified version of the preceding example: // Methods with differing type signatures are overloaded – not
// overridden.
class A {
int i, j;
A(int a, int b) {
i = a;
j = b;
}
// display i and j
void show() {
System.out.println("i and j: " + i + " " + j);
}
}
// Create a subclass by extending class A.
class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
// overload show()
void show(String msg) {
System.out.println(msg + k);
}
}
class Override {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
subOb.show("This is k: "); // this calls show() in B
subOb.show(); // this calls show() in A
}
}
The output produced by this program is shown here:
This is k: 3
i and j: 1 2
The version of show( ) in B takes a string parameter. This makes its type signature different from the one in A, which takes no parameters. Therefore, no overriding (or name hiding) takes place.
Dynamic Method Dispatch
While the examples in the preceding section demonstrate the mechanics of method overriding, they do not show its power. Indeed, if there were nothing more to method overriding than a name space convention, then it would be, at best, an interesting curiosity, but of little real value. However, this is not the case. Method overriding forms the basis for one of Java’s most powerful concepts: dynamic method dispatch. Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at run time, rather than compile time. Dynamic method dispatch is
important because this is how Java implements run-time polymorphism. Let’s begin by restating an important principle: a superclass reference variable can refer to a subclass object. Java uses this fact to resolve calls to overridden methods at run time. Here is how. When an overridden method is called through a superclass reference, Java determines which version of that method to execute based upon the type of the object being referred to at the time the call occurs. Thus, this determination
is made at run time. When different types of objects are referred to, different versions of an overridden method will be called. In other words, it is the type of the object being referred to (not the type of the reference variable) that determines which version of an overridden method will be executed. Therefore, if a superclass contains a method that is overridden by a subclass, then when different types of objects are referred to through a superclass reference variable, different versions of the method are executed. Here is an example that illustrates dynamic method dispatch:
// Dynamic Method Dispatch
class A {
void callme() {
System.out.println("Inside A's callme method");
}
}
class B extends A {
// override callme()
void callme() {
System.out.println("Inside B's callme method");
}
}
class C extends A {
// override callme()
void callme() {
System.out.println("Inside C's callme method");
}
}
class Dispatch {
public static void main(String args[]) {
A a = new A(); // object of type A
B b = new B(); // object of type B
C c = new C(); // object of type C
A r; // obtain a reference of type A
r = a; // r refers to an A object
r.callme(); // calls A's version of callme
r = b; // r refers to a B object
r.callme(); // calls B's version of callme

r = c; // r refers to a C object
r.callme(); // calls C's version of callme
}
}
The output from the program is shown here:
Inside A’s callme method
Inside B’s callme method
Inside C’s callme method
This program creates one superclass called A and two subclasses of it, called B and C. Subclasses B and C override callme( ) declared in A. Inside the main( ) method, objects of type A, B, and C are declared. Also, a reference of type A, called r, is declared. The program then assigns a reference to each type of object to r and uses that reference to invoke callme( ). As the output shows, the version of callme( ) executed is determined by the type of object being referred to at the time of the call. Had it been determined by the type of the reference variable, r, you would see three calls to A’s callme( ) method. Readers familiar with C++ or C# will recognize that overridden methods in Java are
similar to virtual functions in those languages.
Why Overridden Methods?
As stated earlier, overridden methods allow Java to support run-time polymorphism. Polymorphism is essential to object-oriented programming for one reason: it allows a general class to specify methods that will be common to all of its derivatives, while allowing subclasses to define the specific implementation of some or all of those methods. Overridden methods are another way that Java implements the “one interface, multiple methods” aspect of polymorphism.
Part of the key to successfully applying polymorphism is understanding that the superclasses and subclasses form a hierarchy which moves from lesser to greater specialization. Used correctly, the superclass provides all elements that a subclass can use directly. It also defines those methods that the derived class must implement on its own. This allows the subclass the flexibility to define its own methods, yet still enforces a consistent interface. Thus, by combining inheritance with overridden
methods, a superclass can define the general form of the methods that will be used by all of its subclasses. Dynamic, run-time polymorphism is one of the most powerful mechanisms that object-oriented design brings to bear on code reuse and robustness. The ability of existing code libraries to call methods on instances of new classes without recompiling while maintaining a clean abstract interface is a profoundly powerful tool.
Applying Method Overriding
Let’s look at a more practical example that uses method overriding. The following program creates a superclass called Figure that stores the dimensions of various two-dimensional objects. It also defines a method called area( ) that computes the area of an object. The program derives two subclasses from Figure. The first is Rectangle and the second is Triangle. Each of these subclasses overrides area( ) so that it returns the area of a rectangle and a triangle, respectiv ely.
// Using run-time polymorphism.
class Figure {
double dim1;
double dim2;
Figure(double a, double b) {
dim1 = a;
dim2 = b;
}
double area() {
System.out.println("Area for Figure is undefined.");
return 0;
}
}
class Rectangle extends Figure {
Rectangle(double a, double b) {
super(a, b);
}
// override area for rectangle
double area() {
System.out.println("Inside Area for Rectangle.");
return dim1 * dim2;
}
}
class Triangle extends Figure {
Triangle(double a, double b) {
super(a, b);
}
// override area for right triangle
double area() {
System.out.println("Inside Area for Triangle.");
return dim1 * dim2 / 2;
}
}
class FindAreas {
public static void main(String args[]) {
Figure f = new Figure(10, 10);
Rectangle r = new Rectangle(9, 5);
Triangle t = new Triangle(10, 8);
Figure figref;
figref = r;
System.out.println("Area is " + figref.area());
figref = t;
System.out.println("Area is " + figref.area());
figref = f;
System.out.println("Area is " + figref.area());
}
}
The output from the program is shown here:
Inside Area for Rectangle.
Area is 45
Inside Area for Triangle.
Area is 40
Area for Figure is undefined.
Area is 0
Through the dual mechanisms of inheritance and run-time polymorphism, it is possible to define one consistent interface that is used by several different, yet related, types of objects. In this case, if an object is derived from Figure, then its area can be obtained by calling area( ). The interface to this operation is the same no matter what type of figure is being used.









































Lecture Notes –18
Topc: 1. Abstract Classes


There are situations in which you will want to define a superclass that declares the structure of a given abstraction without providing a complete implementation of every method. That is, sometimes you will want to create a superclass that only defines a generalized form that will be shared by all of its subclasses, leaving it to each subclass to fill in the details. Such a class determines the nature of the methods that the subclasses must implement. One way this situation can occur is when a superclass is unable to create a meaningful implementation for a method. This is the case with the class Figure used in the preceding example. The definition of area( ) is simply a placeholder. It will not compute and display the area of any type of object. As you will see as you create your own class libraries, it is not uncommon for a method to have no meaningful definition in the context of its superclass. You can handle this situation two ways. One way, as shown in the previous example, is to simply have it report a warning message. While this approach can be useful in certain situations—such as debugging—it is not usually appropriate. You may have methods which must be overridden by the subclass in order for the subclass to have any meaning. Consider the class Triangle. It has no meaning if area( ) is not defined. In this case, you want some way to ensure that a subclass does, indeed, override all necessary methods.
Java’s solution to this problem is the abstract method.
You can require that certain methods be overridden by subclasses by specifying the abstract type modifier. These methods are sometimes referred to as subclasser responsibility because they have no implementation specified in the superclass. Thus, a subclass must override them—it cannot simply use the version defined in the superclass. To declare an abstract method, use this general form:
abstract type name(parameter-list);
As you can see, no method body is present. Any class that contains one or more abstract methods must also be declared abstract. To declare a class abstract, you simply use the abstract keyword in front of the class keyword at the beginning of the class declaration. There can be no objects of an abstract class. That is, an abstract class cannot be directly instantiated with the new operator. Such objects would be useless, because an abstract class is not fully defined. Also, you cannot declare abstract constructors, or abstract static methods. Any subclass of an abstract class must either implement all of the abstract methods in the superclass, or be itself declared abstract.
Here is a simple example of a class with an abstract method, followed by a class
which implements that method:
// A Simple demonstration of abstract.
abstract class A {
abstract void callme();
// concrete methods are still allowed in abstract classes
void callmetoo() {
System.out.println("This is a concrete method.");
}
}
class B extends A {
void callme() {
System.out.println("B's implementation of callme.");
}
}
class AbstractDemo {
public static void main(String args[]) {
B b = new B();
b.callme();
b.callmetoo();
}
}
Notice that no objects of class A are declared in the program. As mentioned, it is not possible to instantiate an abstract class. One other point: class A implements a concrete method called callmetoo( ). This is perfectly acceptable. Abstract classes can include as much implementation as they see fit.
Although abstract classes cannot be used to instantiate objects, they can be used to create object references, because Java’s approach to run-time polymorphism is implemented through the use of superclass references. Thus, it must be possible to create a reference to an abstract class so that it can be used to point to a subclass object. You will see this feature put to use in the next example. Using an abstract class, you can improve the Figure class shown earlier. Since there is no meaningful concept of area for an undefined two-dimensional figure, the following version of the program declares area( ) as abstract inside Figure. This, of course, means that all classes derived from Figure must override area( ).
// Using abstract methods and classes.
abstract class Figure {
double dim1;
double dim2;
Figure(double a, double b) {
dim1 = a;
dim2 = b;
}
// area is now an abstract method
abstract double area();
}
class Rectangle extends Figure {
Rectangle(double a, double b) {
super(a, b);
}
// override area for rectangle
double area() {
System.out.println("Inside Area for Rectangle.");
return dim1 * dim2;
}
}
class Triangle extends Figure {
Triangle(double a, double b) {
super(a, b);
}
// override area for right triangle
double area() {
System.out.println("Inside Area for Triangle.");
return dim1 * dim2 / 2;
}
}
class AbstractAreas {
public static void main(String args[]) {
// Figure f = new Figure(10, 10); // illegal now
Rectangle r = new Rectangle(9, 5);
Triangle t = new Triangle(10, 8);
Figure figref; // this is OK, no object is created
figref = r;
System.out.println("Area is " + figref.area());
figref = t;
System.out.println("Area is " + figref.area());
}
}
As the comment inside main( ) indicates, it is no longer possible to declare objects of type Figure, since it is now abstract. And, all subclasses of Figure must override area( ). To prove this to yourself, try creating a subclass that does not override area( ). You will receive a compile-time error.
Although it is not possible to create an object of type Figure, you can create a reference variable of type Figure. The variable figref is declared as a reference to Figure, which means that it can be used to refer to an object of any class derived from Figure. As explained, it is through super class reference variables that overridden
methods are resolved at run time. Using final with Inheritance The keyword final has three uses. First, it can be used to create the equivalent of a named constant. This use was described in the preceding chapter. The other two uses
of final apply to inheritance. Both are examined here. Using final to Prevent Overriding While method overriding is one of Java’s most powerful features, there will be times when you will want to prevent it from occurring. To disallow a method from being overridden, specify final as a modifier at the start of its declaration. Methods declared as final cannot be overridden. The following fragment illustrates final:
class A {
final void meth() {
System.out.println("This is a final method.");
}
}
class B extends A {
void meth() { // ERROR! Can't override.
System.out.println("Illegal!");
}
}
Because meth( ) is declared as final, it cannot be overridden in B. If you attempt to do so, a compile-time error will result. Methods declared as final can sometimes provide a performance enhancement: The compiler is free to inline calls to them because it “knows” they will not be overridden by a subclass. When a small final method is called, often the Java compiler can copy the bytecode for the subroutine directly inline with the compiled code of the calling method, thus eliminating the costly overhead associated with a method call. Inlining is only an option with final methods. Normally, Java resolves calls to methods dynamically, at run time. This is called late binding. However, since final methods cannot be overridden, a call to one can be resolved at compile time. This is called early binding. Using final to Prevent Inheritance Sometimes you will want to prevent a class from being inherited. To do this, precede the class declaration with final. Declaring a class as final implicitly declares all of its methods as final, too. As you might expect, it is illegal to declare a class as both abstract and final since an abstract class is incomplete by itself and relies upon its subclasses to provide complete implementations.
Here is an example of a final class:
final class A {
// ...
}
// The following class is illegal.
class B extends A { // ERROR! Can't subclass A
// ...
}
As the comments imply, it is illegal for B to inherit A since A is declared as final.
The Object Class
There is one special class, Object, defined by Java. All other classes are subclasses of Object. That is, Object is a superclass of all other classes. This means that a reference variable of type Object can refer to an object of any other class. Also, since arrays are implemented as classes, a variable of type Object can also refer to any array. Object defines the following methods, which means that they are available in
every object.
Method Purpose
Object clone( ) Creates a new object that is the same as
the object being cloned.
boolean equals(Object object) Determines whether one object is equal to
another.
void finalize( ) Called before an unused object is
recycled.
Class getClass( ) Obtains the class of an object at run time.
int hashCode( ) Returns the hash code associated with the
invoking object.
void notify( ) Resumes execution of a thread waiting on
the invoking object.
void notifyAll( ) Resumes execution of all threads waiting
on the invoking object.
String toString( ) Returns a string that describes the object.
void wait( )
void wait(long milliseconds)
void wait(long milliseconds,
int nanoseconds)
Waits on another thread of execution.
The methods getClass( ), notify( ), notifyAll( ), and wait( ) are declared as final. You may override the others. These methods are described elsewhere in this book. However, notice two methods now: equals( ) and toString( ). The equals( ) method compares the contents of two objects. It returns true if the objects are equivalent, and false otherwise. The toString( ) method returns a string that contains a description of the object on which it is called. Also, this method is automatically called when an object is output using println( ). Many classes override this method. for more information on toString( ).












Lecture Notes –19
Topc: Interface
Using the keyword interface, you can fully abstract a class’ interface from its implementation. That is, using interface, you can specify what a class must do, but not how it does it. Interfaces are syntactically similar to classes, but they lack instance variables, and their methods are declared without any body. In practice, this means that you can define interfaces which don’t make assumptions about how they are implemented. Once it is defined, any number of classes can implement an interface. Also, one class can implement any number of interfaces. To implement an interface, a class must create the complete set of methods defined by the interface. However, each class is free to determine the details of its own implementation. By providing the interface keyword, Java allows you to fully utilize the “one interface, multiple methods” aspect of polymorphism.
Interfaces are designed to support dynamic method resolution at run time. Normally, in order for a method to be called from one class to another, both classes need to be present at compile time so the Java compiler can check to ensure that the method signatures are compatible. This requirement by itself makes for a static and nonextensible classing environment. Inevitably in a system like this, functionality gets pushed up higher and higher in the class hierarchy so that the mechanisms will be
available to more and more subclasses. Interfaces are designed to avoid this problem. They disconnect the definition of a method or set of methods from the inheritance hierarchy. Since interfaces are in a different hierarchy from classes, it is possible for classes that are unrelated in terms of the class hierarchy to implement the same interface. This is where the real power of interfaces is realized. Interfaces add most of the functionality that is required for many applications which
would normally resort to using multiple inheritance in a language such as C++. Defining an Interface An interface is defined much like a class. This is the general form of an interface:

access interface name {
return-type method-name1(parameter-list);
return-type method-name2(parameter-list);
type final-varname1 = value;

type final-varname2 = value;
// ...
return-type method-nameN(parameter-list);
type final-varnameN = value;
}
Here, access is either public or not used. When no access specifier is included, then default access results, and the interface is only available to other members of the package in which it is declared. When it is declared as public, the interface can be used by any other code. name is the name of the interface, and can be any valid identifier. Notice that the methods which are declared have no bodies. They end with a semicolon after the parameter list. They are, essentially, abstract methods; there can be no default implementation of any method specified within an interface. Each class that includes an interface must implement all of the methods. Variables can be declared inside of interface declarations. They are implicitly final and static, meaning they cannot be changed by the implementing class. They must also be initialized with a constant value. All methods and variables are implicitly public if the interface, itself, is declared as public. Here is an example of an interface definition. It declares a simple interface which contains one method called callback( ) that takes a single integer parameter.
interface Callback {
void callback(int param);
}
Implementing Interfaces
Once an interface has been defined, one or more classes can implement that interface. To implement an interface, include the implements clause in a class definition, and then create the methods defined by the interface. The general form of a class that includes the implements clause looks like this:
access class classname [extends superclass]
[implements interface [,interface...]] {
// class-body
}
Here, access is either public or not used. If a class implements more than one interface, the interfaces are separated with a comma. If a class implements two interfaces that declare the same method, then the same method will be used by clients of either interface. The methods that implement an interface must be declared public. Also, the type signature of the implementing method must match exactly the type signature specified in the interface definition. Here is a small example class that implements the Callback interface shown earlier.
class Client implements Callback {
// Implement Callback's interface
public void callback(int p) {
System.out.println("callback called with " + p);
}
}
Notice that callback( ) is declared using the public access specifier. When you implement an interface method, it must be declared as public. It is both permissible and common for classes that implement interfaces to define additional members of their own. For example, the following version of Client implements callback( ) and adds the method nonIfaceMeth( ):
class Client implements Callback {
// Implement Callback's interface
public void callback(int p) {
System.out.println("callback called with " + p);
}
void nonIfaceMeth() {
System.out.println("Classes that implement interfaces " +
"may also define other members, too.");
}
}
Accessing Implementations Through Interface References
You can declare variables as object references that use an interface rather than a class type. Any instance of any class that implements the declared interface can be referred to by such a variable. When you call a method through one of these references, the correct version will be called based on the actual instance of the interface being referred to. This is one of the key features of interfaces. The method to be executed is looked up dynamically at run time, allowing classes to be created later than the code which calls methods on them. The calling code can dispatch through an interface without having to know anything about the “callee.” This process is similar to using a superclass
Because dynamic lookup of a method at run time incurs a significant overhead when compared with the normal method invocation in Java, you should be careful not to use interfaces casually in performance-critical code. The following example calls the callback( ) method via an interface reference
variable:
class TestIface {
public static void main(String args[]) {
Callback c = new Client();
c.callback(42);
}
}
The output of this program is shown here:
callback called with 42
Notice that variable c is declared to be of the interface type Callback, yet it was assigned an instance of Client. Although c can be used to access the callback( ) method, it cannot access any other members of the Client class. An interface reference variable only has knowledge of the methods declared by its interface declaration. Thus, c could not be used to access nonIfaceMeth( ) since it is defined by Client but not Callback. While the preceding example shows, mechanically, how an interface reference variable can access an implementation object, it does not demonstrate the polymorphic power of such a reference. To sample this usage, first create the second implementation
of Callback, shown here:
// Another implementation of Callback.
class AnotherClient implements Callback {
// Implement Callback's interface
public void callback(int p) {
System.out.println("Another version of callback");
System.out.println("p squared is " + (p*p));
}
}
Now, try the following class:
class TestIface2 {
public static void main(String args[]) {
Callback c = new Client();
AnotherClient ob = new AnotherClient();
c.callback(42);
c = ob; // c now refers to AnotherClient object
c.callback(42);
}
}
The output from this program is shown here:
callback called with 42
Another version of callback
p squared is 1764
As you can see, the version of callback( ) that is called is determined by the type of object that c refers to at run time. While this is a very simple example, you will see another, more practical one shortly.
Partial Implementations
If a class includes an interface but does not fully implement the methods defined by
that interface, then that class must be declared as abstract. For example:
abstract class Incomplete implements Callback {
int a, b;
void show() {
System.out.println(a + " " + b);
}
// ...
}
Here, the class Incomplete does not implement callback( ) and must be declared as abstract. Any class that inherits Incomplete must implement callback( ) or be declared abstract itself. Applying Interfaces
To understand the power of interfaces, let’s look at a more practical example. In earlier chapters you developed a class called Stack that implemented a simple fixed-size stack. However, there are many ways to implement a stack. For example, the stack can be of a fixed size or it can be “growable.” The stack can also be held in an array, a linked list, a binary tree, and so on. No matter how the stack is implemented, the interface to the stack remains the same. That is, the methods push( ) and pop( ) define the interface to the stack independently of the details of the implementation. Because the interface to a stack is separate from its implementation, it is easy to define a stack interface, leaving it to each implementation to define the specifics. Let’s look at two examples.
First, here is the interface that defines an integer stack. Put this in a file called IntStack.java. This interface will be used by both stack implementations.
// Define an integer stack interface.
interface IntStack {
void push(int item); // store an item
int pop(); // retrieve an item
}
The following program creates a class called FixedStack that implements a fixed-length version of an integer stack:
Interfaces Can Be Extended
One interface can inherit another by use of the keyword extends. The syntax is the
same as for inheriting classes. When a class implements an interface that inherits
another interface, it must provide implementations for all methods defined within
the interface inheritance chain. Following is an example:
// One interface can extend another.
interface A {
void meth1();
void meth2();
}
// B now includes meth1() and meth2() -- it adds meth3().
interface B extends A {
void meth3();
}
// This class must implement all of A and B
class MyClass implements B {
public void meth1() {
System.out.println("Implement meth1().");
}
public void meth2() {
System.out.println("Implement meth2().");
}
public void meth3() {
System.out.println("Implement meth3().");
}
}
class IFExtend {
public static void main(String arg[]) {
MyClass ob = new MyClass();
ob.meth1();
ob.meth2();
ob.meth3();
}
}
As an experiment you might want to try removing the implementation for meth1( ) in MyClass. This will cause a compile-time error. As stated earlier, any class that implements an interface must implement all methods defined by that interface, including any that are inherited from other interfaces. Although the examples we’ve included in this book do not make frequent use of packages or interfaces, both of these tools are an important part of the Java programming environment. Virtually all real programs and applets that you write in Java will be contained within packages. A number will probably implement interfaces as well. It is important, therefore, that you be comfortable with their usage.




















Lecture Notes –20
Topc: Packages.
In the preceding chapters, the name of each example class was taken from the same name space. This means that a unique name had to be used for each class to avoid name collisions. After a while, without some way to manage the name space, you could run out of convenient, descriptive names for individual classes. You also need some way to be assured that the name you choose for a class will be reasonably unique and not collide with class names chosen by other programmers. (Imagine a small group of programmers fighting over who gets to use the name “Foobar” as a class name. Or, imagine the entire Internet community arguing over who first named
a class “Espresso.”) Thankfully, Java provides a mechanism for partitioning the class name space into more manageable chunks. This mechanism is the package. The package is both a naming and a visibility control mechanism. You can define classes inside a package that are not accessible by code outside that package. You can also define class members that are only exposed to other members of the same package. This allows your classes to have intimate knowledge of each other, but not expose that knowledge to the rest of the world.
Defining a Package
To create a package is quite easy: simply include a package command as the first statement in a Java source file. Any classes declared within that file will belong to the specified package. The package statement defines a name space in which classes are stored. If you omit the package statement, the class names are put into the default package, which has no name. (This is why you haven’t had to worry about packages before now.) While the default package is fine for short, sample programs, it is inadequate for real applications. Most of the time, you will define a package for
your code.
This is the general form of the package statement:
package pkg;
Here, pkg is the name of the package. For example, the following statement creates a package called MyPackage.
package MyPackage;
Java uses file system directories to store packages. For example, the .class files for any classes you declare to be part of MyPackage must be stored in a directory called MyPackage. Remember that case is significant, and the directory name must match the package name exactly. More than one file can include the same package statement. The package statement simply specifies to which package the classes defined in a file belong. It does not exclude other classes in other files from being part of that same package. Most real-world packages are spread across many files.
You can create a hierarchy of packages. To do so, simply separate each package name from the one above it by use of a period. The general form of a multileveled package statement is shown here:
package pkg1[.pkg2[.pkg3]];
A package hierarchy must be reflected in the file system of your Java development system. For example, a package declared as
package java.awt.image;
needs to be stored in java/awt/image, java\awt\image, or java:awt:image on your
UNIX, Windows, or Macintosh file system, respectively. Be sure to choose your
package names carefully. You cannot rename a package without renaming the
directory in which the classes are stored.
.
A Short Package Example
Keeping the preceding discussion in mind, you can try this simple package:
// A simple package
package MyPack;
class Balance {
String name;
double bal;
Balance(String n, double b) {
name = n;
bal = b;
}
void show() {
if(bal<0)
System.out.print("--> ");
System.out.println(name + ": $" + bal);
}
}
class AccountBalance {
public static void main(String args[]) {
Balance current[] = new Balance[3];
current[0] = new Balance("K. J. Fielding", 123.23);
current[1] = new Balance("Will Tell", 157.02);
current[2] = new Balance("Tom Jackson", -12.33);
for(int i=0; i<3; i++) current[i].show();
}
}
Call this file AccountBalance.java, and put it in a directory called MyPack. Next, compile the file. Make sure that the resulting .class file is also in the MyPack directory. Then try executing the AccountBalance class, using the following command line:java MyPack.AccountBalance Remember, you will need to be in the directory above MyPack when you execute this command, or to have your CLASSPATH environmental variable set appropriately.
As explained, AccountBalance is now part of the package MyPack. This means that it cannot be executed by itself. That is, you cannot use this command line:
java AccountBalance
AccountBalance must be qualified with its package name.
Access Protection
In the preceding chapters, you learned about various aspects of Java’s access control mechanism and its access specifiers. For example, you already know that access to a private member of a class is granted only to other members of that class. Packages add another dimension to access control. As you will see, Java provides many levels of protection to allow fine-grained control over the visibility of variables and methods within classes, subclasses, and packages. Classes and packages are both means of encapsulating and containing the name space and scope of variables and methods. Packages act as containers for classes and
other subordinate packages. Classes act as containers for data and code. The class is Java’s smallest unit of abstraction. Because of the interplay between classes and packages, Java addresses four categories of visibility for class members:
¦ Subclasses in the same package
¦ Non-subclasses in the same package
¦ Subclasses in different packages
¦ Classes that are neither in the same package nor subclasses
The three access specifiers, private, public, and protected, provide a variety of ways to produce the many levels of access required by these categories. Table 9-1 sums up the interactions. While Java’s access control mechanism may seem complicated, we can simplify it as follows. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member does not have an explicit access specification, it is visible to subclasses as well as to other classes in the same package. This is the default access. If you want to allow an element to be seen outside your current package, but only to classes that subclass your class directly, then declare
that element protected.
Table 9-1 applies only to members of classes. A class has only two possible access levels: default and public. When a class is declared as public, it is accessible by any other code. If a class has default access, then it can only be accessed by other code within its same package.
Private No modifier Protected Public
Same class Yes Yes Yes Yes
Same package
subclass
No Yes Yes Yes
Same package
non-subclass
No Yes Yes Yes
Different
package
subclass
No No Yes Yes
Different
package
non-subclass
No No No Yes
Table 9-1. Class Member Access
An Access Example
The following example shows all combinations of the access control modifiers. This example has two packages and five classes. Remember that the classes for the two different packages need to be stored in directories named after their respective packages—in this case, p1 and p2. The source for the first package defines three classes: Protection, Derived, and
SamePackage. The first class defines four int variables in each of the legal protection modes. The variable n is declared with the default protection, n_pri is private, n_pro is protected, and n_pub is public.
Each subsequent class in this example will try to access the variables in an instance of this class. The lines that will not compile due to access restrictions are commented out by use of the single-line comment //. Before each of these lines is a comment listing the places from which this level of protection would allow access.
The second class, Derived, is a subclass of Protection in the same package, p1. This grants Derived access to every variable in Protection except for n_pri, the private one. The third class, SamePackage, is not a subclass of Protection, but is in the same package
and also has access to all but n_pri.
This is file Protection.java:
package p1;
public class Protection {
int n = 1;
private int n_pri = 2;
protected int n_pro = 3;
public int n_pub = 4;
public Protection() {
System.out.println("base constructor");
System.out.println("n = " + n);
System.out.println("n_pri = " + n_pri);
System.out.println("n_pro = " + n_pro);
System.out.println("n_pub = " + n_pub);
}
}
This is file Derived.java:
package p1;
class Derived extends Protection {
Derived() {
System.out.println("derived constructor");
System.out.println("n = " + n);
// class only
// System.out.println("n_pri = " + n_pri);
System.out.println("n_pro = " + n_pro);
System.out.println("n_pub = " + n_pub);
}
}
This is file SamePackage.java:
package p1;
class SamePackage {
SamePackage() {
Protection p = new Protection();
System.out.println("same package constructor");
System.out.println("n = " + p.n);
// class only
// System.out.println("n_pri = " + p.n_pri);
System.out.println("n_pro = " + p.n_pro);
System.out.println("n_pub = " + p.n_pub);
}
}
Following is the source code for the other package, p2. The two classes defined in
p2 cover the other two conditions which are affected by access control. The first class, Protection2, is a subclass of p1.Protection. This grants access to all of p1.Protection’s variables except for n_pri (because it is private) and n, the variable declared with the default protection. Remember, the default only allows access from within the class or the package, not extra-package subclasses. Finally, the class OtherPackage has access to only one variable, n_pub, which was declared public.
This is file Protection2.java:
package p2;
class Protection2 extends p1.Protection {
Protection2() {
System.out.println("derived other package constructor");
// class or package only
// System.out.println("n = " + n);
// class only
// System.out.println("n_pri = " + n_pri);
System.out.println("n_pro = " + n_pro);
System.out.println("n_pub = " + n_pub);
}
}
This is file OtherPackage.java:
package p2;
class OtherPackage {
OtherPackage() {
p1.Protection p = new p1.Protection();
System.out.println("other package constructor");
// class or package only
// System.out.println("n = " + p.n);
// class only
// System.out.println("n_pri = " + p.n_pri);
// class, subclass or package only
// System.out.println("n_pro = " + p.n_pro);
System.out.println("n_pub = " + p.n_pub);
}
}
If you wish to try these two packages, here are two test files you can use. The one
for package p1 is shown here:
// Demo package p1.
package p1;
// Instantiate the various classes in p1.
public class Demo {
public static void main(String args[]) {
Protection ob1 = new Protection();
Derived ob2 = new Derived();
SamePackage ob3 = new SamePackage();
}
}
The test file for p2 is shown next:
// Demo package p2.
package p2;
// Instantiate the various classes in p2.
public class Demo {
public static void main(String args[]) {
Protection2 ob1 = new Protection2();
OtherPackage ob2 = new OtherPackage();
}
}
Importing Packages
Given that packages exist and are a good mechanism for compartmentalizing diverse classes from each other, it is easy to see why all of the built-in Java classes are stored in packages. There are no core Java classes in the unnamed default package; all of the standard classes are stored in some named package. Since classes within packages must be fully qualified with their package name or names, it could become tedious to type in the long dot-separated package path name for every class you want to use. For this reason, Java includes the import statement to bring certain classes, or entire
packages, into visibility. Once imported, a class can be referred to directly, using only its name. The import statement is a convenience to the programmer and is not technically needed to write a complete Java program. If you are going to refer to a few dozen classes in your application, however, the import statement will save a lot
of typing.
In a Java source file, import statements occur immediately following the package statement (if it exists) and before any class definitions. This is the general form of the
import statement:
import pkg1[.pkg2].(classname*);
Here, pkg1 is the name of a top-level package, and pkg2 is the name of a subordinate package inside the outer package separated by a dot (.). There is no practical limit on the depth of a package hierarchy, except that imposed by the file system. Finally, you specify either an explicit classname or a star (*), which indicates that the Java compiler should import the entire package. This code fragment shows both forms in use:
import java.util.Date;
import java.io.*;
The star form may increase compilation time—especially if you import several large packages. For this reason it is a good idea to explicitly name the classes that you want to use rather than importing whole packages. However, the star form has absolutely no effect on the run-time performance or size of your classes. All of the standard Java classes included with Java are stored in a package called
java. The basic language functions are stored in a package inside of the java package called java.lang. Normally, you have to import every package or class that you want to use, but since Java is useless without much of the functionality in java.lang, it is implicitly imported by the compiler for all programs. This is equivalent to the following
line being at the top of all of your programs:
import java.lang.*;
If a class with the same name exists in two different packages that you import using the star form, the compiler will remain silent, unless you try to use one of the classes. In that case, you will get a compile-time error and have to explicitly name
the class specifying its package.
Any place you use a class name, you can use its fully qualified name, which
includes its full package hierarchy. For example, this fragment uses an import
statement:
import java.util.*;
class MyDate extends Date {
}
The same example without the import statement looks like this:
class MyDate extends java.util.Date {
}
As shown in Table 9-1, when a package is imported, only those items within the
package declared as public will be available to non-subclasses in the importing code. For example, if you want the Balance class of the package MyPack shown earlier to be available as a stand-alone class for general use outside of MyPack, then you will need to declare it as public and put it into its own file, as shown here:
package MyPack;
/* Now, the Balance class, its constructor, and its
show() method are public. This means that they can
be used by non-subclass code outside their package.
*/
public class Balance {
String name;
double bal;
public Balance(String n, double b) {
name = n;
bal = b;
}
public void show() {
if(bal<0)
System.out.print("--> ");
System.out.println(name + ": $" + bal);
}
}
As you can see, the Balance class is now public. Also, its constructor and its show( ) method are public, too. This means that they can be accessed by any type of code outside the MyPack package. For example, here TestBalance imports MyPack
and is then able to make use of the Balance class:
import MyPack.*;
class TestBalance {
public static void main(String args[]) {
/* Because Balance is public, you may use Balance
class and call its constructor. */
Balance test = new Balance("J. J. Jaspers", 99.88);
test.show(); // you may also call show()
}
}
As an experiment, remove the public specifier from the Balance class and then try
compiling TestBalance. As explained, errors will result.



















Lecture Notes –21
Topc: I.O Handling
As you may have noticed while reading the preceding 11 chapters, not much use has been made of I/O in the example programs. In fact, aside from print( ) and println( ), none of the I/O methods have been used significantly. The reason is simple: most real applications of Java are not text-based, console programs. Rather, they are graphically oriented applets that rely upon Java’s Abstract Window Toolkit (AWT) for interaction with the user. Although text-based programs are excellent as teaching examples, they do not constitute an important use for Java in the real world. Also, Java’s support for console I/O is limited and somewhat awkward to use—even in simple example
programs. Text-based console I/O is just not very important to Java programming. The preceding paragraph notwithstanding, Java does provide strong, flexible support for I/O as it relates to files and networks. Java’s I/O system is cohesive and consistent. In fact, once you understand its fundamentals, the rest of the I/O system is
easy to master.
Streams
Java programs perform I/O through streams. A stream is an abstraction that either produces or consumes information. A stream is linked to a physical device by the Java I/O system. All streams behave in the same manner, even if the actual physical devices to which they are linked differ. Thus, the same I/O classes and methods can be applied to any type of device. This means that an input stream can abstract many different kinds of input: from a disk file, a keyboard, or a network socket. Likewise, an output stream may refer to the console, a disk file, or a network connection. Streams are a
clean way to deal with input/output without having every part of your code understand the difference between a keyboard and a network, for example. Java implements streams within class hierarchies defined in the java.io package. If you are familiar with C/C++/C#, then you are already familiar with the concept of the
stream. Java’s approach to streams is loosely the same. Byte Streams and Character Streams Java 2 defines two types of streams: byte and character. Byte streams provide a convenient means for handling input and output of bytes. Byte streams are used, for example, when reading or writing binary data. Character streams provide a convenient means for handling input and output of characters. They use Unicode and, therefore, can be internationalized. Also, in some cases, character streams are more efficient than byte streams. The original version of Java (Java 1.0) did not include character streams and, thus,
all I/O was byte-oriented. Character streams were added by Java 1.1, and certain byte-oriented classes and methods were deprecated. This is why older code that doesn’t use character streams should be updated to take advantage of them, where appropriate. One other point: at the lowest level, all I/O is still byte-oriented. The character-based streams simply provide a convenient and efficient means for handling characters. An overview of both byte-oriented streams and character-oriented streams is
presented in the following sections.
The Byte Stream Classes
Byte streams are defined by using two class hierarchies. At the top are two abstract classes: InputStream and OutputStream. Each of these abstract classes has several concrete subclasses, that handle the differences between various devices, such as disk files, network connections, and even memory buffers. The byte stream classes are shown in Table 12-1. A few of these classes are discussed later in this section. Others are described in Part II. Remember, to use the stream classes, you must import java.io. The abstract classes InputStream and OutputStream define several key methods that the other stream classes implement. Two of the most important are read( ) and
write( ), which, respectively, read and write bytes of data. Both methods are declared as abstract inside InputStream and OutputStream. They are overridden by derived stream classes.
The Character Stream Classes
Character streams are defined by using two class hierarchies. At the top are two abstract classes, Reader and Writer. These abstract classes handle Unicode character streams. Java has several concrete subclasses of each of these. The character stream classes are shown in Table 12-2. The abstract classes Reader and Writer define several key methods that the other stream classes implement. Two of the most important methods are read( ) and write( ), which read and write characters of data, respectively. These methods are overridden by derived stream classes.
Stream Class Meaning
BufferedInputStream Buffered input stream
BufferedOutputStream Buffered output stream
ByteArrayInputStream Input stream that reads from a byte array
ByteArrayOutputStream Output stream that writes to a byte array
DataInputStream An input stream that contains methods for
reading the Java standard data types
DataOutputStream An output stream that contains methods for
writing the Java standard data types
FileInputStream Input stream that reads from a file
FileOutputStream Output stream that writes to a file
FilterInputStream Implements InputStream
FilterOutputStream Implements OutputStream
InputStream Abstract class that describes stream input
OutputStream Abstract class that describes stream output
PipedInputStream Input pipe
PipedOutputStream Output pipe
PrintStream Output stream that contains print( ) and
println( )
PushbackInputStream Input stream that supports one-byte “unget,”
which returns a byte to the input stream
RandomAccessFile Supports random access file I/O
SequenceInputStream Input stream that is a combination of two or
more input streams that will be read
sequentially, one after the other
Stream Class Meaning
BufferedReader Buffered input character stream
BufferedWriter Buffered output character stream
CharArrayReader Input stream that reads from a character array
CharArrayWriter Output stream that writes to a character array
FileReader Input stream that reads from a file
FileWriter Output stream that writes to a file
FilterReader Filtered reader
FilterWriter Filtered writer
InputStreamReader Input stream that translates bytes to characters
LineNumberReader Input stream that counts lines
OutputStreamWriter Output stream that translates characters
to bytes
PipedReader Input pipe
PipedWriter Output pipe
PrintWriter Output stream that contains print( ) and
println( )
PushbackReader Input stream that allows characters to be
returned to the input stream
Reader Abstract class that describes character
stream input
StringReader Input stream that reads from a string
StringWriter Output stream that writes to a string
Writer Abstract class that describes character
stream output
Table 12-2. The Character Stream I/O Classes
The Predefined Streams
As you know, all Java programs automatically import the java.lang package. This package defines a class called System, which encapsulates several aspects of the run-time environment. For example, using some of its methods, you can obtain the current time and the settings of various properties associated with the system. System
also contains three predefined stream variables, in, out, and err. These fields are declared as public and static within System. This means that they can be used by any other part of your program and without reference to a specific System object. System.out refers to the standard output stream. By default, this is the console. System.in refers to standard input, which is the keyboard by default. System.err refers to the standard error stream, which also is the console by default. However, these
streams may be redirected to any compatible I/O device.
System.in is an object of type InputStream; System.out and System.err are objects
of type PrintStream. These are byte streams, even though they typically are used to
read and write characters from and to the console. As you will see, you can wrap these
within character-based streams, if desired.
The preceding chapters have been using System.out in their examples. You can use
System.err in much the same way. As explained in the next section, use of System.in is
a little more complicated.
Reading Console Input
The following program demonstrates read( ) by reading characters from the console
until the user types a “q”:
// Use a BufferedReader to read characters from the console.
import java.io.*;
class BRRead {
public static void main(String args[])
throws IOException
{
char c;
BufferedReader br = new
BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter characters, 'q' to quit.");
LANGUAGE
// read characters
do {
c = (char) br.read();
System.out.println(c);
} while(c != 'q');
}
}
Here is a sample run:
Enter characters, 'q' to quit.
123abcq
1
2
3
a
b
c
q
This output may look a little different from what you expected, because System.in is line buffered, by default. This means that no input is actually passed to the program until you press ENTER. As you can guess, this does not make read( ) particularly valuable for interactive, console input. Reading Strings
To read a string from the keyboard, use the version of readLine( ) that is a member of the BufferedReader class. Its general form is shown here: String readLine( ) throws IOException
As you can see, it returns a String object.
The following program demonstrates BufferedReader and the readLine( ) method; the program reads and displays lines of text until you enter the word “stop”:
// Read a string from console using a BufferedReader.
import java.io.*;
class BRReadLines {
public static void main(String args[])
throws IOException
{
// create a BufferedReader using System.in
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String str;
System.out.println("Enter lines of text.");
System.out.println("Enter 'stop' to quit.");
do {
str = br.readLine();
System.out.println(str);
} while(!str.equals("stop"));
}
}
The next example creates a tiny text editor. It creates an array of String objects and then reads in lines of text, storing each line in the array. It will read up to 100 lines or until you enter “stop”. It uses a BufferedReader to read from the console.
// A tiny editor.
import java.io.*;
class TinyEdit {
public static void main(String args[])
throws IOException
{
// create a BufferedReader using System.in
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String str[] = new String[100];
System.out.println("Enter lines of text.");
System.out.println("Enter 'stop' to quit.");
for(int i=0; i<100; i++) {
str[i] = br.readLine();
if(str[i].equals("stop")) break;
}
System.out.println("\nHere is your file:");
THE
JAVA
LANGUAGE
// display the lines
for(int i=0; i<100; i++) {
if(str[i].equals("stop")) break;
System.out.println(str[i]);
}
}
}
Here is a sample run:
Enter lines of text.
Enter 'stop' to quit.
This is line one.
This is line two.
Java makes working with strings easy.
Just create String objects.
stop
Here is your file:
This is line one.
This is line two.
Java makes working with strings easy.
Just create String objects.
Writing Console Output
Console output is most easily accomplished with print( ) and println( ), described earlier, which are used in most of the examples in this book. These methods are defined by the class PrintStream (which is the type of the object referenced by System.out). Even though System.out is a byte stream, using it for simple program output is still acceptable. However, a character-based alternative is described in the next section.
Because PrintStream is an output stream derived from OutputStream, it also implements the low-level method write( ). Thus, write( ) can be used to write to the console. The simplest form of write( ) defined by PrintStream is shown here: void write(int byteval) This method writes to the stream the byte specified by byteval. Although byteval is declared as an integer, only the low-order eight bits are written. Here is a short example that uses write( ) to output the character “A” followed by a newline to the screen:
// Demonstrate System.out.write().
class WriteDemo {
public static void main(String args[]) {
int b;
b = 'A';
System.out.write(b);
System.out.write('\n');
}
}
You will not often use write( ) to perform console output (although doing so might be useful in some situations), because print( ) and println( ) are substantially easier to use. The PrintWriter Class
Although using System.out to write to the console is still permissible under Java, its use is recommended mostly for debugging purposes or for sample programs, such as those found in this book. For real-world programs, the recommended method of writing to the console when using Java is through a PrintWriter stream. PrintWriter is one of the character-based classes. Using a character-based class for console output
makes it easier to internationalize your program.
PrintWriter defines several constructors. The one we will use is shown here: PrintWriter(OutputStream outputStream, boolean flushOnNewline) Here, outputStream is an object of type OutputStream, and flushOnNewline controls whether Java flushes the output stream every time a println( ) method is called. If flushOnNewline is true, flushing automatically takes place. If false, flushing is not
automatic.
PrintWriter supports the print( ) and println( ) methods for all types including Object. Thus, you can use these methods in the same way as they have been used with System.out. If an argument is not a simple type, the PrintWriter methods call the object’s toString( ) method and then print the result.
To write to the console by using a PrintWriter, specify System.out for the output stream and flush the stream after each newline. For example, this line of code creates a PrintWriter that is connected to console output:
PrintWriter pw = new PrintWriter(System.out, true);
The following application illustrates using a PrintWriter to handle console output:
// Demonstrate PrintWriter
import java.io.*;
public class PrintWriterDemo {
public static void main(String args[]) {
PrintWriter pw = new PrintWriter(System.out, true);
pw.println("This is a string");
int i = -7;
pw.println(i);
double d = 4.5e-7;
pw.println(d);
}
}
The output from this program is shown here:
This is a string
-7
4.5E-7
Remember, there is nothing wrong with using System.out to write simple text output to the console when you are learning Java or debugging your programs. However, using a PrintWriter will make your real-world applications easier to internationalize. Because no advantage is gained by using a PrintWriter in the sample programs shown in this book, we will continue to use System.out to write
to the console.
Reading and Writing Files
This chapter examines the basics of file I/O.
Two of the most often-used stream classes are FileInputStream and FileOutputStream, which create byte streams linked to files. To open a file, you simply create an object of one of these classes, specifying the name of the file as an argument to the constructor. While both classes support additional, overridden constructors, the following are the forms that we will be using: FileInputStream(String fileName) throws FileNotFoundException FileOutputStream(String fileName) throws FileNotFoundException Here, fileName specifies the name of the file that you want to open. When you create an input stream, if the file does not exist, then FileNotFoundException is thrown. For output streams, if the file cannot be created, then FileNotFoundException is thrown. When an output file is opened, any preexisting file by the same name is destroyed. In earlier versions of Java, FileOutputStream( ) threw an IOException when an output file could not be created. This was changed by Java 2. When you are done with a file, you should close it by calling close( ). It is defined by both FileInputStream and FileOutputStream, as shown here: void close( ) throws IOException
To read from a file, you can use a version of read( ) that is defined within FileInputStream. The one that we will use is shown here:
int read( ) throws IOException
Each time that it is called, it reads a single byte from the file and returns the byte as an integer value. read( ) returns –1 when the end of the file is encountered. It can throw an IOException. The following program uses read( ) to input and display the contents of a text file, the name of which is specified as a command-line argument. Note the try/catch blocks that handle the two errors that might occur when this program is used—the specified file not being found or the user forgetting to include the name of the file. You can use this same approach whenever you use command-line arguments.
/* Display a text file.
To use this program, specify the name
of the file that you want to see.
For example, to see a file called TEST.TXT,
use the following command line.
java ShowFile TEST.TXT
*/
import java.io.*;
class ShowFile {
public static void main(String args[])
throws IOException
{
int i;
FileInputStream fin;
try {
fin = new FileInputStream(args[0]);
} catch(FileNotFoundException e) {
System.out.println("File Not Found");
return;
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Usage: ShowFile File");
return;
}
// read characters until EOF is encountered
do {
i = fin.read();
if(i != -1) System.out.print((char) i);
} while(i != -1);
fin.close();
}
}
To write to a file, you will use the write( ) method defined by FileOutputStream. Its simplest form is shown here:
void write(int byteval) throws IOException This method writes the byte specified by byteval to the file. Although byteval is declared as an integer, only the low-order eight bits are written to the file. If an error occurs during writing, an IOException is thrown. The next example uses write( ) to copy a
text file:

/* Copy a text file.
To use this program, specify the name
of the source file and the destination file.
For example, to copy a file called FIRST.TXT
to a file called SECOND.TXT, use the following
command line.
java CopyFile FIRST.TXT SECOND.TXT
*/
import java.io.*;
class CopyFile {
public static void main(String args[])
throws IOException
{
int i;
FileInputStream fin;
FileOutputStream fout;
try {
// open input file
try {
fin = new FileInputStream(args[0]);
} catch(FileNotFoundException e) {
System.out.println("Input File Not Found");
return;
}
// open output file
try {
fout = new FileOutputStream(args[1]);
} catch(FileNotFoundException e) {
System.out.println("Error Opening Output File");
return;
}
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Usage: CopyFile From To");
return;
}
// Copy File
try {
do {
i = fin.read();
JAVA
LANGUAGE
if(i != -1) fout.write(i);
} while(i != -1);
} catch(IOException e) {
System.out.println("File Error");
}
fin.close();
fout.close();
}
}

Lecture Notes –22
Topc: Exception Handling
T an exception is a run-time error. In computer languages that do not support exception handling, errors must be checked and handled manually—typically through the use of error codes, and so on. This approach is as cumbersome as it is troublesome. Java’s exception handling avoids these problems and, in the process, brings run-time error management into the object-oriented world.
For the most part, exception handling has not changed since the original version of Java. However, Java 2, version 1.4 has added a new subsystem called the chained exception facility. This feature is described near the end of this chapter. Exception-Handling Fundamentals A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of code. When an exceptional condition arises, an object representing that exception is created and thrown in the method that caused the error. That method may choose to handle the exception itself, or pass it on. Either way, at some point, the exception is caught and processed. Exceptions can be generated by the
Java run-time system, or they can be manually generated by your code. Exceptions thrown by Java relate to fundamental errors that violate the rules of the Java language or the constraints of the Java execution environment. Manually generated exceptions are typically used to report some error condition to the caller of a method. Java exception handling is managed via five keywords: try, catch, throw, throws,
and finally. Briefly, here is how they work. Program statements that you want to monitor for exceptions are contained within a try block. If an exception occurs within the try block, it is thrown. Your code can catch this exception (using catch) and handle it in some rational manner. System-generated exceptions are automatically thrown by the Java run-time system. To manually throw an exception, use the keyword throw. Any exception that is thrown out of a method must be specified as such by a throws
clause. Any code that absolutely must be executed before a method returns is put in a finally block.
This is the general form of an exception-handling block:
try {
// block of code to monitor for errors
}
catch (ExceptionType1 exOb) {
// exception handler for ExceptionType1
}
catch (ExceptionType2 exOb) {
// exception handler for ExceptionType2
}
// ...
finally {
// block of code to be executed before try block ends
}
Here, ExceptionType is the type of exception that has occurred. The remainder of this
chapter describes how to apply this framework.
Exception Types
All exception types are subclasses of the built-in class Throwable. Thus, Throwable is at the top of the exception class hierarchy. Immediately below Throwable are two subclasses that partition exceptions into two distinct branches. One branch is headed by Exception. This class is used for exceptional conditions that user programs should catch. This is also the class that you will subclass to create your own custom exception types. There is an important subclass of Exception, called RuntimeException.
Exceptions of this type are automatically defined for the programs that you write and include things such as division by zero and invalid array indexing. The other branch is topped by Error, which defines exceptions that are not expected to be caught under normal circumstances by your program. Exceptions of type Error are used by the Java run-time system to indicate errors having to do with the run-time environment, itself. Stack overflow is an example of such an error. This chapter will not be dealing with exceptions of type Error, because these are typically created in
response to catastrophic failures that cannot usually be handled by your program. Uncaught Exceptions Before you learn how to handle exceptions in your program, it is useful to see what happens when you don’t handle them. This small program includes an expression that intentionally causes a divide-by-zero error.
class Exc0 {
public static void main(String args[]) {
int d = 0;
int a = 42 / d;
}
}
When the Java run-time system detects the attempt to divide by zero, it constructs a
new exception object and then throws this exception. This causes the execution of Exc0 to stop, because once an exception has been thrown, it must be caught by an exception handler and dealt with immediately. In this example, we haven’t supplied any exception handlers of our own, so the exception is caught by the default handler provided by the Java run-time system. Any exception that is not caught by your program will ultimately be processed by the default handler. The default handler displays a string describing the exception, prints a stack trace from the point at which the exception occurred, and terminates the program.
Here is the output generated when this example is executed.
java.lang.ArithmeticException: / by zero
at Exc0.main(Exc0.java:4)
Notice how the class name, Exc0; the method name, main; the filename, Exc0.java; and the line number, 4, are all included in the simple stack trace. Also, notice that the type of the exception thrown is a subclass of Exception called ArithmeticException, which more specifically describes what type of error happened. As discussed later in this chapter, Java supplies several built-in exception types that match the various sorts of run-time errors that can be generated. The stack trace will always show the sequence of method invocations that led up to the error. For example, here is another version of the preceding program that introduces the same error but in a method separate from main( ):
class Exc1 {
static void subroutine() {
int d = 0;
int a = 10 / d;
}
public static void main(String args[]) {
Exc1.subroutine();
}
}
The resulting stack trace from the default exception handler shows how the entire call stack is displayed:
java.lang.ArithmeticException: / by zero
at Exc1.subroutine(Exc1.java:4)
at Exc1.main(Exc1.java:7)
As you can see, the bottom of the stack is main’s line 7, which is the call to subroutine( ), which caused the exception at line 4. The call stack is quite useful for debugging, because it pinpoints the precise sequence of steps that led to the error.
Using try and catch Although the default exception handler provided by the Java run-time system is useful for debugging, you will usually want to handle an exception yourself. Doing so provides two benefits. First, it allows you to fix the error. Second, it prevents the program from automatically terminating. Most users would be confused (to say the least) if your program stopped running and printed a stack trace whenever an error occurred! Fortunately, it is quite easy to prevent this.
To guard against and handle a run-time error, simply enclose the code that you want to monitor inside a try block. Immediately following the try block, include a catch clause that specifies the exception type that you wish to catch. To illustrate how easily this can be done, the following program includes a try block and a catch clause which processes the ArithmeticException generated by the division-by-zero error:
class Exc2 {
public static void main(String args[]) {
int d, a;
try { // monitor a block of code.
d = 0;
a = 42 / d;
System.out.println("This will not be printed.");
} catch (ArithmeticException e) { // catch divide-by-zero error
System.out.println("Division by zero.");
}
System.out.println("After catch statement.");
}
}
This program generates the following output:
Division by zero.
After catch statement.
Notice that the call to println( ) inside the try block is never executed. Once an exception is thrown, program control transfers out of the try block into the catch block. Put differently, catch is not “called,” so execution never “returns” to the try block from a catch. Thus, the line “This will not be printed.” is not displayed. Once the catch statement has executed, program control continues with the next line in the program following the entire try/catch mechanism. A try and its catch statement form a unit. The scope of the catch clause is restricted to those statements specified by the immediately preceding try statement. A catch statement cannot catch an exception thrown by another try statement (except in the case of nested try statements, described shortly). The statements that are protected by try must be surrounded by curly braces. (That is, they must be within a block.) You cannot use try on a single statement. The goal of most well-constructed catch clauses should be to resolve the exceptional condition and then continue on as if the error had never happened. For example, in the next program each iteration of the for loop obtains two random integers. Those two integers are divided by each other, and the result is used to divide
the value 12345. The final result is put into a. If either division operation causes a divide-by-zero error, it is caught, the value of a is set to zero, and the program continues.
// Handle an exception and move on.
import java.util.Random;
class HandleError {
public static void main(String args[]) {
int a=0, b=0, c=0;
Random r = new Random();
for(int i=0; i<32000; i++) {
try {
b = r.nextInt();
c = r.nextInt();
a = 12345 / (b/c);
} catch (ArithmeticException e) {
System.out.println("Division by zero.");
a = 0; // set a to zero and continue
}
System.out.println("a: " + a);
}
}
}
Displaying a Description of an Exception Throwable overrides the toString( ) method (defined by Object) so that it returns a string containing a description of the exception. You can display this description in a println( ) statement by simply passing the exception as an argument. For example, the
catch block in the preceding program can be rewritten like this:
catch (ArithmeticException e) {
System.out.println("Exception: " + e);
a = 0; // set a to zero and continue
}
When this version is substituted in the program, and the program is run, each
divide-by-zero error displays the following message:
Exception: java.lang.ArithmeticException: / by zero
While it is of no particular value in this context, the ability to display a description of an exception is valuable in other circumstances—particularly when you are experimenting with exceptions or when you are debugging. Multiple catch Clauses In some cases, more than one exception could be raised by a single piece of code. To handle this type of situation, you can specify two or more catch clauses, each catching a different type of exception. When an exception is thrown, each catch statement is
inspected in order, and the first one whose type matches that of the exception is executed. After one catch statement executes, the others are bypassed, and execution continues after the try/catch block. The following example traps two different exception types:
// Demonstrate multiple catch statements.
class MultiCatch {
public static void main(String args[]) {
try {
int a = args.length;
System.out.println("a = " + a);
int b = 42 / a;
int c[] = { 1 };
c[42] = 99;
} catch(ArithmeticException e) {
System.out.println("Divide by 0: " + e);
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Array index oob: " + e);
}
System.out.println("After try/catch blocks.");
}
}
This program will cause a division-by-zero exception if it is started with no commandline parameters, since a will equal zero. It will survive the division if you provide a command-line argument, setting a to something larger than zero. But it will cause an ArrayIndexOutOfBoundsException, since the int array c has a length of 1, yet the program attempts to assign a value to c[42].
Here is the output generated by running it both ways:
C:\>java MultiCatch
a = 0
Divide by 0: java.lang.ArithmeticException: / by zero
After try/catch blocks.
C:\>java MultiCatch TestArg
a = 1
Array index oob: java.lang.ArrayIndexOutOfBoundsException After try/catch blocks. When you use multiple catch statements, it is important to remember that exception subclasses must come before any of their superclasses. This is because a catch statement that uses a superclass will catch exceptions of that type plus any of its subclasses. Thus, a subclass would never be reached if it came after its superclass.
Further, in Java, unreachable code is an error. For example, consider the following
program:
/* This program contains an error.
A subclass must come before its superclass in
a series of catch statements. If not,
unreachable code will be created and a
compile-time error will result.
*/
class SuperSubCatch {
public static void main(String args[]) {
try {
int a = 0;
int b = 42 / a;
} catch(Exception e) {
System.out.println("Generic Exception catch.");
}
/* This catch is never reached because
ArithmeticException is a subclass of Exception. */
catch(ArithmeticException e) { // ERROR - unreachable
System.out.println("This is never reached.");
}
}
}
If you try to compile this program, you will receive an error message stating that the second catch statement is unreachable because the exception has already been
caught. Since ArithmeticException is a subclass of Exception, the first catch statement
will handle all Exception-based errors, including ArithmeticException. This means
that the second catch statement will never execute. To fix the problem, reverse the
order of the catch statements.
Nested try Statements
The try statement can be nested. That is, a try statement can be inside the block of
another try. Each time a try statement is entered, the context of that exception is
pushed on the stack. If an inner try statement does not have a catch handler for a
particular exception, the stack is unwound and the next try statement’s catch handlers
are inspected for a match. This continues until one of the catch statements succeeds, or
until all of the nested try statements are exhausted. If no catch statement matches, then
the Java run-time system will handle the exception. Here is an example that uses
nested try statements:
// An example of nested try statements.
class NestTry {
public static void main(String args[]) {
try {
int a = args.length;
/* If no command-line args are present,
the following statement will generate
a divide-by-zero exception. */
int b = 42 / a;
System.out.println("a = " + a);
try { // nested try block
/* If one command-line arg is used,
then a divide-by-zero exception
will be generated by the following code. */
if(a==1) a = a/(a-a); // division by zero
/* If two command-line args are used,
then generate an out-of-bounds exception. */
if(a==2) {
int c[] = { 1 };
c[42] = 99; // generate an out-of-bounds exception
}
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Array index out-of-bounds: " + e);
}
} catch(ArithmeticException e) {
System.out.println("Divide by 0: " + e);
}
}
}
As you can see, this program nests one try block within another. The program works as follows. When you execute the program with no command-line arguments, a divide-by-zero exception is generated by the outer try block. Execution of the program by one command-line argument generates a divide-by-zero exception from within the nested try block. Since the inner block does not catch this exception, it is passed on to the outer try block, where it is handled. If you execute the program with two command-line arguments, an array boundary exception is generated from within the inner try block. Here are sample runs that illustrate each case:
C:\>java NestTry
Divide by 0: java.lang.ArithmeticException: / by zero
C:\>java NestTry One
a = 1
Divide by 0: java.lang.ArithmeticException: / by zero
C:\>java NestTry One Two
a = 2
Array index out-of-bounds:
java.lang.ArrayIndexOutOfBoundsException
Nesting of try statements can occur in less obvious ways when method calls are involved. For example, you can enclose a call to a method within a try block. Inside that method is another try statement. In this case, the try within the method is still nested inside the outer try block, which calls the method. Here is the previous program recoded so that the nested try block is moved inside the method nesttry( ):
/* Try statements can be implicitly nested via
calls to methods. */
class MethNestTry {
static void nesttry(int a) {
try { // nested try block
/* If one command-line arg is used,
then a divide-by-zero exception
will be generated by the following code. */
if(a==1) a = a/(a-a); // division by zero
/* If two command-line args are used,
then generate an out-of-bounds exception. */
if(a==2) {
int c[] = { 1 };
c[42] = 99; // generate an out-of-bounds exception
}
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Array index out-of-bounds: " + e);
}
}
public static void main(String args[]) {
try {
int a = args.length;
/* If no command-line args are present,
the following statement will generate
a divide-by-zero exception. */
int b = 42 / a;
System.out.println("a = " + a);
nesttry(a);
} catch(ArithmeticException e) {
System.out.println("Divide by 0: " + e);
}
}
}
The output of this program is identical to that of the preceding example. Throw So far, you have only been catching exceptions that are thrown by the Java run-time system. However, it is possible for your program to throw an exception explicitly, using the throw statement. The general form of throw is shown here:
throw ThrowableInstance;
Here, ThrowableInstance must be an object of type Throwable or a subclass of
Throwable. Simple types, such as int or char, as well as non-Throwable classes, such as String and Object, cannot be used as exceptions. There are two ways you can obtain a Throwable object: using a parameter into a catch clause, or creating one with the new operator.
The flow of execution stops immediately after the throw statement; any subsequent
statements are not executed. The nearest enclosing try block is inspected to see if it has a catch statement that matches the type of the exception. If it does find a match, control is transferred to that statement. If not, then the next enclosing try statement is inspected, and so on. If no matching catch is found, then the default exception handler halts the program and prints the stack trace.
Here is a sample program that creates and throws an exception. The handler that
catches the exception rethrows it to the outer handler.
// Demonstrate throw.
class ThrowDemo {
static void demoproc() {
try {
throw new NullPointerException("demo");
} catch(NullPointerException e) {
System.out.println("Caught inside demoproc.");
throw e; // rethrow the exception
}
}
public static void main(String args[]) {
try {
demoproc();
} catch(NullPointerException e) {
System.out.println("Recaught: " + e);
}
}
}
This program gets two chances to deal with the same error. First, main( ) sets up an exception context and then calls demoproc( ). The demoproc( ) method then sets up another exception-handling context and immediately throws a new instance of NullPointerException, which is caught on the next line. The exception is then rethrown. Here is the resulting output:
Caught inside demoproc.
Recaught: java.lang.NullPointerException: demo
The program also illustrates how to create one of Java’s standard exception objects. Pay close attention to this line:
throw new NullPointerException("demo");
Here, new is used to construct an instance of NullPointerException. All of Java’s built-in run-time exceptions have at least two constructors: one with no parameter and one that takes a string parameter. When the second form is used, the argument specifies a string that describes the exception. This string is displayed when the object is used as an argument to print( ) or println( ). It can also be obtained by a call to
getMessage( ), which is defined by Throwable.
throws
If a method is capable of causing an exception that it does not handle, it must specify this behavior so that callers of the method can guard themselves against that exception. You do this by including a throws clause in the method’s declaration. A throws clause lists the types of exceptions that a method might throw. This is necessary for all exceptions, except those of type Error or RuntimeException, or any of their subclasses. All other exceptions that a method can throw must be declared in the throws clause. If they are not, a compile-time error will result.
This is the general form of a method declaration that includes a throws clause:
type method-name(parameter-list) throws exception-list
{
// body of method
}
Following is an example of an incorrect program that tries to throw an exception
that it does not catch. Because the program does not specify a throws clause to declare
this fact, the program will not compile.
// This program contains an error and will not compile.
class ThrowsDemo {
static void throwOne() {
System.out.println("Inside throwOne.");
throw new IllegalAccessException("demo");
}
public static void main(String args[]) {
throwOne();
}
}
To make this example compile, you need to make two changes. First, you need to
declare that throwOne( ) throws IllegalAccessException. Second, main( ) must define
a try/catch statement that catches this exception.
The corrected example is shown here:
// This is now correct.
class ThrowsDemo {
static void throwOne() throws IllegalAccessException {
System.out.println("Inside throwOne.");
throw new IllegalAccessException("demo");
}
public static void main(String args[]) {
try {
throwOne();
} catch (IllegalAccessException e) {
System.out.println("Caught " + e);
}
}
}
Here is the output generated by running this example program:
inside throwOne
caught java.lang.IllegalAccessException: demo
















Lecture Notes –23

Topc: Next part of Exception Handling

finally
When exceptions are thrown, execution in a method takes a rather abrupt, nonlinear path that alters the normal flow through the method. Depending upon how the method is coded, it is even possible for an exception to cause the method to return prematurely. This could be a problem in some methods. For example, if a method opens a file upon entry and closes it upon exit, then you will not want the code that closes the file to be bypassed by the exception-handling mechanism. The finally
keyword is designed to address this contingency. finally creates a block of code that will be executed after a try/catch block has completed and before the code following the try/catch block. The finally block will execute whether or not an exception is thrown. If an exception is thrown, the finally
block will execute even if no catch statement matches the exception. Any time a method is about to return to the caller from inside a try/catch block, via an uncaught exception or an explicit return statement, the finally clause is also executed just before the method returns. This can be useful for closing file handles and freeing up any other resources that might have been allocated at the beginning of a method with the intent of disposing of them before returning. The finally clause is optional. However, each try statement requires at least one catch or a finally clause. Here is an example program that shows three methods that exit in various ways, none without executing their finally clauses:
// Demonstrate finally.
class FinallyDemo {
// Through an exception out of the method.
static void procA() {
try {
System.out.println("inside procA");
throw new RuntimeException("demo");
} finally {
System.out.println("procA's finally");
}
}
// Return from within a try block.
static void procB() {
try {
System.out.println("inside procB");
return;
} finally {
System.out.println("procB's finally");
}
}
// Execute a try block normally.
static void procC() {
try {
System.out.println("inside procC");
} finally {
System.out.println("procC's finally");
}
}
public static void main(String args[]) {
try {
procA();
} catch (Exception e) {
System.out.println("Exception caught");
}
procB();
procC();
}
}
In this example, procA( ) prematurely breaks out of the try by throwing an exception. The finally clause is executed on the way out. procB( )’s try statement is exited via a return statement. The finally clause is executed before procB( ) returns. In procC( ), the try statement executes normally, without error. However, the finally block is still executed. If a finally block is associated with a try, the finally block will be executed upon
conclusion of the try.
Here is the output generated by the preceding program:
inside procA
procA’s finally
Exception caught
inside procB
procB’s finally
inside procC
procC’s finally
Java’s Built-in Exceptions
Inside the standard package java.lang, Java defines several exception classes. A few have been used by the preceding examples. The most general of these exceptions are subclasses of the standard type RuntimeException. Since java.lang is implicitly imported into all Java programs, most exceptions derived from RuntimeException are automatically available. Furthermore, they need not be included in any method’s throws list. In the language of Java, these are called unchecked exceptions because the compiler does not check to see if a method handles or throws these exceptions. The unchecked exceptions defined in java.lang are listed in Table 10-1. Table 10-2 lists those
exceptions defined by java.lang that must be included in a method’s throws list if that
method can generate one of these exceptions and does not handle it itself. These are
called checked exceptions. Java defines several other types of exceptions that relate to its
various class libraries.
Exception Meaning
ArithmeticException Arithmetic error, such as
divide-by-zero.
ArrayIndexOutOfBoundsException Array index is out-of-bounds.
ArrayStoreException Assignment to an array element of an
incompatible type.
ClassCastException Invalid cast.
IllegalArgumentException Illegal argument used to invoke a
method.
IllegalMonitorStateException Illegal monitor operation, such as
waiting on an unlocked thread.
IllegalStateException Environment or application is in
incorrect state.
IllegalThreadStateException Requested operation not compatible
with current thread state.
IndexOutOfBoundsException Some type of index is out-of-bounds.
NegativeArraySizeException Array created with a negative size.
Table 10-1. Java’s UncheckedRuntimeException Subclasses
Exception Meaning
ClassNotFoundException Class not found.
CloneNotSupportedException Attempt to clone an object that does not
implement the Cloneable interface.
IllegalAccessException Access to a class is denied.
InstantiationException Attempt to create an object of an
abstract class or interface.
InterruptedException One thread has been interrupted by
another thread.
NoSuchFieldException A requested field does not exist.
NoSuchMethodException A requested method does not exist.
Table 10-2. Java’s Checked Exceptions Defined in java.lang
Exception Meaning
NullPointerException Invalid use of a null reference.
NumberFormatException Invalid conversion of a string to a
numeric format.
SecurityException Attempt to violate security.
StringIndexOutOfBounds Attempt to index outside the bounds of
a string.
UnsupportedOperationException An unsupported operation was
encountered.
Table 10-1. Java’s UncheckedRuntimeException Subclasses(continued)
Creating Your Own Exception Subclasses
Although Java’s built-in exceptions handle most common errors, you will probably want to create your own exception types to handle situations specific to your applications. This is quite easy to do: just define a subclass of Exception (which is, of course, a subclass of Throwable). Your subclasses don’t need to actually implement anything—it is their existence in the type system that allows you to use them as exceptions. The Exception class does not define any methods of its own. It does, of course, inherit those methods provided by Throwable. Thus, all exceptions, including those that you create, have the methods defined by Throwable available to them. They are shown in Table 10-3. Notice that several methods were added by Java 2, version 1.4.
You may also wish to override one or more of these methods in exception classes that
you create.
Method Description
Throwable fillInStackTrace( ) Returns a Throwable object that contains a completed stack trace. This object can be
rethrown.
Throwable getCause( ) Returns the exception that underlies the current exception. If there is no underlying
exception, null is returned. Added by Java 2,
version 1.4.
String getLocalizedMessage( ) Returns a localized description of the exception. String getMessage( ) Returns a description of the exception. StackTraceElement[ ] getStackTrace( ) Returns an array that contains the stack trace, one element at a time as an array of
StackTraceElement. The method at the top of the stack is the last method called before
the exception was thrown. This method is found in the first element of the array.
The StackTraceElement class gives your program access to information about each
element in the trace, such as its method name. Added by Java 2, version 1.4
Throwable initCause(Throwable
causeExc)
Associates causeExc with the invoking
exception as a cause of the invoking exception.
Returns a reference to the exception. Added
by Java 2, version 1.4
Table 10-3. The Methods Defined byThrowable
The following example declares a new subclass of Exception and then uses that subclass to signal an error condition in a method. It overrides the toString( ) method, allowing the description of the exception to be displayed using println( ).
// This program creates a custom exception type.
class MyException extends Exception {
private int detail;
MyException(int a) {
detail = a;
}
public String toString() {
return "MyException[" + detail + "]";
}
}
class ExceptionDemo {
static void compute(int a) throws MyException {
System.out.println("Called compute(" + a + ")");
void printStackTrace( ) Displays the stack trace.
void printStackTrace(PrintStream
stream)
Sends the stack trace to the specified stream.
void printStackTrace(PrintWriter
stream)
Sends the stack trace to the specified stream.
void setStackTrace(StackTraceElement
elements[ ])
Sets the stack trace to the elements passed
in elements. This method is for specialized
applications, not normal use. Added by Java 2,
version 1.4
String toString( ) Returns a String object containing a
description of the exception. This method
is called by println( ) when outputting a
Throwable object.
Table 10-3. The Methods Defined byThrowable (continued)
if(a > 10)
throw new MyException(a);
System.out.println("Normal exit");
}
public static void main(String args[]) {
try {
compute(1);
compute(20);
} catch (MyException e) {
System.out.println("Caught " + e);
}
}
}
This example defines a subclass of Exception called MyException. This subclass
is quite simple: it has only a constructor plus an overloaded toString( ) method that
displays the value of the exception. The ExceptionDemo class defines a method
named compute( ) that throws a MyException object. The exception is thrown when
compute( )’s integer parameter is greater than 10. The main( ) method sets up an
exception handler for MyException, then calls compute( ) with a legal value (less
than 10) and an illegal one to show both paths through the code. Here is the result:
Called compute(1)
Normal exit
Called compute(20)
Caught MyException[20]
Chained Exceptions
Java 2, version 1.4 added a new feature to the exception subsystem: chained exceptions. The chained exception feature allows you to associate another exception with an exception. This second exception describes the cause of the first exception. For example, imagine a situation in which a method throws an ArithmeticException because of an attempt to divide by zero. However, the actual cause of the problem was that an I/O error occurred, which caused the divisor to be set improperly. Although the method must certainly throw an ArithmeticException, since that is the error that occurred, you might also want to let the calling code know that the underlying cause was an I/O error. Chained exceptions let you handle this, and any other situation in which layers of exceptions exist. To allow chained exceptions, Java 2, version 1.4 added two constructors and two
methods to Throwable. The constructors are shown here.
Throwable(Throwable causeExc)
Throwable(String msg, Throwable causeExc)
In the first form, causeExc is the exception that causes the current exception. That is, auseExc is the underlying reason that an exception occurred. The second form allows you to specify a description at the same time that you specify a cause exception. These two constructors have also been added to the Error, Exception, and RuntimeException classes. The chained exception methods added to Throwable are getCause( ) and initCause( ). These methods are shown in Table 10-3, and are repeated here for the sake of discussion. Throwable getCause( ) Throwable initCause(Throwable causeExc) The getCause( ) method returns the exception that underlies the current exception. If there is no underlying exception, null is returned. The initCause( ) method associates causeExc with the invoking exception and returns a reference to the exception. Thus, you
can associate a cause with an exception after the exception has been created. However, the cause exception can be set only once. Thus, you can call initCause( ) only once for each exception object. Furthermore, if the cause exception was set by a constructor, then you
can’t set it again using initCause( ). In general, initCause( ) is used to set a cause for legacy exception classes which don’t support the two additional constructors described earlier. At the time of this writing, most of Java’s built-in exceptions, such as ArithmeticException, do not define the additional constructors. Thus, you will use initCause( ) if you need to add an exception chain to these exceptions. When creating your own exception classes you will want to add the two chained-exception constructors if you will be using your exceptions in situations in which layered exceptions are possible. Here is an example that illustrates the mechanics of handling chained exceptions.
// Demonstrate exception chaining.
class ChainExcDemo {
static void demoproc() {
// create an exception
NullPointerException e =
new NullPointerException("top layer");
// add a cause
e.initCause(new ArithmeticException("cause"));
throw e;
}
public static void main(String args[]) {
try {
demoproc();
} catch(NullPointerException e) {
// display top level exception
System.out.println("Caught: " + e);
// display cause exception
System.out.println("Original cause: " +
e.getCause());
}
}
}
The output from the program is shown here.
Caught: java.lang.NullPointerException: top layer
Original cause: java.lang.ArithmeticException: cause
In this example, the top-level exception is NullPointerException. To it is added a cause exception, ArithmeticException. When the exception is thrown out of demoproc( ), it is caught by main( ). There, the top-level exception is displayed, followed by the underlying exception, which is obtained by calling getCause( ). Chained exceptions can be carried on to whatever depth is necessary. Thus, the
cause exception can, itself, have a cause. Be aware that overly long chains of exceptions may indicate poor design. Chained exceptions are not something that every program will need. However, in cases in which knowledge of an underlying cause is useful, they offer an elegant solution. Using Exceptions
Exception handling provides a powerful mechanism for controlling complex programs that have many dynamic run-time characteristics. It is important to think of try, throw, and catch as clean ways to handle errors and unusual boundary conditions in your program’s logic. If you are like most programmers, then you probably are used to returning an error code when a method fails. When you are programming in Java, you should break this habit. When a method can fail, have it throw an exception. This is a cleaner way to handle failure modes.
One last point: Java’s exception-handling statements should not be considered a general mechanism for nonlocal branching. If you do so, it will only confuse your code and make it hard to maintain.







Lecture Notes –24
Topc: 1. Multithreading
U parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution. Thus, multithreading is a specialized form of multitasking. You are almost certainly acquainted with multitasking, because it is supported by virtually all modern operating systems. However, there are two distinct types of multitasking: process-based and thread-based. It is important to understand the difference between the two. For most readers, process-based multitasking is the more familiar form. A process is, in essence, a program that is executing.
Thus, process-based multitasking is the feature that allows your computer to run two or more programs concurrently. For example, process-based multitasking enables you to run the Java compiler at the same time that you are using a text editor. In process-based multitasking, a program is the smallest unit of code that can be dispatched by the scheduler.
In a thread-based multitasking environment, the thread is the smallest unit of dispatchable code. This means that a single program can perform two or more tasks simultaneously. For instance, a text editor can format text at the same time that it is printing, as long as these two actions are being performed by two separate threads. Thus, process-based multitasking deals with the “big picture,” and thread-based multitasking handles the details. Multitasking threads require less overhead than multitasking processes. Processes
are heavyweight tasks that require their own separate address spaces. Interprocess communication is expensive and limited. Context switching from one process to another is also costly. Threads, on the other hand, are lightweight. They share the same address space and cooperatively share the same heavyweight process. Interthread communication is inexpensive, and context switching from one thread to the next is low cost. While Java programs make use of process-based multitasking environments, process-based multitasking is not under the control of Java. However, multithreaded
multitasking is.
Multithreading enables you to write very efficient programs that make maximum use of the CPU, because idle time can be kept to a minimum. This is especially important for the interactive, networked environment in which Java operates, because idle time is common. For example, the transmission rate of data over a network is much slower than the rate at which the computer can process it. Even local file system resources are read and written at a much slower pace than they can be processed by the CPU. And, of course, user input is much slower than the computer. In a traditional,
single-threaded environment, your program has to wait for each of these tasks to finish before it can proceed to the next one—even though the CPU is sitting idle most of the time. Multithreading lets you gain access to this idle time and put it to good use. If you have programmed for operating systems such as Windows 98 or Windows 2000, then you are already familiar with multithreaded programming. However, the fact that Java manages threads makes multithreading especially convenient, because many of the details are handled for you.
The Java Thread Model
The Java run-time system depends on threads for many things, and all the class libraries are designed with multithreading in mind. In fact, Java uses threads to enable the entire environment to be asynchronous. This helps reduce inefficiency by preventing the waste of CPU cycles. The value of a multithreaded environment is best understood in contrast to its counterpart. Single-threaded systems use an approach called an event loop with polling. In this model, a single thread of control runs in an infinite loop, polling a single event queue to decide what to do next. Once this polling mechanism returns with, say, a
signal that a network file is ready to be read, then the event loop dispatches control to the appropriate event handler. Until this event handler returns, nothing else can happen in the system. This wastes CPU time. It can also result in one part of a program dominating the system and preventing any other events from being processed. In general, in a singled-threaded environment, when a thread blocks (that is, suspends execution) because it is waiting for some resource, the entire program stops running. The benefit of Java’s multithreading is that the main loop/polling mechanism is
eliminated. One thread can pause without stopping other parts of your program. For example, the idle time created when a thread reads data from a network or waits for user input can be utilized elsewhere. Multithreading allows animation loops to sleep for a second between each frame without causing the whole system to pause. When a thread blocks in a Java program, only the single thread that is blocked pauses. All other threads continue to run. Threads exist in several states. A thread can be running. It can be ready to run as soon as it gets CPU time. A running thread can be suspended, which temporarily suspends its activity. A suspended thread can then be resumed, allowing it to pick up where it left off. A thread can be blocked when waiting for a resource. At any time, a thread can be terminated, which halts its execution immediately. Once terminated, a thread cannot be resumed.
Thread Priorities
Java assigns to each thread a priority that determines how that thread should be treated with respect to the others. Thread priorities are integers that specify the relative priority of one thread to another. As an absolute value, a priority is meaningless; a higher-priority thread doesn’t run any faster than a lower-priority thread if it is the only thread running. Instead, a thread’s priority is used to decide when to switch from one running thread to the next. This is called a context switch. The rules that determine
when a context switch takes place are simple:
Synchronization
Because multithreading introduces an asynchronous behavior to your programs, there must be a way for you to enforce synchronicity when you need it. For example, if you want two threads to communicate and share a complicated data structure, such as a linked list, you need some way to ensure that they don’t conflict with each other. That is, you must prevent one thread from writing data while another thread is in the
middle of reading it. For this purpose, Java implements an elegant twist on an age-old model of interprocess synchronization: the monitor. The monitor is a control mechanism first defined by C.A.R. Hoare. You can think of a monitor as a very small box that can hold only one thread. Once a thread enters a monitor, all other threads must wait until that thread exits the monitor. In this way, a monitor can be used to protect a shared
asset from being manipulated by more than one thread at a time.
Most multithreaded systems expose monitors as objects that your program must explicitly acquire and manipulate. Java provides a cleaner solution. There is no class “Monitor”; instead, each object has its own implicit monitor that is automatically entered when one of the object’s synchronized methods is called. Once a thread is inside a synchronized method, no other thread can call any other synchronized method on the same object. This enables you to write very clear and concise multithreaded code,
because synchronization support is built in to the language. Messaging you can’t directly refer to the ethereal state of a running thread, you will deal with it The Main Thread When a Java program starts up, one thread begins running immediately. This is usually called the main thread of your program, because it is the one that is executed when your program begins. The main thread is important for two reasons: ¦ It is the thread from which other “child” threads will be spawned.
¦ Often it must be the last thread to finish execution because it performs various shutdown actions.
Although the main thread is created automatically when your program is started, it can be controlled through a Thread object. To do so, you must obtain a reference to it by calling the method currentThread( ), which is a public static member of Thread. Its general form is shown here:
static Thread currentThread( )
This method returns a reference to the thread in which it is called. Once you have a
reference to the main thread, you can control it just like any other thread.
Let’s begin by reviewing the following example:
// Controlling the main Thread.
class CurrentThreadDemo {
public static void main(String args[]) {
Thread t = Thread.currentThread();
System.out.println("Current thread: " + t);
// change the name of the thread
t.setName("My Thread");
System.out.println("After name change: " + t);
try {
for(int n = 5; n > 0; n--) {
System.out.println(n);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println("Main thread interrupted");
}
}
}
In this program, a reference to the current thread (the main thread, in this case) is
obtained by calling currentThread( ), and this reference is stored in the local variable t.
Next, the program displays information about the thread. The program then calls
setName( ) to change the internal name of the thread. Information about the thread is
then redisplayed. Next, a loop counts down from five, pausing one second between
each line. The pause is accomplished by the sleep( ) method. The argument to sleep( )
specifies the delay period in milliseconds. Notice the try/catch block around this loop.
The sleep( ) method in Thread might throw an InterruptedException. This would
happen if some other thread wanted to interrupt this sleeping one. This example just
prints a message if it gets interrupted. In a real program, you would need to handle
this differently. Here is the output generated by this program:
Current thread: Thread[main,5,main]
After name change: Thread[My Thread,5,main]
5
4
3
2
1
Notice the output produced when t is used as an argument to println( ). This displays,
in order: the name of the thread, its priority, and the name of its group. By default, the
name of the main thread is main. Its priority is 5, which is the default value, and main
is also the name of the group of threads to which this thread belongs. A thread group is
a data structure that controls the state of a collection of threads as a whole. This process
is managed by the particular run-time environment and is not discussed in detail here.
After the name of the thread is changed, t is again output. This time, the new name of
the thread is displayed.
Let’s look more closely at the methods defined by Thread that are used in the
program. The sleep( ) method causes the thread from which it is called to suspend

Creating a Thread
In the most general sense, you create a thread by instantiating an object of type Thread.
Java defines two ways in which this can be accomplished:
¦ You can implement the Runnable interface.
¦ You can extend the Thread class, itself.
The following two sections look at each method, in turn.
Implementing Runnable
The easiest way to create a thread is to create a class that implements the Runnable
interface. Runnable abstracts a unit of executable code. You can construct a thread on
any object that implements Runnable. To implement Runnable, a class need only
implement a single method called run( ), which is declared like this:
public void run( )
Inside run( ), you will define the code that constitutes the new thread. It is
important to understand that run( ) can call other methods, use other classes, and
declare variables, just like the main thread can. The only difference is that run( )
establishes the entry point for another, concurrent thread of execution within your
program. This thread will end when run( ) returns.
After you create a class that implements Runnable, you will instantiate an object of
type Thread from within that class. Thread defines several constructors. The one that
we will use is shown here:
Thread(Runnable threadOb, String threadName)
In this constructor, threadOb is an instance of a class that implements the Runnable
interface. This defines where execution of the thread will begin. The name of the new
thread is specified by threadName.
After the new thread is created, it will not start running until you call its start( )
method, which is declared within Thread. In essence, start( ) executes a call to run( ).
The start( ) method is shown here:
void start( )
Here is an example that creates a new thread and starts it running:
// Create a second thread.
class NewThread implements Runnable {
Thread t;
NewThread() {
// Create a new, second thread
t = new Thread(this, "Demo Thread");
System.out.println("Child thread: " + t);
t.start(); // Start the thread
}
// This is the entry point for the second thread.
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}
} catch (InterruptedException e) {
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}
class ThreadDemo {
public static void main(String args[]) {
new NewThread(); // create a new thread
try {
for(int i = 5; i > 0; i--) {
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}
Inside NewThread’s constructor, a new Thread object is created by the following
statement:
t = new Thread(this, "Demo Thread");
Passing this as the first argument indicates that you want the new thread to call the
run( ) method on this object. Next, start( ) is called, which starts the thread of execution
beginning at the run( ) method. This causes the child thread’s for loop to begin. After
calling start( ), NewThread’s constructor returns to main( ). When the main thread
resumes, it enters its for loop. Both threads continue running, sharing the CPU, until
their loops finish. The output produced by this program is as follows:
Child thread: Thread[Demo Thread,5,main]
Main Thread: 5
Child Thread: 5
Child Thread: 4
Main Thread: 4
Child Thread: 3
Child Thread: 2
Main Thread: 3
Child Thread: 1
Exiting child thread.
Main Thread: 2
Main Thread: 1
Main thread exiting.
As mentioned earlier, in a multithreaded program, often the main thread must
be the last thread to finish running. In fact, for some older JVMs, if the main thread
finishes before a child thread has completed, then the Java run-time system may
“hang.” The preceding program ensures that the main thread finishes last, because
the main thread sleeps for 1,000 milliseconds between iterations, but the child thread
sleeps for only 500 milliseconds. This causes the child thread to terminate earlier than
the main thread. Shortly, you will see a better way to wait for a thread to finish.
Extending Thread
The second way to create a thread is to create a new class that extends Thread,
and then to create an instance of that class. The extending class must override the
run( ) method, which is the entry point for the new thread. It must also call start( )
to begin execution of the new thread. Here is the preceding program rewritten to
extend Thread:
// Create a second thread by extending Thread
class NewThread extends Thread {
NewThread() {
// Create a new, second thread
super("Demo Thread");
System.out.println("Child thread: " + this);
start(); // Start the thread
}
// This is the entry point for the second thread.
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}
} catch (InterruptedException e) {
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}
class ExtendThread {
public static void main(String args[]) {
new NewThread(); // create a new thread
try {
for(int i = 5; i > 0; i--) {
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}
This program generates the same output as the preceding version. As you can see, the
child thread is created by instantiating an object of NewThread, which is derived
from Thread.
Notice the call to super( ) inside NewThread. This invokes the following form of
the Thread constructor:
public Thread(String threadName)
Here, threadName specifies the name of the thread.
Choosing an Approach
At this point, you might be wondering why Java has two ways to create child threads,
and which approach is better. The answers to these questions turn on the same point.
The Thread class defines several methods that can be overridden by a derived class.
Of these methods, the only one that must be overridden is run( ). This is, of course, the
same method required when you implement Runnable. Many Java programmers feel
that classes should be extended only when they are being enhanced or modified in
some way. So, if you will not be overriding any of Thread’s other methods, it is
probably best simply to implement Runnable. This is up to you, of course. However,
throughout the rest of this chapter, we will create threads by using classes that
implement Runnable.
Creating Multiple Threads
So far, you have been using only two threads: the main thread and one child thread.
However, your program can spawn as many threads as it needs. For example, the
following program creates three child threads:
// Create multiple threads.
class NewThread implements Runnable {
String name; // name of thread
Thread t;
NewThread(String threadname) {
name = threadname;
t = new Thread(this, name);
System.out.println("New thread: " + t);
t.start(); // Start the thread
}
// This is the entry point for thread.
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println(name + ": " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println(name + "Interrupted");
}
System.out.println(name + " exiting.");
}
}
class MultiThreadDemo {
public static void main(String args[]) {
new NewThread("One"); // start threads
new NewThread("Two");
new NewThread("Three");
try {
// wait for other threads to end
Thread.sleep(10000);
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Main thread exiting.");
}
}
The output from this program is shown here:
New thread: Thread[One,5,main]
New thread: Thread[Two,5,main]
New thread: Thread[Three,5,main]
One: 5
Two: 5
Three: 5
One: 4
Two: 4
Three: 4
One: 3
Three: 3
Two: 3
One: 2
Three: 2
Two: 2
One: 1
Three: 1
Two: 1
One exiting.
Two exiting.
Three exiting.
Main thread exiting.
As you can see, once started, all three child threads share the CPU. Notice the call to
sleep(10000) in main( ). This causes the main thread to sleep for ten seconds and
ensures that it will finish last.
Using isAlive( ) and join( )
As mentioned, often you will want the main thread to finish last. In the preceding
examples, this is accomplished by calling sleep( ) within main( ), with a long enough
delay to ensure that all child threads terminate prior to the main thread. However,
this is hardly a satisfactory solution, and it also raises a larger question: How can one
thread know when another thread has ended? Fortunately, Thread provides a means
by which you can answer this question.
Two ways exist to determine whether a thread has finished. First, you can call
isAlive( ) on the thread. This method is defined by Thread, and its general form is
shown here:
final boolean isAlive( )
The isAlive( ) method returns true if the thread upon which it is called is still running.
It returns false otherwise.
While isAlive( ) is occasionally useful, the method that you will more commonly
use to wait for a thread to finish is called join( ), shown here:
final void join( ) throws InterruptedException
This method waits until the thread on which it is called terminates. Its name comes
from the concept of the calling thread waiting until the specified thread joins it.
Additional forms of join( ) allow you to specify a maximum amount of time that
you want to wait for the specified thread to terminate.
Here is an improved version of the preceding example that uses join( ) to ensure
that the main thread is the last to stop. It also demonstrates the isAlive( ) method.
// Using join() to wait for threads to finish.
class NewThread implements Runnable {
String name; // name of thread
Thread t;
NewThread(String threadname) {
name = threadname;
t = new Thread(this, name);
System.out.println("New thread: " + t);
t.start(); // Start the thread
}
// This is the entry point for thread.
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println(name + ": " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println(name + " interrupted.");
}
System.out.println(name + " exiting.");
}
}
class DemoJoin {
public static void main(String args[]) {
NewThread ob1 = new NewThread("One");
NewThread ob2 = new NewThread("Two");
NewThread ob3 = new NewThread("Three");
System.out.println("Thread One is alive: "
+ ob1.t.isAlive());
System.out.println("Thread Two is alive: "
+ ob2.t.isAlive());
System.out.println("Thread Three is alive: "
+ ob3.t.isAlive());
// wait for threads to finish
try {
System.out.println("Waiting for threads to finish.");
ob1.t.join();
ob2.t.join();
ob3.t.join();
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Thread One is alive: "
+ ob1.t.isAlive());
System.out.println("Thread Two is alive: "
+ ob2.t.isAlive());
System.out.println("Thread Three is alive: "
+ ob3.t.isAlive());
System.out.println("Main thread exiting.");
}
}
Sample output from this program is shown here:
New thread: Thread[One,5,main]
New thread: Thread[Two,5,main]
New thread: Thread[Three,5,main]
Thread One is alive: true
Thread Two is alive: true
Thread Three is alive: true
Waiting for threads to finish.
One: 5
Two: 5
Three: 5
One: 4
Two: 4
Three: 4
One: 3
Two: 3
Three: 3
One: 2
Two: 2
Three: 2
One: 1
Two: 1
Three: 1
Two exiting.
Three exiting.
One exiting.
Thread One is alive: false
Thread Two is alive: false
Thread Three is alive: false










































Lecture Notes –25
Topc: Next part of Multithreading
1. Main thread exiting.
2. Extending Threading


Main thread exiting.
As you can see, after the calls to join( ) return, the threads have stopped executing. Thread Priorities Thread priorities are used by the thread scheduler to decide when each thread should
be allowed to run. In theory, higher-priority threads get more CPU time than lowerpriority
threads. In practice, the amount of CPU time that a thread gets often depends
on several factors besides its priority. (For example, how an operating system implements
multitasking can affect the relative availability of CPU time.) A higher-priority thread
can also preempt a lower-priority one. For instance, when a lower-priority thread is
running and a higher-priority thread resumes (from sleeping or waiting on I/O, for
example), it will preempt the lower-priority thread.
In theory, threads of equal priority should get equal access to the CPU. But you need
to be careful. Remember, Java is designed to work in a wide range of environments.
Some of those environments implement multitasking fundamentally differently than
others. For safety, threads that share the same priority should yield control once in
a while. This ensures that all threads have a chance to run under a nonpreemptive
operating system. In practice, even in nonpreemptive environments, most threads
still get a chance to run, because most threads inevitably encounter some blocking
situation, such as waiting for I/O. When this happens, the blocked thread is suspended
and other threads can run. But, if you want smooth multithreaded execution, you are
better off not relying on this. Also, some types of tasks are CPU-intensive. Such threads
dominate the CPU. For these types of threads, you want to yield control occasionally,
so that other threads can run.
To set a thread’s priority, use the setPriority( ) method, which is a member of
Thread. This is its general form:
final void setPriority(int level)
Here, level specifies the new priority setting for the calling thread. The value of level
must be within the range MIN_PRIORITY and MAX_PRIORITY. Currently, these
values are 1 and 10, respectively. To return a thread to default priority, specify
NORM_PRIORITY, which is currently 5. These priorities are defined as final
variables within Thread.
You can obtain the current priority setting by calling the getPriority( ) method of
Thread, shown here:
final int getPriority( )
Implementations of Java may have radically different behavior when it comes to
scheduling. The Windows XP/98/NT/2000 version works, more or less, as you would
expect. However, other versions may work quite differently. Most of the inconsistencies
arise when you have threads that are relying on preemptive behavior, instead of
cooperatively giving up CPU time. The safest way to obtain predictable, cross-platform
behavior with Java is to use threads that voluntarily give up control of the CPU.
The following example demonstrates two threads at different priorities, which do
not run on a preemptive platform in the same way as they run on a nonpreemptive
platform. One thread is set two levels above the normal priority, as defined by
Thread.NORM_PRIORITY, and the other is set to two levels below it. The threads
are started and allowed to run for ten seconds. Each thread executes a loop, counting
the number of iterations. After ten seconds, the main thread stops both threads. The
number of times that each thread made it through the loop is then displayed.
// Demonstrate thread priorities.
class clicker implements Runnable {
int click = 0;
Thread t;
private volatile boolean running = true;
public clicker(int p) {
t = new Thread(this);
t.setPriority(p);
}
public void run() {
while (running) {
click++;
}
}
public void stop() {
running = false;
}
public void start() {
t.start();
}
}
class HiLoPri {
public static void main(String args[]) {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
clicker hi = new clicker(Thread.NORM_PRIORITY + 2);
clicker lo = new clicker(Thread.NORM_PRIORITY - 2);
lo.start();
hi.start();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
System.out.println("Main thread interrupted.");
}
lo.stop();
hi.stop();
// Wait for child threads to terminate.
try {
hi.t.join();
lo.t.join();
} catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
System.out.println("Low-priority thread: " + lo.click);
System.out.println("High-priority thread: " + hi.click);
}
}
The output of this program, shown as follows when run under Windows 98,
indicates that the threads did context switch, even though neither voluntarily yielded
the CPU nor blocked for I/O. The higher-priority thread got approximately 90 percent
of the CPU time.
Low-priority thread: 4408112
High-priority thread: 589626904
Of course, the exact output produced by this program depends on the speed of your
CPU and the number of other tasks running in the system. When this same program
is run under a nonpreemptive system, different results will be obtained.
One other note about the preceding program. Notice that running is preceded
by the keyword volatile. Although volatile is examined more carefully in the next
chapter, it is used here to ensure that the value of running is examined each time the
following loop iterates:
while (running) {
click++;
}
Without the use of volatile, Java is free to optimize the loop in such a way that a local
copy of running is created. The use of volatile prevents this optimization, telling Java
that running may change in ways not directly apparent in the immediate code.
Synchronization
When two or more threads need access to a shared resource, they need some way to
ensure that the resource will be used by only one thread at a time. The process by
which this is achieved is called synchronization. As you will see, Java provides unique,
language-level support for it.
Key to synchronization is the concept of the monitor (also called a semaphore). A
monitor is an object that is used as a mutually exclusive lock, or mutex. Only one thread
can own a monitor at a given time. When a thread acquires a lock, it is said to have
entered the monitor. All other threads attempting to enter the locked monitor will be
suspended until the first thread exits the monitor. These other threads are said to
be waiting for the monitor. A thread that owns a monitor can reenter the same monitor
if it so desires.
If you have worked with synchronization when using other languages, such as C or
C++, you know that it can be a bit tricky to use. This is because most languages do not,
themselves, support synchronization. Instead, to synchronize threads, your programs
need to utilize operating system primitives. Fortunately, because Java implements
synchronization through language elements, most of the complexity associated with
synchronization has been eliminated.
You can synchronize your code in either of two ways. Both involve the use of the
synchronized keyword, and both are examined here.
Using Synchronized Methods
Synchronization is easy in Java, because all objects have their own implicit monitor
associated with them. To enter an object’s monitor, just call a method that has been
modified with the synchronized keyword. While a thread is inside a synchronized
method, all other threads that try to call it (or any other synchronized method)
on the same instance have to wait. To exit the monitor and relinquish control of
the object to the next waiting thread, the owner of the monitor simply returns
from the synchronized method.
To understand the need for synchronization, let’s begin with a simple example that
does not use it—but should. The following program has three simple classes. The first
one, Callme, has a single method named call( ). The call( ) method takes a String
parameter called msg. This method tries to print the msg string inside of square
brackets. The interesting thing to notice is that after call( ) prints the opening bracket
and the msg string, it calls Thread.sleep(1000), which pauses the current thread for
one second.
The constructor of the next class, Caller, takes a reference to an instance of the
Callme class and a String, which are stored in target and msg, respectively. The
constructor also creates a new thread that will call this object’s run( ) method. The
thread is started immediately. The run( ) method of Caller calls the call( ) method on
the target instance of Callme, passing in the msg string. Finally, the Synch class starts
by creating a single instance of Callme, and three instances of Caller, each with a unique
message string. The same instance of Callme is passed to each Caller.
// This program is not synchronized.
class Callme {
void call(String msg) {
System.out.print("[" + msg);
try {
Thread.sleep(1000);
} catch(InterruptedException e) {
System.out.println("Interrupted");
}
System.out.println("]");
}
}
class Caller implements Runnable {
String msg;
Callme target;
Thread t;
public Caller(Callme targ, String s) {
target = targ;
msg = s;
t = new Thread(this);
t.start();
}
public void run() {
target.call(msg);
}
JAVA
LANGUAGE
}
class Synch {
public static void main(String args[]) {
Callme target = new Callme();
Caller ob1 = new Caller(target, "Hello");
Caller ob2 = new Caller(target, "Synchronized");
Caller ob3 = new Caller(target, "World");
// wait for threads to end
try {
ob1.t.join();
ob2.t.join();
ob3.t.join();
} catch(InterruptedException e) {
System.out.println("Interrupted");
}
}
}
Here is the output produced by this program:
Hello[Synchronized[World]
]
]
As you can see, by calling sleep( ), the call( ) method allows execution to switch to
another thread. This results in the mixed-up output of the three message strings. In
this program, nothing exists to stop all three threads from calling the same method, on
the same object, at the same time. This is known as a race condition, because the three
threads are racing each other to complete the method. This example used sleep( ) to
make the effects repeatable and obvious. In most situations, a race condition is more
subtle and less predictable, because you can’t be sure when the context switch will
occur. This can cause a program to run right one time and wrong the next.
To fix the preceding program, you must serialize access to call( ). That is, you must
restrict its access to only one thread at a time. To do this, you simply need to precede
call( )’s definition with the keyword synchronized, as shown here:
class Callme {
synchronized void call(String msg) {
...
This prevents other threads from entering call( ) while another thread is using it.
After synchronized has been added to call( ), the output of the program is as follows:
[Hello]
[Synchronized]
[World]
Any time that you have a method, or group of methods, that manipulates the
internal state of an object in a multithreaded situation, you should use the synchronized
keyword to guard the state from race conditions. Remember, once a thread enters any
synchronized method on an instance, no other thread can enter any other synchronized
method on the same instance. However, nonsynchronized methods on that instance
will continue to be callable.
The synchronized Statement
While creating synchronized methods within classes that you create is an easy and
effective means of achieving synchronization, it will not work in all cases. To understand
why, consider the following. Imagine that you want to synchronize access to objects of
a class that was not designed for multithreaded access. That is, the class does not use
synchronized methods. Further, this class was not created by you, but by a third party,
and you do not have access to the source code. Thus, you can’t add synchronized to
the appropriate methods within the class. How can access to an object of this class be
synchronized? Fortunately, the solution to this problem is quite easy: You simply put
calls to the methods defined by this class inside a synchronized block.
This is the general form of the synchronized statement:
synchronized(object) {
// statements to be synchronized
}
Here, object is a reference to the object being synchronized. A synchronized block
ensures that a call to a method that is a member of object occurs only after the current
thread has successfully entered object’s monitor.
Here is an alternative version of the preceding example, using a synchronized block
within the run( ) method:
// This program uses a synchronized block.
class Callme {
void call(String msg) {
System.out.print("[" + msg);
try {
THE
JAVA
LANGUAGE
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Interrupted");
}
System.out.println("]");
}
}
class Caller implements Runnable {
String msg;
Callme target;
Thread t;
public Caller(Callme targ, String s) {
target = targ;
msg = s;
t = new Thread(this);
t.start();
}
// synchronize calls to call()
public void run() {
synchronized(target) { // synchronized block
target.call(msg);
}
}
}
class Synch1 {
public static void main(String args[]) {
Callme target = new Callme();
Caller ob1 = new Caller(target, "Hello");
Caller ob2 = new Caller(target, "Synchronized");
Caller ob3 = new Caller(target, "World");
// wait for threads to end
try {
ob1.t.join();
ob2.t.join();
ob3.t.join();
} catch(InterruptedException e) {
System.out.println("Interrupted");
}
}
}
Here, the call( ) method is not modified by synchronized. Instead, the synchronized
statement is used inside Caller’s run( ) method. This causes the same correct output as
the preceding example, because each thread waits for the prior one to finish before
proceeding.
Interthread Communication
The preceding examples unconditionally blocked other threads from asynchronous
access to certain methods. This use of the implicit monitors in Java objects is powerful,
but you can achieve a more subtle level of control through interprocess communication.
As you will see, this is especially easy in Java.
As discussed earlier, multithreading replaces event loop programming by dividing
your tasks into discrete and logical units. Threads also provide a secondary benefit:
they do away with polling. Polling is usually implemented by a loop that is used to
check some condition repeatedly. Once the condition is true, appropriate action is
taken. This wastes CPU time. For example, consider the classic queuing problem,
where one thread is producing some data and another is consuming it. To make the
problem more interesting, suppose that the producer has to wait until the consumer is
finished before it generates more data. In a polling system, the consumer would waste
many CPU cycles while it waited for the producer to produce. Once the producer was
finished, it would start polling, wasting more CPU cycles waiting for the consumer to
finish, and so on. Clearly, this situation is undesirable.
To avoid polling, Java includes an elegant interprocess communication mechanism
via the wait( ), notify( ), and notifyAll( ) methods. These methods are implemented
as final methods in Object, so all classes have them. All three methods can be called
only from within a synchronized context. Although conceptually advanced from
a computer science perspective, the rules for using these methods are actually
quite simple:
¦ wait( ) tells the calling thread to give up the monitor and go to sleep until some
other thread enters the same monitor and calls notify( ).
¦ notify( ) wakes up the first thread that called wait( ) on the same object.
¦ notifyAll( ) wakes up all the threads that called wait( ) on the same object.
The highest priority thread will run first.

These methods are declared within Object, as shown here:
final void wait( ) throws InterruptedException
final void notify( )
final void notifyAll( )
Additional forms of wait( ) exist that allow you to specify a period of time to wait.
The following sample program incorrectly implements a simple form of the
producer/consumer problem. It consists of four classes: Q, the queue that you’re
trying to synchronize; Producer, the threaded object that is producing queue entries;
Consumer, the threaded object that is consuming queue entries; and PC, the tiny
class that creates the single Q, Producer, and Consumer.
// An incorrect implementation of a producer and consumer.
class Q {
int n;
synchronized int get() {
System.out.println("Got: " + n);
return n;
}
synchronized void put(int n) {
this.n = n;
System.out.println("Put: " + n);
}
}
class Producer implements Runnable {
Q q;
Producer(Q q) {
this.q = q;
new Thread(this, "Producer").start();
}
public void run() {
int i = 0;
while(true) {
q.put(i++);

}
}
}

class Consumer implements Runnable {
Q q;
Consumer(Q q) {
this.q = q;
new Thread(this, "Consumer").start();
}
public void run() {
while(true) {
q.get();
}
}
}
class PC {
public static void main(String args[]) {
Q q = new Q();
new Producer(q);
new Consumer(q);
System.out.println("Press Control-C to stop.");
}
}
Although the put( ) and get( ) methods on Q are synchronized, nothing stops the
producer from overrunning the consumer, nor will anything stop the consumer from
consuming the same queue value twice. Thus, you get the erroneous output shown
here (the exact output will vary with processor speed and task load):
Put: 1
Got: 1
Got: 1
Got: 1
Got: 1
Got: 1
Put: 2
Put: 3
Put: 4
Put: 5
Put: 6
Put: 7
Got: 7
As you can see, after the producer put 1, the consumer started and got the same 1 five
times in a row. Then, the producer resumed and produced 2 through 7 without letting
the consumer have a chance to consume them.
The proper way to write this program in Java is to use wait( ) and notify( ) to signal
in both directions, as shown here:
// A correct implementation of a producer and consumer.
class Q {
int n;
boolean valueSet = false;
synchronized int get() {
if(!valueSet)
try {
wait();
} catch(InterruptedException e) {
System.out.println("InterruptedException caught");
}
System.out.println("Got: " + n);
valueSet = false;
notify();
return n;
}
synchronized void put(int n) {
if(valueSet)
try {
wait();
} catch(InterruptedException e) {
System.out.println("InterruptedException caught");
}
this.n = n;
LANGUAGE
valueSet = true;
System.out.println("Put: " + n);
notify();
}
}
class Producer implements Runnable {
Q q;
Producer(Q q) {
this.q = q;
new Thread(this, "Producer").start();
}
public void run() {
int i = 0;
while(true) {
q.put(i++);
}
}
}
class Consumer implements Runnable {
Q q;
Consumer(Q q) {
this.q = q;
new Thread(this, "Consumer").start();
}
public void run() {
while(true) {
q.get();
}
}
}
class PCFixed {
public static void main(String args[]) {
Q q = new Q();
new Producer(q);
new Consumer(q);
System.out.println("Press Control-C to stop.");
}
}
Inside get( ), wait( ) is called. This causes its execution to suspend until the Producer
notifies you that some data is ready. When this happens, execution inside get( )
resumes. After the data has been obtained, get( ) calls notify( ). This tells Producer that
it is okay to put more data in the queue. Inside put( ), wait( ) suspends execution until
the Consumer has removed the item from the queue. When execution resumes, the
next item of data is put in the queue, and notify( ) is called. This tells the Consumer
that it should now remove it.
Here is some output from this program, which shows the clean synchronous behavior:
Put: 1
Got: 1
Put: 2
Got: 2
Put: 3
Got: 3
Put: 4
Got: 4
Put: 5
Got: 5
2. Deadlock
A special type of error that you need to avoid that relates specifically to multitasking
is deadlock, which occurs when two threads have a circular dependency on a pair of
synchronized objects. For example, suppose one thread enters the monitor on object X
and another thread enters the monitor on object Y. If the thread in X tries to call any
synchronized method on Y, it will block as expected. However, if the thread in Y, in
turn, tries to call any synchronized method on X, the thread waits forever, because to
access X, it would have to release its own lock on Y so that the first thread could
complete. Deadlock is a difficult error to debug for two reasons:
¦ In general, it occurs only rarely, when the two threads time-slice in just the
right way.
¦ It may involve more than two threads and two synchronized objects. (That is,
deadlock can occur through a more convoluted sequence of events than just
described.)
To understand deadlock fully, it is useful to see it in action. The next example creates
two classes, A and B, with methods foo( ) and bar( ), respectively, which pause briefly
before trying to call a method in the other class. The main class, named Deadlock, creates
an A and a B instance, and then starts a second thread to set up the deadlock condition.
The foo( ) and bar( ) methods use sleep( ) as a way to force the deadlock condition
to occur.
// An example of deadlock.
class A {
synchronized void foo(B b) {
String name = Thread.currentThread().getName();
System.out.println(name + " entered A.foo");
try {
Thread.sleep(1000);
} catch(Exception e) {
System.out.println("A Interrupted");
}
System.out.println(name + " trying to call B.last()");
b.last();
}
synchronized void last() {
System.out.println("Inside A.last");
}
}
class B {
synchronized void bar(A a) {
String name = Thread.currentThread().getName();
System.out.println(name + " entered B.bar");
try {
Thread.sleep(1000);
} catch(Exception e) {
System.out.println("B Interrupted");
}
System.out.println(name + " trying to call A.last()");
a.last();
}
synchronized void last() {
System.out.println("Inside A.last");
}
}
class Deadlock implements Runnable {
A a = new A();
B b = new B();
Deadlock() {
Thread.currentThread().setName("MainThread");
Thread t = new Thread(this, "RacingThread");
t.start();
a.foo(b); // get lock on a in this thread.
System.out.println("Back in main thread");
}
public void run() {
b.bar(a); // get lock on b in other thread.
System.out.println("Back in other thread");
}
public static void main(String args[]) {
new Deadlock();
}
}
When you run this program, you will see the output shown here:
MainThread entered A.foo
RacingThread entered B.bar
MainThread trying to call B.last()
RacingThread trying to call A.last()
Because the program has deadlocked, you need to press CTRL-C to end the program.
You can see a full thread and monitor cache dump by pressing CTRL-BREAK on a PC . You
will see that RacingThread owns the monitor on b, while it is waiting for the monitor
on a. At the same time, MainThread owns a and is waiting to get b. This program will
never complete. As this example illustrates, if your multithreaded program locks up
occasionally, deadlock is one of the first conditions that you should check for.
Suspending, Resuming, and Stopping Threads
Sometimes, suspending execution of a thread is useful. For example, a separate thread
can be used to display the time of day. If the user doesn’t want a clock, then its thread
can be suspended. Whatever the case, suspending a thread is a simple matter. Once
suspended, restarting the thread is also a simple matter.
The mechanisms to suspend, stop, and resume threads differ between Java 2 and
earlier versions. Although you should use the Java 2 approach for all new code, you
still need to understand how these operations were accomplished for earlier Java
environments. For example, you may need to update or maintain older, legacy code.
You also need to understand why a change was made for Java 2. For these reasons, the
next section describes the original way that the execution of a thread was controlled,
followed by a section that describes the approach required for Java 2.
Suspending, Resuming, and Stopping Threads Using
Java 1.1 and Earlier
Prior to Java 2, a program used suspend( ) and resume( ), which are methods defined by
Thread, to pause and restart the execution of a thread. They have the form shown below:
final void suspend( )
final void resume( )
The following program demonstrates these methods:
// Using suspend() and resume().
class NewThread implements Runnable {
String name; // name of thread
Thread t;
NewThread(String threadname) {
name = threadname;
t = new Thread(this, name);
System.out.println("New thread: " + t);
t.start(); // Start the thread
}
// This is the entry point for thread.
public void run() {
try {
for(int i = 15; i > 0; i--) {
System.out.println(name + ": " + i);
Thread.sleep(200);
}
} catch (InterruptedException e) {
System.out.println(name + " interrupted.");
}
System.out.println(name + " exiting.");
}
}
class SuspendResume {
public static void main(String args[]) {
NewThread ob1 = new NewThread("One");
NewThread ob2 = new NewThread("Two");
try {
Thread.sleep(1000);
ob1.t.suspend();
System.out.println("Suspending thread One");
Thread.sleep(1000);
ob1.t.resume();
System.out.println("Resuming thread One");
ob2.t.suspend();
System.out.println("Suspending thread Two");
Thread.sleep(1000);
ob2.t.resume();
System.out.println("Resuming thread Two");
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
// wait for threads to finish
try {
System.out.println("Waiting for threads to finish.");
ob1.t.join();
ob2.t.join();
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Main thread exiting.");
}
}
Sample output from this program is shown here:
New thread: Thread[One,5,main]
One: 15
New thread: Thread[Two,5,main]
Two: 15
One: 14
Two: 14
One: 13
Two: 13
One: 12
Two: 12
One: 11
Two: 11
Suspending thread One
Two: 10
Two: 9
Two: 8
Two: 7
Two: 6
Resuming thread One
Suspending thread Two
One: 10
One: 9
One: 8
One: 7
One: 6
Resuming thread Two
Waiting for threads to finish.
Two: 5
One: 5
Two: 4
One: 4
Two: 3
One: 3
Two: 2
One: 2
Two: 1
One: 1
Two exiting.
One exiting.
Main thread exiting.
The Thread class also defines a method called stop( ) that stops a thread. Its signature
is shown here:
final void stop( )
Once a thread has been stopped, it cannot be restarted using resume( ).
Suspending, Resuming, and Stopping Threads Using
Java 2
While the suspend( ), resume( ), and stop( ) methods defined by Thread seem to be a
perfectly reasonable and convenient approach to managing the execution of threads,
they must not be used for new Java programs. Here’s why. The suspend( ) method
of the Thread class is deprecated in Java 2. This was done because suspend( ) can
sometimes cause serious system failures. Assume that a thread has obtained locks on
critical data structures. If that thread is suspended at that point, those locks are not
relinquished. Other threads that may be waiting for those resources can be deadlocked.
The resume( ) method is also deprecated. It does not cause problems, but cannot be
used without the suspend( ) method as its counterpart.
The stop( ) method of the Thread class, too, is deprecated in Java 2. This was done
because this method can sometimes cause serious system failures. Assume that a thread
is writing to a critically important data structure and has completed only part of its
changes. If that thread is stopped at that point, that data structure might be left in a
corrupted state.
Because you can’t use the suspend( ), resume( ), or stop( ) methods in Java 2 to
control a thread, you might be thinking that no way exists to pause, restart, or terminate
a thread. But, fortunately, this is not true. Instead, a thread must be designed so that the
run( ) method periodically checks to determine whether that thread should suspend,
resume, or stop its own execution. Typically, this is accomplished by establishing a flag
variable that indicates the execution state of the thread. As long as this flag is set to
“running,” the run( ) method must continue to let the thread execute. If this variable is
set to “suspend,” the thread must pause. If it is set to “stop,” the thread must terminate.
Of course, a variety of ways exist in which to write such code, but the central theme will
be the same for all programs.
The following example illustrates how the wait( ) and notify( ) methods that are
inherited from Object can be used to control the execution of a thread. This example is
similar to the program in the previous section. However, the deprecated method calls
have been removed. Let us consider the operation of this program.
The NewThread class contains a boolean instance variable named suspendFlag,
which is used to control the execution of the thread. It is initialized to false by the
constructor. The run( ) method contains a synchronized statement block that checks
suspendFlag. If that variable is true, the wait( ) method is invoked to suspend the
execution of the thread. The mysuspend( ) method sets suspendFlag to true. The
308 J a v a ™ 2 : T h e C o m p l e t e R e f e r e n c e
myresume( ) method sets suspendFlag to false and invokes notify( ) to wake up the
thread. Finally, the main( ) method has been modified to invoke the mysuspend( ) and
myresume( ) methods.
// Suspending and resuming a thread for Java 2
class NewThread implements Runnable {
String name; // name of thread
Thread t;
boolean suspendFlag;
NewThread(String threadname) {
name = threadname;
t = new Thread(this, name);
System.out.println("New thread: " + t);
suspendFlag = false;
t.start(); // Start the thread
}
// This is the entry point for thread.
public void run() {
try {
for(int i = 15; i > 0; i--) {
System.out.println(name + ": " + i);
Thread.sleep(200);
synchronized(this) {
while(suspendFlag) {
wait();
}
}
}
} catch (InterruptedException e) {
System.out.println(name + " interrupted.");
}
System.out.println(name + " exiting.");
}
void mysuspend() {
suspendFlag = true;
}
synchronized void myresume() {
suspendFlag = false;

notify();
}
}
class SuspendResume {
public static void main(String args[]) {
NewThread ob1 = new NewThread("One");
NewThread ob2 = new NewThread("Two");
try {
Thread.sleep(1000);
ob1.mysuspend();
System.out.println("Suspending thread One");
Thread.sleep(1000);
ob1.myresume();
System.out.println("Resuming thread One");
ob2.mysuspend();
System.out.println("Suspending thread Two");
Thread.sleep(1000);
ob2.myresume();
System.out.println("Resuming thread Two");
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
// wait for threads to finish
try {
System.out.println("Waiting for threads to finish.");
ob1.t.join();
ob2.t.join();
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Main thread exiting.");
}
}
The output from this program is identical to that shown in the previous section.
Later in this book, you will see more examples that use the Java 2 mechanism of thread control. Although this mechanism isn’t as “clean” as the old way, nevertheless, it is the way required to ensure that run-time errors don’t occur. It is the approach that must be used for all new code. Using Multithreading
If you are like most programmers, having multithreaded support built into the language will be new to you. The key to utilizing this support effectively is to think concurrently rather than serially. For example, when you have two subsystems within a program that can execute concurrently, make them individual threads. With the careful use of multithreading, you can create very efficient programs. A word of caution is in order, however: If you create too many threads, you can actually degrade the performance of your program rather than enhance it. Remember, some overhead is associated with context switching. If you create too many threads, more CPU time will be spent changing contexts than executing your program!

















Lecture Notes –26
Topc: Applet Programming
All of the preceding examples in this book have been Java applications. However, applications constitute only one class of Java programs. Another type of program is the applet. As mentioned in Chapter 1, applets are small applications that are accessed on an Internet server, transported over the Internet, automatically installed, and run as part of a Web document. After an applet arrives on the client, it has limited access to resources, so that it can produce an arbitrary multimedia user interface and run complex computations without introducing the risk of viruses or breaching data integrity. Many of the issues connected with the creation and use of applets are found in Part
II, when the applet package is examined. However, the fundamentals connected to the creation of an applet are presented here, because applets are not structured in the same way as the programs that have been used thus far. As you will see, applets differ from applications in several key areas.
Let’s begin with the simple applet shown here:
import java.awt.*;
import java.applet.*;
public class SimpleApplet extends Applet {
public void paint(Graphics g) {
g.drawString("A Simple Applet", 20, 20);
}
}
This applet begins with two import statements. The first imports the Abstract Window Toolkit (AWT) classes. Applets interact with the user through the AWT, not through the console-based I/O classes. The AWT contains support for a window-based, graphical interface. As you might expect, the AWT is quite large and sophisticated, and a complete discussion of it consumes several chapters in Part II of this book. Fortunately, this simple applet makes very limited use of the AWT. The second import statement imports the applet package, which contains the class Applet. Every applet that you create must be a
subclass of Applet.
The next line in the program declares the class SimpleApplet. This class must be declared as public, because it will be accessed by code that is outside the program. Inside SimpleApplet, paint( ) is declared. This method is defined by the AWT and must be overridden by the applet. paint( ) is called each time that the applet must redisplay its output. This situation can occur for several reasons. For example, the window in which the applet is running can be overwritten by another window and then uncovered. Or, the applet window can be minimized and then restored. paint( ) is
also called when the applet begins execution. Whatever the cause, whenever the applet must redraw its output, paint( ) is called. The paint( ) method has one parameter of type Graphics. This parameter contains the graphics context, which describes the
graphics environment in which the applet is running. This context is used whenever output to the applet is required.
Inside paint( ) is a call to drawString( ), which is a member of the Graphics class. This method outputs a string beginning at the specified X,Y location. It has the following general form: void drawString(String message, int x, int y)
Here, message is the string to be output beginning at x,y. In a Java window, the upper-left corner is location 0,0. The call to drawString( ) in the applet causes the message “A Simple Applet” to be displayed beginning at location 20,20.
Notice that the applet does not have a main( ) method. Unlike Java programs, appletsdo not begin execution at main( ). In fact, most applets don’t even have a main( ) method.
Instead, an applet begins execution when the name of its class is passed to an applet viewer or to a network browser.

After you enter the source code for SimpleApplet, compile in the same way that you have been compiling programs. However, running SimpleApplet involves a different process. In fact, there are two ways in which you can run an applet: ¦ Executing the applet within a Java-compatible Web browser.
¦ Using an applet viewer, such as the standard SDK tool, appletviewer. An applet viewer executes your applet in a window. This is generally the fastest and easiest way to test your applet. Each of these methods is described next.
To execute an applet in a Web browser, you need to write a short HTML text file
that contains the appropriate APPLET tag. Here is the HTML file that executes
SimpleApplet:


The width and height statements specify the dimensions of the display area used by the applet. (The APPLET tag contains several other options that are examined more closely in Part II.) After you create this file, you can execute your browser and then load this file, which causes SimpleApplet to be executed. To execute SimpleApplet with an applet viewer, you may also execute the HTML
file shown earlier. For example, if the preceding HTML file is called RunApp.html, then the following command line will run SimpleApplet: C:\>appletviewer RunApp.html However, a more convenient method exists that you can use to speed up testing. Simply include a comment at the head of your Java source code file that contains the APPLET tag. By doing so, your code is documented with a prototype of the necessary HTML statements, and you can test your compiled applet merely by starting the applet viewer with your Java source code file. If you use this method, the SimpleApplet
source file looks like this:
import java.awt.*;
import java.applet.*;
/*


*/
public class SimpleApplet extends Applet {
public void paint(Graphics g) {
g.drawString("A Simple Applet", 20, 20);
}
}
In general, you can quickly iterate through applet development by using these
three steps:
1. Edit a Java source file.
2. Compile your program.
3. Execute the applet viewer, specifying the name of your applet’s source file. The applet viewer will encounter the APPLET tag within the comment and execute your applet. The window produced by SimpleApplet, as displayed by the applet viewer, is shown in the following illustration:
While the subject of applets is more fully discussed later in this book, here are the key points that you should remember now:
¦ Applets do not need a main( ) method.
¦ Applets must be run under an applet viewer or a Java-compatible browser.
¦ User I/O is not accomplished with Java’s stream I/O classes. Instead, applets
use the interface provided by the AWT.
The transient and volatile Modifiers
Java defines two interesting type modifiers: transient and volatile. These modifiers are used to handle somewhat specialized situations.
When an instance variable is declared as transient, then its value need not persist when an object is stored. For example:
class T {
transient int a; // will not persist

int b; // will persist
}
Here, if an object of type T is written to a persistent storage area, the contents of a would not be saved, but the contents of b would. The volatile modifier tells the compiler that the variable modified by volatile can be changed unexpectedly by other parts of your program. One of these situations
involves multithreaded programs. (You saw an example of this in Chapter 11.) In a multithreaded program, sometimes, two or more threads share the same instance variable. For efficiency considerations, each thread can keep its own, private copy of such a shared variable. The real (or master) copy of the variable is updated at various times, such as when a synchronized method is entered. While this approach works fine, it may be inefficient at times. In some cases, all that really matters is that the
master copy of a variable always reflects its current state. To ensure this, simply specify the variable as volatile, which tells the compiler that it must always use the master copy of a volatile variable (or, at least, always keep any private copies up to date with the master copy, and vice versa). Also, accesses to the master variable must be executed in the precise order in which they are executed on any private copy.
volatile in Java has, more or less, the same meaning that it has in C/C++/C#. Using instanceof Sometimes, knowing the type of an object during run time is useful. For example, you might have one thread of execution that generates various types of objects, and another thread that processes these objects. In this situation, it might be useful for the processing thread to know the type of each object when it receives it. Another situation in which knowledge of an object’s type at run time is important involves casting. In Java, an invalid cast causes a run-time error. Many invalid casts can be caught at compile time.
However, casts involving class hierarchies can produce invalid casts that can be detected only at run time. For example, a superclass called A can produce two subclasses, called B and C. Thus, casting a B object into type A or casting a C object into type A is legal, but casting a B object into type C (or vice versa) isn’t legal. Because an object of type A can refer to objects of either B or C, how can you know, at run time, what type of object is actually being referred to before attempting the cast to type C? It could be an object of type A, B, or C. If it is an object of type B, a run-time exception will be thrown. Java provides the run-time operator instanceof to answer this question.The instanceof operator has this general form: object instanceof typeTHEJAVALANGUAGE Here, object is an instance of a class, and type is a class type. If object is of the specified type or can be cast into the specified type, then the instanceof operator valuates to true. Otherwise, its result is false. Thus, instanceof is the means by which your program can obtain run-time type information about an object.The following program demonstrates instanceof: // Demonstrate instanceof operator.
class A {
int i, j;
}
class B {
int i, j;
}
class C extends A {
int k;
}
class D extends A {
int k;
}
class InstanceOf {
public static void main(String args[]) {
A a = new A();
B b = new B();
C c = new C();
D d = new D();
if(a instanceof A)
System.out.println("a is instance of A");
if(b instanceof B)
System.out.println("b is instance of B");
if(c instanceof C)
System.out.println("c is instance of C");
if(c instanceof A)
System.out.println("c can be cast to A");
if(a instanceof C)
System.out.println("a can be cast to C");
System.out.println();
// compare types of derived types
A ob;
ob = d; // A reference to d
System.out.println("ob now refers to d");
if(ob instanceof D)
System.out.println("ob is instance of D");
System.out.println();
ob = c; // A reference to c
System.out.println("ob now refers to c");
if(ob instanceof D)
System.out.println("ob can be cast to D");
else
System.out.println("ob cannot be cast to D");
if(ob instanceof A)
System.out.println("ob can be cast to A");
System.out.println();
// all objects can be cast to Object
if(a instanceof Object)
System.out.println("a may be cast to Object");
if(b instanceof Object)
System.out.println("b may be cast to Object");
if(c instanceof Object)
System.out.println("c may be cast to Object");
if(d instanceof Object)
System.out.println("d may be cast to Object");
}
}
The output from this program is shown here:
a is instance of A
b is instance of B
c is instance of C
c can be cast to A
ob now refers to d
ob is instance of D
ob now refers to c
ob cannot be cast to D
ob can be cast to A
a may be cast to Object
b may be cast to Object
c may be cast to Object
d may be cast to Object
The instanceof operator isn’t needed by most programs, because, generally, you know the type of bject with which you are working. However, it can be very useful when you’re writing generalized routines that operate on objects of a complex class hierarchy. Strictfp Java 2 added a new keyword to the Java language, called strictfp. With the creation of Java 2, the floating point computation model was relaxed slightly to make certain floating point computations faster for certain processors, such as the Pentium. Specifically, the new model does not require the truncation of certain intermediate values that occur during a computation. By modifying a class or a method with strictfp, you ensure that floating point calculations (and thus all truncations) take place precisely as they did in earlier versions of Java. The truncation affects only the exponent of ertain operations. When a class is modified by strictfp, all the methods in the class are also modified by strictfp automatically. For example, the following fragment tells Java to use the original loating point model for calculations in all methods defined within MyClass:
strictfp class MyClass { //...
Frankly, most programmers never need to use strictfp, because it affects only a very small class of problems. Native Methods Although it is rare, occasionally, you may want to call a subroutine that s written in a language other than Java. Typically, such a subroutine exists as executable code for he CPU and environment in which you are working—that is, native code. For example, you may want to call a native code subroutine to achieve faster execution time. Or, you may want to use a pecialized, third-party library, such as a statistical package.However, because Java programs are compiled to bytecode, which is then interpreted (or compiled on-the-fly) by the Java run-time System, it would seem impossible to call a native code subroutine from within your Java program. Fortunately, this conclusion is false. Java provides the native keyword, which is used to declare native code methods. Once declared, these methods can be called from inside your Java program just as you call any other Java method. To declare a native method, precede the method with the native modifier, but do not define any body for the method. For example:
public native int meth() ;After you declare a native method, you must write the native method and follow a rather complex series of steps to link it with your Java code. Most native methods are written in C. The mechanism used to integrate C code with a Java program is called the Java Native Interface (JNI). This methodology was created by Java 1.1 and then expanded and enhanced by Java 2. (Java 1.0 used a different approach, which is now completely outdated.) A detailed description of the JNI is beyond the scope of this book, but the following description provides sufficient information for most applications.
The precise steps that you need to follow will vary between different Java environments and versions. This also depends on the language that you are using to implement the native method. The following discussion assumes a Windows 95/98/XP/NT/2000 environment. The language used to implement the native method is C. The easiest way to understand the process is to work through an example. To
begin, enter the following short program, which uses a native method called test( ):
// A simple example that uses a native method.
public class NativeDemo {
int i;
public static void main(String args[]) {
NativeDemo ob = new NativeDemo();
ob.i = 10;
System.out.println("This is ob.i before the native method:" +
ob.i);
ob.test(); // call a native method
System.out.println("This is ob.i after the native method:" +
ob.i);
}
// declare native method
public native void test() ;
// load DLL that contains static method
static {
System.loadLibrary("NativeDemo");
}
}
Notice that the test( ) method is declared as native and has no body. This is the method
that we will implement in C shortly. Also notice the static block. As explained earlier in
this book, a static block is executed only once, when your program begins execution
(or, more precisely, when its class is first loaded). In this case, it is used to load the
dynamic link library that contains the native implementation of test( ). (You will see
how to create this library soon.)
The library is loaded by the loadLibrary( ) method, which is part of the System
class. This is its general form:
static void loadLibrary(String filename)
Here, filename is a string that specifies the name of the file that holds the library. For the
Windows environment, this file is assumed to have the .DLL extension.
After you enter the program, compile it to produce NativeDemo.class. Next, you
must use javah.exe to produce one file: NativeDemo.h. (javah.exe is included in the
SDK.) You will include NativeDemo.h in your implementation of test( ). To produce
NativeDemo.h, use the following command:
javah -jni NativeDemo
This command produces a header file called NativeDemo.h. This file must be included in
the C file that implements test( ). The output produced by this command is shown here:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class NativeDemo */
#ifndef _Included_NativeDemo
#define _Included_NativeDemo
#ifdef _ _cplusplus
extern "C" {
#endif
/*
* Class: NativeDemo
* Method: test
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_NativeDemo_test
(JNIEnv *, jobject);
#ifdef _ _cplusplus
}
#endif
#endif
Pay special attention to the following line, which defines the prototype for the
test( ) function that you will create:
JNIEXPORT void JNICALL Java_NativeDemo_test(JNIEnv *, jobject);
Notice that the name of the function is Java_NativeDemo_test( ). You must use this as the name of the native function that you implement. That is, instead of creating a C function called test( ), you will create one called Java_NativeDemo_test( ). The NativeDemo component of the prefix is added because it identifies the test( ) method as being part of the NativeDemo class. Remember, another class may define its own native test( ) method that is completely different from the one declared by NativeDemo. Including the class name in the prefix provides a way to differentiate between differing versions. As a general rule,native functions will be given a name whose prefix includes the name of the class in which they are declared.
After producing the necessary header file, you can write your implementation of test( ) and store it in a file named NativeDemo.c:
/* This file contains the C version of the
test() method.
*/
#include
#include "NativeDemo.h"
#include
JNIEXPORT void JNICALL Java_NativeDemo_test(JNIEnv *env, jobject obj)
{
jclass cls;
jfieldID fid;
jint i;
JAVA
LANGUAGE
printf("Starting the native method.\n");
cls = (*env)->GetObjectClass(env, obj);
fid = (*env)->GetFieldID(env, cls, "i", "I");
if(fid == 0) {
printf("Could not get field id.\n");
return;
}
i = (*env)->GetIntField(env, obj, fid);
printf("i = %d\n", i);
(*env)->SetIntField(env, obj, fid, 2*i);
printf("Ending the native method.\n");
}
Notice that this file includes jni.h, which contains interfacing information. This file is provided by your Java compiler. The header file NativeDemo.h was created by javah, earlier. In this function, the GetObjectClass( ) method is used to obtain a C structure that has information about the class NativeDemo. The GetFieldID( ) method returns a C
structure with information about the field named “i” for the class. GetIntField( ) retrieves the original value of that field. SetIntField( ) stores an updated value in that field. (See the file jni.h for additional methods that handle other types of data.) After creating NativeDemo.c, you must compile it and create a DLL. To do this by using the Microsoft C/C++ compiler, use the following command line. (You might
need to specifiy the path to jni.h and its subordinate file jni_md.h.) Cl /LD NativeDemo.c
This produces a file called NativeDemo.dll. Once this is done, you can execute the Java
program, which will produce the following output:
This is ob.i before the native method: 10
Starting the native method.
i = 10
Ending the native method.
This is ob.i after the native method: 20
The specifics surrounding the use of native are implementation- and environmentdependent.
Furthermore, the specific manner in which you interface to Java code is
subject to change. You must consult the documentation that accompanies your Java
development system for details on native methods.
The Life Cycle of an Applet
Here is the Simple applet.

The following is the source code for the Simple. The Simple applet displays a descriptive string whenever it encounters a major milestone in its life, such as when the user first visits the page the applet's on. The pages that follow use the Simple applet and build upon it to illustrate concepts that are common to many applets. If you find yourself baffled by the Java source code, you might want to go to Learning the Java Language to learn about the language. import java.applet.Applet;import java.awt.Graphics; //No need to extend JApplet, since we don't add any components;//we just paint.public class Simple extends Applet { StringBuffer buffer; public void init() { buffer = new StringBuffer(); addItem("initializing... "); } public void start() { addItem("starting... "); } public void stop() { addItem("stopping... "); } public void destroy() { addItem("preparing for unloading..."); } private void addItem(String newWord) { System.out.println(newWord); buffer.append(newWord); repaint(); } public void paint(Graphics g) { //Draw a Rectangle around the applet's display area. g.drawRect(0, 0, getWidth() - 1, getHeight() - 1); //Draw the current string inside the rectangle. g.drawString(buffer.toString(), 5, 15); }}
Note: In this example, we extend the Applet class, not the Swing JApplet class, as we do not need to add Swing components to this applet.
Loading the Applet
You should see "initializing... starting..." above, as the result of the applet being loaded. When an applet is loaded, here's what happens:
An instance of the applet's controlling class (an Applet subclass)is created.
The applet initializes itself.
The applet starts running.
Leaving and Returning to the Applet's Page
When the user leaves the page — for example, to go to another page — the browser stops the applet. When the user returns to the page, the browser starts the applet.
Browser note: Some browsers reload the applet when you return to its page. In at least one browser, a bug exists where an applet can initialize itself more than once without being reloaded.









Lecture Notes –27
Topc: Graphics Programming
T The AWT contains numerous classes and methods that allow you to create
and manage windows. A full description of the AWT would easily fill an entire book.
Therefore, it is not possible to describe in detail every method, instance variable, or class contained in the AWT. However, this and the following two chapters explain the techniques needed to effectively use the AWT when creating your own applets or stand-alone programs. From there, you will be able to explore other parts of the AWT on your own. In this chapter, you will learn how to create and manage windows, manage fonts, output text, and utilize graphics. Chapter 22 describes the various controls, such as scroll bars and push buttons, supported by the AWT. It also explains further aspects of Java’s event-handling mechanism. Chapter 23 examines the AWT’s imaging subsystem and animation. Although the main purpose of the AWT is to support applet windows, it can also be used to create stand-alone windows that run in a GUI environment, such as Windows. Most of the examples are contained in applets, so to run them, you need
to use an applet viewer or a Java-compatible Web browser. A few examples will demonstrate the creation of stand-alone, windowed programs. If you have not yet read Chapter 20, please do so now. It provides an overview of event handling, which is used by many of the examples in this chapter. AWT Classes The AWT classes are contained in the java.awt package. It is one of Java’s largest packages. Fortunately, because it is logically organized in a top-down, hierarchical
fashion, it is easier to understand and use than you might at first believe. Table 21-1 lists some of the many AWT classes.
Class Description
AWTEvent Encapsulates AWT events.
AWTEventMulticaster Dispatches events to multiple listeners.
BorderLayout The border layout manager. Border layouts use five
components: North, South, East, West, and Center.
Button Creates a push button control.
Class Description
Canvas A blank, semantics-free window.
CardLayout The card layout manager. Card layouts emulate
index cards. Only the one on top is showing.
Checkbox Creates a check box control.
CheckboxGroup Creates a group of check box controls.
CheckboxMenuItem Creates an on/off menu item.
Choice Creates a pop-up list.
Color Manages colors in a portable, platform-independent
fashion.
Component An abstract superclass for various AWT components.
Container A subclass of Component that can hold other
components.
Cursor Encapsulates a bitmapped cursor.
Dialog Creates a dialog window.
Dimension Specifies the dimensions of an object. The width is
stored in width, and the height is stored in height.
Event Encapsulates events.
EventQueue Queues events.
FileDialog Creates a window from which a file can be selected.
FlowLayout The flow layout manager. Flow layout positions
components left to right, top to bottom.
Font Encapsulates a type font.
FontMetrics Encapsulates various information related to a font.
This information helps you display text in a window.
Frame Creates a standard window that has a title bar, resize
corners, and a menu bar.
Graphics Encapsulates the graphics context. This context is
used by the various output methods to display
output in a window.
GraphicsDevice Describes a graphics device such as a screen or printer.

GraphicsEnvironment Describes the collection of available Font and
GraphicsDevice objects.
GridBagConstraints Defines various constraints relating to the
GridBagLayout class.
GridBagLayout The grid bag layout manager. Grid bag layout
displays components subject to the constraints
specified by GridBagConstraints.
GridLayout The grid layout manager. Grid layout displays
components in a two-dimensional grid.
Image Encapsulates graphical images.
Insets Encapsulates the borders of a container.
Label Creates a label that displays a string.
List Creates a list from which the user can choose. Similar
to the standard Windows list box.
MediaTracker Manages media objects.
Menu Creates a pull-down menu.
MenuBar Creates a menu bar.
MenuComponent An abstract class implemented by various menu
classes.
MenuItem Creates a menu item.
MenuShortcut Encapsulates a keyboard shortcut for a menu item.
Panel The simplest concrete subclass of Container.
Point Encapsulates a Cartesian coordinate pair, stored in x
and y.
Polygon Encapsulates a polygon.
PopupMenu Encapsulates a pop-up menu.
PrintJob An abstract class that represents a print job.
Rectangle Encapsulates a rectangle.
Robot Supports automated testing of AWT- based applications.
(Added by Java 2, vl.3)
Scrollbar Creates a scroll bar control.
Table 21-1. Some AWT Classes(continued)
Although the basic structure of the AWT has been the same since Java 1.0, some of the
original methods were deprecated and replaced by new ones when Java 1.1 was released. For backward-compatibility, Java 2 still supports all the original 1.0 methods. However,because these methods are not for use with new code, this book does not describe them.Window Fundamentals
The AWT defines windows according to a class hierarchy that adds functionality and specificity with each level. The two most common windows are those derived from Panel, which is used by applets, and those derived from Frame, which creates a
standard window. Much of the functionality of these windows is derived from their
parent classes. Thus, a description of the class hierarchies relating to these two classes
is fundamental to their understanding. Figure 21-1 shows the class hierarchy for Panel
and Frame. Let’s look at each of these classes now.
Component
At the top of the AWT hierarchy is the Component class. Component is an abstract
class that encapsulates all of the attributes of a visual component. All user interface
elements that are displayed on the screen and that interact with the user are subclasses
of Component. It defines over a hundred public methods that are responsible for
managing events, such as mouse and keyboard input, positioning and sizing the
window, and repainting. (You already used many of these methods when you created
applets in Chapters 19 and 20.) A Component object is responsible for remembering
the current foreground and background colors and the currently selected text font.
LIBRARY
Class Description
ScrollPane A container that provides horizontal and/or vertical
scroll bars for another component.
SystemColor Contains the colors of GUI widgets such as windows,
scroll bars, text, and others.
TextArea Creates a multiline edit control.
TextComponent A superclass for TextArea and TextField.
TextField Creates a single-line edit control.
Toolkit Abstract class implemented by the AWT.
Window Creates a window with no frame, no menu bar,
and no title.
Table 21-1. Some AWT Classes(continued)
Container
The Container class is a subclass of Component. It has additional methods that allow
other Component objects to be nested within it. Other Container objects can be stored
inside of a Container (since they are themselves instances of Component). This makes
for a multileveled containment system. A container is responsible for laying out (that
is, positioning) any components that it contains. It does this through the use of various
layout managers, which you will learn about in Chapter 22.
Panel
The Panel class is a concrete subclass of Container. It doesn’t add any new methods; it
simply implements Container. A Panel may be thought of as a recursively nestable,
concrete screen component. Panel is the superclass for Applet. When screen output is
directed to an applet, it is drawn on the surface of a Panel object. In essence, a Panel is
a window that does not contain a title bar, menu bar, or border. This is why you don’t
see these items when an applet is run inside a browser. When you run an applet using
an applet viewer, the applet viewer provides the title and border.
Other components can be added to a Panel object by its add( ) method (inherited
from Container). Once these components have been added, you can position and resize
them manually using the setLocation( ), setSize( ), or setBounds( ) methods defined by
Component.
Window
The Window class creates a top-level window. A top-level window is not contained
within any other object; it sits directly on the desktop. Generally, you won’t create
Window objects directly. Instead, you will use a subclass of Window called Frame,
described next.
Frame
Frame encapsulates what is commonly thought of as a “window.” It is a subclass of
Window and has a title bar, menu bar, borders, and resizing corners. If you create a
Frame object from within an applet, it will contain a warning message, such as “Java
Applet Window,” to the user that an applet window has been created. This message
warns users that the window they see was started by an applet and not by software
running on their computer. (An applet that could masquerade as a host-based
application could be used to obtain passwords and other sensitive information without
the user’s knowledge.) When a Frame window is created by a program rather than an
applet, a normal window is created.
Canvas
Although it is not part of the hierarchy for applet or frame windows, there is one other
type of window that you will find valuable: Canvas. Canvas encapsulates a blank
window upon which you can draw. You will see an example of Canvas later in this book.
Working with Frame Windows
After the applet, the type of window you will most often create is derived from Frame.
You will use it to create child windows within applets, and top-level or child windows
for applications. As mentioned, it creates a standard-style window.
Here are two of Frame’s constructors:
Frame( )
Frame(String title)
The first form creates a standard window that does not contain a title. The second form
creates a window with the title specified by title. Notice that you cannot specify the
dimensions of the window. Instead, you must set the size of the window after it
has been created.
There are several methods you will use when working with Frame windows. They
are examined here.
:
// Draw lines
import java.awt.*;
import java.applet.*;
/*


*/
public class Lines extends Applet {
public void paint(Graphics g) {
g.drawLine(0, 0, 100, 100);
g.drawLine(0, 100, 100, 0);
g.drawLine(40, 25, 250, 180);
g.drawLine(75, 90, 400, 400);
g.drawLine(20, 150, 400, 40);
g.drawLine(5, 290, 80, 19);
}
}
Sample output from this program is shown here:
Drawing Rectangles
The drawRect( ) and fillRect( ) methods display an outlined and filled rectangle,
shown here:
void drawRoundRect(int top, int left, int width, int height,
int xDiam, int yDiam)

void fillRoundRect(int top, int left, int width, int height,
int xDiam, int yDiam)
.
The following applet draws several rectangles:
// Draw rectangles
import java.awt.*;
import java.applet.*;
/*


*/
public class Rectangles extends Applet {
public void paint(Graphics g) {
g.drawRect(10, 10, 60, 50);
g.fillRect(100, 10, 60, 50);
g.drawRoundRect(190, 10, 60, 50, 15, 15);
g.fillRoundRect(70, 90, 140, 100, 30, 40);
}
}
Sample output from this program is shown here:
Drawing Ellipses and Circles
To draw an ellipse, use drawOval( ). To fill an ellipse, use fillOval( ). These methods
are shown here:
void drawOval(int top, int left, int width, int height)
void fillOval(int top, int left, int width, int height)
The ellipse is drawn within a bounding rectangle whose upper-left corner is specified
by top,left and whose width and height are specified by width and height.
To draw a circle, specify a square as the bounding rectangle.
The following program draws several ellipses:
// Draw Ellipses
import java.awt.*;
import java.applet.*;
/*


*/
public class Ellipses extends Applet {
public void paint(Graphics g) {
g.drawOval(10, 10, 50, 50);
g.fillOval(100, 10, 75, 50);
g.drawOval(190, 10, 90, 30);
g.fillOval(70, 90, 140, 100);
}
}
Sample output from this program is shown here:
Drawing Arcs
Arcs can be drawn with drawArc( ) and fillArc( ), shown here:
void drawArc(int top, int left, int width, int height, int startAngle,
int sweepAngle)
void fillArc(int top, int left, int width, int height, int startAngle,
int sweepAngle)
negative. Therefore, to draw an arc from twelve o’clock to six o’clock, the start angle
would be 90 and the sweep angle 180.
The following applet draws several arcs:
// Draw Arcs
import java.awt.*;
import java.applet.*;
/*


*/
public class Arcs extends Applet {
public void paint(Graphics g) {
g.drawArc(10, 40, 70, 70, 0, 75);
g.fillArc(100, 40, 70, 70, 0, 75);
g.drawArc(10, 100, 70, 80, 0, 175);
g.fillArc(100, 100, 70, 90, 0, 270);
g.drawArc(200, 80, 80, 80, 0, 180);
}
}
Sample output from this program is shown here:
Drawing Polygons
It is possible to draw arbitrarily shaped figures using drawPolygon( ) and fillPolygon( ),
shown here:
void drawPolygon(int x[ ], int y[ ], int numPoints)
void fillPolygon(int x[ ], int y[ ], int numPoints)
The polygon’s endpoints are specified by the coordinate pairs contained within the x and
y arrays. The number of points defined by x and y is specified by numPoints. There are
alternative forms of these methods in which the polygon is specified by a Polygon object.
The following applet draws an hourglass shape:
// Draw Polygon
import java.awt.*;
import java.applet.*;
/*


*/
public class HourGlass extends Applet {
public void paint(Graphics g) {
int xpoints[] = {30, 200, 30, 200, 30};
int ypoints[] = {30, 30, 200, 200, 30};
int num = 5;
g.drawPolygon(xpoints, ypoints, num);
}
}

Lecture Notes –28

1. Introduction to HTML


HTML Example

My First Heading


My first paragraph.



Try it yourself
What is HTML?
HTML is a language for describing web pages.
HTML stands for Hyper Text Markup Language
HTML is not a programming language, it is a markup language
A markup language is a set of markup tags
HTML uses markup tags to describe web pages
HTML Tags
HTML markup tags are usually called HTML tags
HTML tags are keywords surrounded by angle brackets like
HTML tags normally come in pairs like and
The first tag in a pair is the start tag, the second tag is the end tag
· Start and end tags are also called opening tags and closing tags.
HTML Documents - Web Pages
HTML documents describe web pages
HTML documents contain HTML tags and plain text
HTML documents are also called web pages
The purpose of a web browsers (like Internet Explorer) is to read HTML documents and display them as web pages. The browser does not display the HTML tags, but uses the tags to interpret the content of the page:

My First Heading

My first paragraph


Try it yourself
Example Explained
The text between and describes the web page
The text between and is the visible page content
The text between

and
What Do You Need?
You don't need any tools to learn HTML at W3Schools.
You don't need any HTML editor
You don't need a web server
You don't need a web site
Editing HTML
In this tutorial we use a plain text editor (like Notepad) to edit HTML. We believe this is the best way to learn HTML.
However, professional web developers often prefer HTML editors like FrontPage or Dreamweaver, instead of writing plain text.
Creating Your Own Test Web
If you just want to learn HTML, skip the rest of this chapter.
If you want to create a test web on your own computer, just copy the 3 files below to your desktop.
(Right click on each link, and select "save target as" or "save link as")
mainpage.htm
page1.htm
page2.htm
After you have copied the files, you can double-click on the file called "mainpage.htm" and see your first web site in action.
Use Your Test Web For Learning
We suggest you experiment with everything you learn at W3Schools by editing your web files with a text editor (like Notepad).
Note: If your first web site contains HTML markup tags you have not learned yet, don't panic. You will learn much more HTML in the next chapters.
HTM or HTML Extension?
When you save an HTML file, you can use either the .htm or the .html extension. We use .htm in our examples. It is a habit from the past, when the software only allowed three letters in file extensions.
With new software it is perfectly safe to use .html
Don't worry if the examples below use tags you have not learned yet. You will learn about it in the next chapters.
HTML Headings
HTML headings are defined with the

to

tags.

This is a heading

This is a heading

This is a heading


Try it yourself
HTML Paragraphs
HTML paragraphs are defined with the

tag.

This is a paragraph

This is another paragraph


Try it yourself
HTML Links
HTML links are defined with the tag.
This is a link
Note: The tag contains an attribute (href) to provide the link address.
Try it yourself
HTML Images
HTML images are defined with the tag.

HTML Headings
Headings are defined with the

to

tags.

defines the largest heading.

defines the smallest heading.

This is a heading

This is a heading

This is a heading


Try it yourself
Note: Browsers automatically adds an empty line before and after headings.
Headings Are Important
Use the HTML heading tags for headings only. Don't use headings to make something BIG or bold.
Search engines use your headings to index the structure and content of your web pages.
Since users may skim your pages by its headings, it is important to use headings to show the document structure.
H1 headings should be used as main headings, followed by H2 headings, and less important H3 headings, and so on.
HTML Comments
Comments can be inserted in the HTML code to make it more readable and understandable. Comments are ignored by the browser and not displayed.
Comments are written like this:

Try it yourself
Note: There is an exclamation point after the opening bracket, but not before the closing bracket.
HTML Tip - How to View HTML Source
Have you ever seen a Web page and wondered "Hey! How did they do that?"
To find out, click the VIEW option in your browser's toolbar and select SOURCE or PAGE SOURCE. This will open a window that shows you the HTML code of the page.
Examples From This Page
HeadingsThis example demonstrates the tags that display headings in an HTML document.
Hidden commentsThis example demonstrates how to insert a hidden comment in the HTML source code.
HTML Tag Reference
When you lookup W3Schools' tag reference, you will see additional information about tags and their attributes.
You will learn more about HTML tag attributes in the next chapters of this tutorial.
Tag
Description

Defines an HTML document

Defines the document's body

to


Defines header 1 to header 6

Defines a comment
HTML documents are divided into paragraphs.
HTML Paragraphs
Paragraphs are defined with the

tag.

This is a paragraph

This is another paragraph


Note: Browsers automatically adds an empty line before and after paragraphs.
Try it yourself
Don't Forget the End Tag
Most browsers will display HTML correctly even if you forget the end tag:

This is a paragraph

This is another paragraph
Try it yourself
The example above will work in most browsers, but don't rely on it. Forgetting the end tag can produce unexpected results or errors.
Note: Future version of HTML will not allow you to skip end tags.
HTML Line Breaks
Use the
tag if you want a line break (a new line) without starting a new paragraph:

This is
a para
graph with line breaks


Try it yourself
The
tag is an empty tag. It has no end tag like
.
You can read more about empty HTML tags in the next chapters of this tutorial.

or

In XHTML, XML, and future versions of HTML, tags with no end tags (closing tags) are not allowed.
Even if
works in all browsers, writing
instead is more future proof.
HTML Rules (Lines)
The
tag is used to create an horizontal rule (line).
Example:

This is a paragraph


This is a paragraph


This is a paragraph


Try it yourself
HTML Output - Useful Tips
You cannot be sure how HTML will be displayed. Large or small screens, and resized windows will create different results.
With HTML, you cannot change the output by adding extra spaces or extra lines in your HTML code.
The browser will remove extra spaces and extra lines when the page is displayed. Any number of lines count as one space, and any number of spaces count as one space.
Try it yourself (The example demonstrates some HTML formatting problems)
More paragraphsThis example demonstrates some of the default behaviors of paragraph elements.
HTML Tag Reference
When you lookup W3Schools' tag reference, you will see additional information about tags and their attributes.
You will learn more about HTML tag attributes in the next chapters of this tutorial.
Tag
Description


Defines a paragraph


Inserts a single line break



Defines a horizontal rule

This text is bold
This text is big
This text is italic
This is computer output
This is subscript and superscript
Try it yourself
HTML Formatting Tags
HTML uses tags like and for formatting output, like bold or italic text.
These HTML tags are called formatting tags.
Refer to the bottom of this page for a complete reference.
Examples - Try it Yourself
Text Formatting Tags
Tag
Description

Defines bold text

Defines big text

Defines emphasized text

Defines italic text

Defines small text

Defines strong text

Defines subscripted text

Defines superscripted text

Defines inserted text

Defines deleted text

Deprecated. Use instead

Deprecated. Use instead

Deprecated. Use styles instead
"Computer Output" Tags
Tag
Description

Defines computer code text

Defines keyboard text

Defines sample computer code

Defines teletype text

Defines a variable


Defines preformatted text

Deprecated. Use
 instead

<br />Deprecated. Use <pre> instead <br /><xmp> <br />Deprecated. Use <pre> instead <br />Citations, Quotations, and Definition Tags <br />Tag <br />Description <br /><a href="http://www.w3schools.com/tags/tag_abbr.asp"><abbr></a> <br />Defines an abbreviation <br /><a href="http://www.w3schools.com/tags/tag_acronym.asp"><acronym></a> <br />Defines an acronym <br /><a href="http://www.w3schools.com/tags/tag_address.asp"><address></a> <br />Defines an address element <br /><a href="http://www.w3schools.com/tags/tag_bdo.asp"><bdo></a> <br />Defines the text direction <br /><a href="http://www.w3schools.com/tags/tag_blockquote.asp"><blockquote></a> <br />Defines a long quotation <br /><a href="http://www.w3schools.com/tags/tag_q.asp"><q></a> <br />Defines a short quotation <br /><a href="http://www.w3schools.com/tags/tag_phrase_elements.asp"><cite></a> <br />Defines a citation <br /><a href="http://www.w3schools.com/tags/tag_phrase_elements.asp"><dfn></a> <br />Defines a definition term <br />HTML Elements <br />An HTML element is everything from the start tag to the end tag: <br />Start tag <br />Element content <br />End tag <br /><p> <br />This is a paragraph <br /></p> <br /><a href="default.htm"> <br />This is a link <br /></a> <br /> <br />HTML Element Syntax <br />An HTML element starts with a start tag <br />An HTML element ends with an end tag <br />The element content is everything between the start and end tag <br />Some HTML elements have empty content <br />Some HTML elements have a missing end tag <br />Note: The start tag can have additional information (attributes). See next chapter. <br />Nested HTML Elements <br />Most HTML elements can be nested (can contain other HTML elements). <br />Most HTML documents consist of nested HTML elements. <br />HTML Document Example <br /><html> <br /><body><p>This is my first paragraph</p></body> <br /></html> <br />The example above contains 3 HTML elements: <br /><p>This is my first paragraph</p> <br />The <p> element defines a paragraph in the HTML document:The element has a start tag <p> and an end tag </p>The element content is: This is my first paragraph <br /><body><p>This is my first paragraph</p></body> <br />The <body> element defines the body of the HTML documentThe element has a start tag <body> and an end tag </body>The element content is another element (a paragraph) <br /><html><body><p>This is my first paragraph</p></body></html> <br /> <br /> Lecture Notes &#8211;29 <br /> <br />Topic: 1. HTML Document; <br />The <html> element defines the whole HTML document <br />The element has a start tag <html> and an end tag </html>The element content is another element (the body) <br />Empty HTML Elements <br />HTML elements without content are called empty elements. Empty elements have no end tag. <br /><br /> is an empty element without a closing tag. <br />In XHTML, XML, and future versions of HTML, all elements must be closed. <br />Adding a slash to the start tag, like <br />, is the proper way of closing empty elements, accepted by HTML, XHTML and XML. <br />Even if <br /> works in all browsers, writing <br /> instead is more future proof. <br />HTML Tip - Lowercase Tags <br />HTML tags are not case sensitive: <p> means the same as <p>. Plenty of web sites use uppercase HTML tags in their pages. <br />W3Schools use lowercase tags because the World Wide Web Consortium (W3C) recommends lowercase in HTML 4, and demands lowercase tags in newer versions of (X)HTML. <br />HTML Attributes <br />HTML elements can have attributes <br />Attributes provide additional information about the element <br />Attributes are always specified in the start tag <br />Attribute Syntax <br />Attributes always come in name/value pairs like this: name="value". <br />Examples <br />border="1" <br />href="http://www.w3scchools.com" <br />bgcolor="yellow" <br />Attributes Example 1: <br /><table> defines an HTML table. (You will learn more about HTML tables later) <br /><table border="1"> <br />The border attribute defines a border type for the <table> element. <br />Attributes Example 2: <br /><a> defines an anchor (an HTML link). (You will learn more about HTML links later) <br /><a href="http://www.w3schools.com"> <br />The href attribute provides the link address for the <a> element. <br />Attributes Example 3: <br /><body> defines the body of an HTML document. <br /><body bgcolor="yellow"> <br />The bgcolor attribute defines the background color for the <body> element. <br />Note: bgcolor is a "dying" attribute, use styles instead (next chapter). <br />Always Quote Attribute Values <br />Attribute values should always be enclosed in quotes. Double style quotes are the most common, but single style quotes are also allowed. <br />In some rare situations, like when the attribute value itself contains quotes, it is necessary to use single quotes: <br />name='John "ShotGun" Nelson' <br />HTML Tip - Use Lowercase Attributes <br />Attribute names and attribute values are case-insensitive. <br />However, the World Wide Web Consortium (W3C) recommends lowercase attributes/attribute values in their HTML 4 recommendation <br />Newer versions of (X)HTML will demand lowercase attributes. <br />The HTML Style Attribute <br />The purpose of the style attribute is: <br />To provide a common way to style all HTML elements. <br />Styles was introduced with HTML 4, as the new and preferred way to style HTML elements. With HTML styles, styles can be added to HTML elements directly by using the style attribute, or indirectly by in separate style sheets (CSS files). <br />You can learn everything about styles and CSS in our CSS tutorial. <br />In our HTML tutorial we use the style attribute to introduce you to HTML styles. <br />HTML Style Examples <br />style="background-color:yellow" <br />style="font-size:10px" <br />style="font-family:Times" <br />style="text-align:center" <br />Deprecated Tags and Attributes <br />In HTML 4, some tags and attributes are defined as deprecated. Deprecated means that they will not be supported in future versions of HTML and XHTML. <br />The message is clear: Avoid the use of deprecated tags and attributes. <br />These tags and attributes should be avoided: <br />Tags <br />Description <br /><center> <br />Defines centered content <br /><span> and <basefont> <br />Defines HTML fonts <br /><s> and <strikeout> <br />Defines strikeout text <br /><u> <br />Defines underlined text <br /> <br /> <br />Attributes <br /> <br />align <br />Defines the alignment of text <br />bgcolor <br />Defines the background color <br />color <br />Defines the text color <br />For all the above: Use styles instead. <br />Style Examples: <br />Background Color <br /><body style="background-color:yellow"> <br />The style attribute defines a style for the <body> element. <br /><a href="http://www.w3schools.com/html/tryit.asp?filename=tryhtml_bodybgstyle" target="_blank">Try it yourself: Background color</a> <br />The new style attribute makes the "old" bgcolor attribute obsolete. <br /><a href="http://www.w3schools.com/html/tryit.asp?filename=tryhtml_bodybgcol" target="_blank">Try it yourself: Background the old way</a> <br />Font Family, Color and Size <br /><p style="font-family:courier new; color:red; font-size:20px"> <br />The style attribute defines a style for the <p> element. <br /><a href="http://www.w3schools.com/html/tryit.asp?filename=tryhtml_newfont" target="_blank">Try it yourself: Font Example</a> <br />The new style attribute makes the old <span> tag obsolete. <br /><a href="http://www.w3schools.com/html/tryit.asp?filename=tryhtml_font" target="_blank">Try it yourself: Fonts the old way</a> <br />Text Alignment <br /><h1 style="text-align:center"> <br />The style attribute defines a style for the <h1> element. <br /><a href="http://www.w3schools.com/html/tryit.asp?filename=tryhtml_headeralign" target="_blank">Try it yourself: Centered heading</a> <br />The new style attribute makes the old "align" attribute obsolete. <br /><a href="http://www.w3schools.com/html/tryit.asp?filename=tryhtml_header" target="_blank">Try it yourself: Centered heading the old way</a> <br />Examples <br /><a href="http://www.w3schools.com/html/tryit.asp?filename=tryhtml_links" target="_blank">HTML links</a>This example demonstrates how to create links in an HTML document. <br /><a href="http://www.w3schools.com/html/tryit.asp?filename=tryhtml_link_target" target="_blank">Open a link in a new browser window</a>This example demonstrates how to link to another page by opening a new window, so that the visitor does not have to leave your Web site. <br />(You can find more examples at the bottom of this page) <br />Hyperlinks, Anchors, and Links <br />In web terms, a hyperlink is a reference (an address) to a resource on the web. <br />Hyperlinks can can point to any resource on the web: an HTML page, an image, a sound file, a movie, etc. <br />An anchor is a term used to define a hyperlink destination inside a document. <br />The HTML anchor element <a>, is used to define both hyperlinks and anchors. <br />We will use the term HTML link when the <a> element points to a resource, and the term HTML anchor when the <a> elements defines an address inside a document.. <br />An HTML Link <br />Link syntax: <br /><a href="url">Link text</a> <br />The start tag contains attributes about the link. <br />The element content (Link text) defines the part to be displayed. <br />Note: The element content don't have to be a text. You can link from an image or any other HTML element. <br />The href Attribute <br />The href attribute defines the link "address". <br />This <a> element defines a link to W3Schools: <br /><a href="http://www.w3schools.com/">Visit W3Schools!</a> <br />The code above will display like this in a browser: <br /><a href="http://www.w3schools.com/">Visit W3Schools!</a> <br />The target Attribute <br />The target attribute defines where the linked document will be opened. <br />The code below will open the document in a new browser window: <br /><a href="http://www.w3schools.com/" target="_blank">Visit W3Schools!</a> <br /><a href="http://www.w3schools.com/html/tryit.asp?filename=tryhtml_link_target" target="_blank">Try it yourself</a> <br />The name Attribute <br />When the name attribute is used, the <a> element defines a named anchor inside a HTML document. <br />Named anchor are not displayed in any special way. They are invisible to the reader. <br />Named anchor syntax: <br /><a name="label">Any content</a> <br />The link syntax to a named anchor: <br /><a href="#label">Any content</a> <br />The # in the href attribute defines a link to a named anchor. <br />Example: <br />A named anchor inside an HTML document: <br /><a name="tips">Useful Tips Section</a> <br />A link to the Useful Tips Section from the same document: <br /><a href="#tips">Jump to the Useful Tips Section</a> <br />A link to the Useful Tips Section from another document: <br /><a href="http://www.w3schools.com/html_tutorial.htm#tips">Jump to the Useful Tips Section</a> <br /> <br />With HTML you can display images in a document. <br />Examples <br /><a href="http://www.w3schools.com/html/tryit.asp?filename=tryhtml_images" target="_blank">Insert images</a>This example demonstrates how to display images in your Web page. <br /><a href="http://www.w3schools.com/html/tryit.asp?filename=tryhtml_images2" target="_blank">Insert images from different locations</a>This example demonstrates how to display images from another folder or another server in your Web page. <br />(You can find more examples at the bottom of this page) <br />The Image Tag and the Src Attribute <br />In HTML, images are defined with the <img> tag. <br />The <img> tag is empty, which means that it contains attributes only and it has no closing tag. <br />To display an image on a page, you need to use the src attribute. Src stands for "source". The value of the src attribute is the URL of the image you want to display on your page. <br />The syntax of defining an image: <br /><img src="url" /> <br />The URL points to the location where the image is stored. An image named "boat.gif" located in the directory "images" on "www.w3schools.com" has the URL: http://www.w3schools.com/images/boat.gif. <br />The browser puts the image where the image tag occurs in the document. If you put an image tag between two paragraphs, the browser shows the first paragraph, then the image, and then the second paragraph. <br />The Alt Attribute <br />The alt attribute is used to define an "alternate text" for an image. The value of the alt attribute is an author-defined text: <br /><img alt="Big Boat" src="boat.gif" /> <br />The "alt" attribute tells the reader what he or she is missing on a page if the browser can't load images. The browser will then display the alternate text instead of the image. It is a good practice to include the "alt" attribute for each image on a page, to improve the display and usefulness of your document for people who have text-only browsers. <br />With HTML you can create tables. <br />Examples <br /><a href="http://www.w3schools.com/html/tryit.asp?filename=tryhtml_tables" target="_blank">Tables</a>This example demonstrates how to create tables in an HTML document. <br /><a href="http://www.w3schools.com/html/tryit.asp?filename=tryhtml_table_borders" target="_blank">Table borders</a>This example demonstrates different table borders. <br />(You can find more examples at the bottom of this page) <br />Tables <br />Tables are defined with the <table> tag. A table is divided into rows (with the <tr> tag), and each row is divided into data cells (with the <td> tag). The letters td stands for "table data," which is the content of a data cell. A data cell can contain text, images, lists, paragraphs, forms, horizontal rules, tables, etc. <br /><table border="1"><tr><td>row 1, cell 1</td><td>row 1, cell 2</td></tr><tr><td>row 2, cell 1</td><td>row 2, cell 2</td></tr></table> <br />How it looks in a browser: <br />row 1, cell 1 <br />row 1, cell 2 <br />row 2, cell 1 <br />row 2, cell 2 <br /> <br />Tables and the Border Attribute <br />If you do not specify a border attribute the table will be displayed without any borders. Sometimes this can be useful, but most of the time, you want the borders to show. <br />To display a table with borders, you will have to use the border attribute: <br /><table border="1"><tr><td>Row 1, cell 1</td><td>Row 1, cell 2</td></tr></table> <br /> <br />Headings in a Table <br />Headings in a table are defined with the <th> tag. <br /><table border="1"><tr><th>Heading</th><th>Another Heading</th></tr><tr><td>row 1, cell 1</td><td>row 1, cell 2</td></tr><tr><td>row 2, cell 1</td><td>row 2, cell 2</td></tr></table> <br />How it looks in a browser: <br />Heading <br />Another Heading <br />row 1, cell 1 <br />row 1, cell 2 <br />row 2, cell 1 <br />row 2, cell 2 <br /> <br />Empty Cells in a Table <br />Table cells with no content are not displayed very well in most browsers. <br /><table border="1"><tr><td>row 1, cell 1</td><td>row 1, cell 2</td></tr><tr><td>row 2, cell 1</td><td></td></tr></table> <br />How it looks in a browser: <br />row 1, cell 1 <br />row 1, cell 2 <br />row 2, cell 1 <br /> <br />Note that the borders around the empty table cell are missing (NB! Mozilla Firefox displays the border). <br />To avoid this, add a non-breaking space ( ) to empty data cells, to make the borders visible: <br /><table border="1"><tr><td>row 1, cell 1</td><td>row 1, cell 2</td></tr><tr><td>row 2, cell 1</td><td> </td></tr></table> <br />How it looks in a browser: <br />row 1, cell 1 <br />row 1, cell 2 <br />row 2, cell 1 <br /> <br /> <br /> <br /> <br />Table Tags <br />Tag <br />Description <br /><a href="http://www.w3schools.com/tags/tag_table.asp"><table></a> <br />Defines a table <br /><a href="http://www.w3schools.com/tags/tag_th.asp"><th></a> <br />Defines a table header <br /><a href="http://www.w3schools.com/tags/tag_tr.asp"><tr></a> <br />Defines a table row <br /><a href="http://www.w3schools.com/tags/tag_td.asp"><td></a> <br />Defines a table cell <br /><a href="http://www.w3schools.com/tags/tag_caption.asp"><caption></a> <br />Defines a table caption <br /><a href="http://www.w3schools.com/tags/tag_colgroup.asp"><colgroup></a> <br />Defines groups of table columns <br /><a href="http://www.w3schools.com/tags/tag_col.asp"><col></a> <br />Defines the attribute values for one or more columns in a table <br /><a href="http://www.w3schools.com/tags/tag_thead.asp"><thead></a> <br />Defines a table head <br /><a href="http://www.w3schools.com/tags/tag_tbody.asp"><tbody></a> <br />Defines a table body <br /><a href="http://www.w3schools.com/tags/tag_tfoot.asp"><tfoot></a> <br />Defines a table footer <br />HTML Lists <br /> <br />This is the first <br />This is the second <br />This is the third <br />Try-It-Yourself Examples <br /><a href="http://www.w3schools.com/html/tryit.asp?filename=tryhtml_lists4" target="_blank">Unordered list</a> <br /><a href="http://www.w3schools.com/html/tryit.asp?filename=tryhtml_lists" target="_blank">Ordered list</a> <br />(You can find more examples at the bottom of this page) <br />Unordered Lists <br />An unordered list is a list of items. The list items are marked with bullets (typically small black circles). <br />An unordered list starts with the <ul> tag. Each list item starts with the <li> tag. <br /><ul><li>Coffee</li><li>Milk</li></ul> <br />Here is how it looks in a browser: <br />Coffee <br />Milk <br />Inside a list item you can put paragraphs, line breaks, images, links, other lists, etc. <br />Ordered Lists <br />An ordered list is also a list of items. The list items are marked with numbers. <br />An ordered list starts with the <ol> tag. Each list item starts with the <li> tag. <br /><ol><li>Coffee</li><li>Milk</li></ol> <br />Here is how it looks in a browser: <br />Coffee <br />Milk <br />Inside a list item you can put paragraphs, line breaks, images, links, other lists, etc. <br />Definition Lists <br />A definition list is not a list of items. This is a list of terms and explanation of the terms. <br />A definition list starts with the <dl> tag. Each definition-list term starts with the <dt> tag. Each definition-list definition starts with the <dd> tag. <br /><dl><dt>Coffee</dt><dd>Black hot drink</dd><dt>Milk</dt><dd>White cold drink</dd></dl> <br />Here is how it looks in a browser: <br />Coffee <br />Black hot drink <br />Milk <br />White cold drink <br />Inside a definition-list definition (the <dd> tag) you can put paragraphs, line breaks, images, links, other lists, etc. <br />Forms <br />A form is an area that can contain form elements. <br />Form elements are elements that allow the user to enter information (like text fields, textarea fields, drop-down menus, radio buttons, checkboxes, etc.) in a form. <br />A form is defined with the <form> tag. <br /><form> <input> <input></form> <br /> <br />Input <br />The most used form tag is the <input> tag. The type of input is specified with the type attribute. The most commonly used input types are explained below. <br />Text Fields <br />Text fields are used when you want the user to type letters, numbers, etc. in a form. <br /><form>First name: <input type="text" name="firstname"><br />Last name: <input type="text" name="lastname"></form> <br /> <br />Radio Buttons <br />Radio Buttons are used when you want the user to select one of a limited number of choices. <br /><form><input type="radio" name="sex" value="male"> Male<br /><input type="radio" name="sex" value="female"> Female</form> <br />How it looks in a browser: <br />Male Female <br />Note that only one option can be chosen. <br />Checkboxes <br />Checkboxes are used when you want the user to select one or more options of a limited number of choices. <br /><form>I have a bike:<input type="checkbox" name="vehicle" value="Bike"><br />I have a car: <input type="checkbox" name="vehicle" value="Car"><br />I have an airplane: <input type="checkbox" name="vehicle" value="Airplane"></form> <br />How it looks in a browser: <br />I have a bike: I have a car: I have an airplane: <br />The Form's Action Attribute and the Submit Button <br />When the user clicks on the "Submit" button, the content of the form is sent to the server. The form's action attribute defines the name of the file to send the content to. The file defined in the action attribute usually does something with the received input. <br /><form name="input" action="html_form_submit.asp" method="get">Username: <input type="text" name="user"><input type="submit" value="Submit"></form> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br />Lecture Notes &#8211;30 <br /> <br />Topic: Introduction to DHTML <br /> <br />DHTML is the art of combining HTML, JavaScript, DOM, and CSS. <br />What you should already know <br />Before you continue you should have a basic understanding of the following: <br />HTML <br />JavaScript <br />CSS <br />If you want to study these subjects first, find the tutorials on our <a href="http://www.w3schools.com/default.asp" target="_top">Home Page</a>. <br />DHTML is Not a Language <br />DHTML stands for Dynamic HTML. <br />DHTML is NOT a language or a web standard. <br />DHTML is a TERM used to describe the technologies used to make web pages dynamic and interactive. <br />To most people DHTML means the combination of HTML, JavaScript, DOM, and CSS. <br />According to the World Wide Web Consortium (W3C):"Dynamic HTML is a term used by some vendors to describe the combination of HTML, style sheets and scripts that allows documents to be animated." <br />DHTML Technologies <br />Below is a listing of DHTML technologies. <br />HTML 4 <br />The W3C HTML 4 standard has rich support for dynamic content: <br />HTML supports JavaScript <br />HTML supports the Document Object Model (DOM) <br />HTML supports HTML Events <br />HTML supports Cascading Style Sheets (CSS) <br />DHTML is about using these features to create dynamic and interactive web pages. <br />JavaScript <br />JavaScript is the scripting standard for HTML. <br />DHTML is about using JavaScript to control, access and manipulate HTML elements. <br />You can read more about this in the next chapter of this tutorial. <br />HTML DOM <br />The HTML DOM is the W3C standard Document Object Model for HTML. <br />The HTML DOM defines a standard set of objects for HTML, and a standard way to access and manipulate them. <br />DHTML is about using the DOM to access and manipulate HTML elements. <br />You can read more about this in a later chapter of this tutorial. <br />HTML Events <br />The W3C HTML Event Model is a part of the HTML DOM. <br />It defines a standard way to handle HTML events. <br />DHTML is about creating web pages that reacts to (user)events. <br />You can read more about this in a later chapter of this tutorial. <br />CSS <br />CSS is the W3C standard style and layout model for HTML. <br />CSS allows web developers to control the style and layout of web pages. <br />HTML 4 allows dynamic changes to CSS. <br />DHTML is about using JavaScript and DOM to change the style and positioning of HTML elements. <br />You can read more about this in a later chapter of this tutorial <br />JavaScript can create dynamic HTML content: <br />Date: <br />document.write(Date()); <br />Fri Dec 26 12:04:44 2008 <br />JavaScript Alone <br />If you have studied JavaScript, you already know that the statement: <br />document.write() <br />can be used to display dynamic content to a web page. <br />Example <br />Using JavaScript to display the current date: <br /><html><body> <script type="text/javascript">document.write(Date());</script> </body></html> <br />JavaScript and the HTML DOM <br />With HTML 4, JavaScript can also be used to change the inner content and attributes of HTML elements dynamically. <br />To change the content of an HTML element use: <br />document.getElementById(id).innerHTML=new HTML <br />To change the attribute of an HTML element use: <br />document.getElementById(id).attribute=new value <br />You will learn more about JavaScript and the HTML DOM in the next chapter of this tutorial. <br />JavaScript and HTML Events <br />New to HTML 4 is the ability to let HTML events trigger actions in the browser, like starting a JavaScript when a user clicks on an HTML element. <br />To execute code when a user clicks on an element, use the following event attribute: <br />onclick=JavaScript <br />You will learn more about JavaScript and HTML Events in a later chapter. <br />JavaScript and CSS <br />With HTML 4, JavaScript can also be used to change the style of HTML elements. <br />To change the style of an HTML element use: <br />document.getElementById(id).style.property=new style <br />You will learn more about JavaScript and CSS in a later chapter of this tutorial. <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br />Lecture Notes &#8211;31 <br /> <br />Topic: DHTML - HTML Document Object Model (DOM) <br /> <br />The DOM presents HTML as a tree-structure (a node tree), with elements, attributes, and text: <br />Examples <br /><a href="http://www.w3schools.com/dhtml/tryit.asp?filename=trydhtml_dom_innertext" target="_blank">innerHTML</a>How to access and change the innerHTML of an element. <br /><a href="http://www.w3schools.com/dhtml/tryit.asp?filename=trydhtml_dom_image" target="_blank">Attribute change</a>How to access an image element and change the "src" attribute. <br />What is the HTML DOM? <br />The HTML DOM is: <br />A standard object model for HTML <br />A standard programming interface for HTML <br />Platform- and language-independent <br />A W3C standard <br />The HTML DOM defines the objects and properties of all HTML elements, and the methods (interface) to access them. <br />In other words: <br />The HTML DOM is a standard for how to get, change, add, or delete HTML elements. <br />Using the HTML DOM to Change an HTML Element <br />The HTML DOM can be used to change the content of an HTML element: <br /><html><body> <h1 id="header">Old Header</h1> <script type="text/javascript">document.getElementById("header").innerHTML="New Header";</script> </body></html> <br />HTML events can trigger actions in the browser, like starting a JavaScript when a user clicks on an element. <br />Example <br />A header changes when the user clicks it: <br /><h1 onclick="this.innerHTML='Ooops!'">Click on this text</h1> <br /><a href="http://www.w3schools.com/dhtml/tryit.asp?filename=trydhtml_event_onclick2" target="_blank">Try it yourself</a> <br />You can also add a script in the head section of the page and then call the function from the event handler: <br /><html><head><script type="text/javascript">function changetext(id){id.innerHTML="Ooops!";}</script></head><body><h1 onclick="changetext(this)">Click on this text</h1></body></html> <br /> <br />HTML 4.0 Event Handlers <br />Event <br />Occurs when... <br />onabort <br />a user aborts page loading <br />onblur <br />a user leaves an object <br />onchange <br />a user changes the value of an object <br />onclick <br />a user clicks on an object <br />ondblclick <br />a user double-clicks on an object <br />onfocus <br />a user makes an object active <br />onkeydown <br />a keyboard key is on its way down <br />onkeypress <br />a keyboard key is pressed <br />onkeyup <br />a keyboard key is released <br />onload <br />a page is finished loading <br />onmousedown <br />a user presses a mouse-button <br />onmousemove <br />a cursor moves on an object <br />onmouseover <br />a cursor moves over an object <br />onmouseout <br />a cursor moves off an object <br />onmouseup <br />a user releases a mouse-button <br />onreset <br />a user resets a form <br />onselect <br />a user selects content on a page <br />onsubmit <br />a user submits a form <br />onunload <br />a user closes a page <br />HTML events can trigger actions in the browser, like starting a JavaScript when a user clicks on an element. <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br />Lecture Notes &#8211;32 <br />Topc: Introduction to XML. <br />The Extensible Markup Language (XML) is a general-purpose specification for creating custom <a href="http://en.wikipedia.org/wiki/Markup_language" title="Markup language">markup languages</a>.<a href="http://en.wikipedia.org/wiki/XML#cite_note-0#cite_note-0" title="">[1]</a> It is classified as an <a href="http://en.wikipedia.org/wiki/Extensible_language" title="Extensible language">extensible language</a>, because it allows the user to define the mark-up elements. XML's purpose is to aid information systems in sharing structured data, especially via the <a href="http://en.wikipedia.org/wiki/Internet" title="Internet">Internet</a>, <a href="http://en.wikipedia.org/wiki/XML#cite_note-XmlOriginsGoals-1#cite_note-XmlOriginsGoals-1" title="">[2]</a> to encode documents, and to <a href="http://en.wikipedia.org/wiki/Serialization" title="Serialization">serialize</a> data; in the last context, it compares with text-based serialization languages such as <a href="http://en.wikipedia.org/wiki/JSON" title="JSON">JSON</a> and <a href="http://en.wikipedia.org/wiki/YAML" title="YAML">YAML</a>. <a href="http://en.wikipedia.org/wiki/XML#cite_note-2#cite_note-2" title="">[3]</a> <br />XML began as a simplified subset of the <a href="http://en.wikipedia.org/wiki/Standard_Generalized_Markup_Language" title="Standard Generalized Markup Language">Standard Generalized Markup Language</a> (SGML), meant to be readable by people via <a href="http://en.wikipedia.org/wiki/Semantics" title="Semantics">semantic</a> constraints; application languages can be implemented in XML. These include <a href="http://en.wikipedia.org/wiki/XHTML" title="XHTML">XHTML</a>,<a href="http://en.wikipedia.org/wiki/XML#cite_note-3#cite_note-3" title="">[4]</a> <a href="http://en.wikipedia.org/wiki/RSS_(file_format)" title="RSS (file format)">RSS</a>, <a href="http://en.wikipedia.org/wiki/MathML" title="MathML">MathML</a>, <a href="http://en.wikipedia.org/wiki/GraphML" title="GraphML">GraphML</a>, <a href="http://en.wikipedia.org/wiki/Scalable_Vector_Graphics" title="Scalable Vector Graphics">Scalable Vector Graphics</a>, <a href="http://en.wikipedia.org/wiki/MusicXML" title="MusicXML">MusicXML</a>, and others. Moreover, XML is sometimes used as the <a href="http://en.wikipedia.org/wiki/Specification_language" title="Specification language">specification language</a> for such application languages. <br />XML is <a href="http://en.wikipedia.org/wiki/W3C_recommendation" title="W3C recommendation">recommended</a> by the <a href="http://en.wikipedia.org/wiki/World_Wide_Web_Consortium" title="World Wide Web Consortium">World Wide Web Consortium</a> (W3C). It is a fee-free <a href="http://en.wikipedia.org/wiki/Open_standard" title="Open standard">open standard</a>. The recommendation specifies <a href="http://en.wikipedia.org/wiki/Lexical_grammar" title="Lexical grammar">lexical grammar</a> and <a href="http://en.wikipedia.org/wiki/Parsing" title="Parsing">parsing</a> requirements <br />Well-formed and Valid XML documents <br />An XML document has two correctness levels: <br />Well-formed. A well-formed document conforms to the XML syntax rules; e.g. if a start-tag (< >) appears without a corresponding end-tag (</>), it is not well-formed. A document not well-formed is not in XML; a conforming parser is disallowed from processing it. <br />Valid. A valid document additionally conforms to semantic rules, either user-defined or in an <a href="http://en.wikipedia.org/wiki/XML_schema" title="XML schema">XML schema</a>, especially <a href="http://en.wikipedia.org/wiki/Document_Type_Definition" title="Document Type Definition">DTD</a>; e.g. if a document contains an undefined element, then it is not valid; a validating parser is disallowed from processing it. <br /><a name="Well-formed_documents:_XML_syntax"></a>[<a href="http://en.wikipedia.org/w/index.php?title=XML&amp;action=edit&amp;section=2" title="Edit section: Well-formed documents: XML syntax">edit</a>] Well-formed documents: XML syntax <br />If only a <a href="http://en.wikipedia.org/wiki/Well-formedness" title="Well-formedness">well-formed element</a> is required, XML is a generic framework for storing any amount of text or any data whose structure can be represented as a <a href="http://en.wikipedia.org/wiki/Tree_(data_structure)" title="Tree (data structure)">tree</a>. The only indispensable syntactical requirement is that the document has exactly one root element (aka the document element), i.e. the text must be enclosed between a root start-tag and a corresponding end-tag, e.g. a "well-formed" XML document:<book>This is a book... </book> <br />The root element can be preceded by an optional XML declaration element stating what XML version is in use (normally 1.0); it might also contain <a href="http://en.wikipedia.org/wiki/Character_encoding" title="Character encoding">character encoding</a> and external dependencies information.<!--xml version="1.0" encoding="UTF-8"?--> <br />The specification requires that <a href="http://en.wikipedia.org/wiki/Data_Processor" title="Data Processor">processors</a> of XML support the pan-<a href="http://en.wikipedia.org/wiki/Unicode" title="Unicode">Unicode</a> <a href="http://en.wikipedia.org/wiki/Character_encoding" title="Character encoding">character encodings</a> <a href="http://en.wikipedia.org/wiki/UTF-8" title="UTF-8">UTF-8</a> and <a href="http://en.wikipedia.org/wiki/UTF-16/UCS-2" title="UTF-16/UCS-2">UTF-16</a> (<a href="http://en.wikipedia.org/wiki/UTF-32/UCS-4" title="UTF-32/UCS-4">UTF-32</a> is not mandatory). The use of more limited encodings, i.e. those based on <a href="http://en.wikipedia.org/wiki/ISO/IEC_8859" title="ISO/IEC 8859">ISO/IEC 8859</a>, is acknowledged, widely used, and supported. <br />Comments can be placed anywhere in the tree, including in the text if the content of the element is text or #PCDATA. <br />XML comments start with <!-- and end with -->. Two consecutive dashes (--) may not appear anywhere in the text of the comment.<!-- This is a comment. --> <br />In any meaningful application, additional markup is used to structure the contents of the XML document. The text enclosed by the root tags may contain an arbitrary number of XML elements. The basic syntax for one element is:<element_name attribute_name="attribute_value">Element Content</element_name> <br />The two instances of &#187;element_name&#171; are referred to as the start-tag and end-tag, respectively. Here, &#187;Element Content&#171; is some text which may again contain XML elements. So, a generic XML document contains a <a href="http://en.wikipedia.org/wiki/Tree_(data_structure)" title="Tree (data structure)">tree-based data structure</a>. Here is an example of a structured XML document: <recipe name="bread" prep_time="5 mins" cook_time="3 hours"> <title>Basic bread</title> <ingredient amount="8" unit="dL">Flour</ingredient> <ingredient amount="10" unit="grams">Yeast</ingredient> <ingredient amount="4" unit="dL" state="warm">Water</ingredient> <ingredient amount="1" unit="teaspoon">Salt</ingredient> <instructions> <step>Mix all ingredients together.</step> <step>Knead thoroughly.</step> <step>Cover with a cloth, and leave for one hour in warm room.</step> <step>Knead again.</step> <step>Place in a bread baking tin.</step> <step>Cover with a cloth, and leave for one hour in warm room.</step> <step>Bake in the oven at 180(degrees)C for 30 minutes.</step> </instructions> </recipe> <br />Attribute values must always be quoted, using single or double quotes, and each attribute name may appear only once in any single element. <br />XML requires that elements be properly nested&#8212;elements may never overlap, and so must be closed in the order opposite to which they are opened. For example, this fragment of code below cannot be part of a well-formed XML document because the title and author elements are closed in the wrong order:<!-- WRONG! NOT WELL-FORMED XML! --><title>Book on Logic<author>Aristotle</title></author> <br />One way of writing the same information in a way which could be incorporated into a well-formed XML document is as follows:<!-- Correct: well-formed XML fragment. --><title>Book on Logic</title> <author>Aristotle</author> <br />XML provides special syntax for representing an element with empty content. Instead of writing a start-tag followed immediately by an end-tag, a document may contain an empty-element tag. An empty-element tag resembles a start-tag but contains a slash just before the closing angle bracket. The following three examples are equivalent in XML:<foo></foo><foo><foo/> <br />An empty-element may contain attributes:<info author="John Smith" genre="science-fiction" date="2009-Jan-01"> <br /><a name="Entity_references"></a>[<a href="http://en.wikipedia.org/w/index.php?title=XML&amp;action=edit&amp;section=3" title="Edit section: Entity references">edit</a>] Entity references <br />An <a href="http://en.wikipedia.org/wiki/SGML_entity" title="SGML entity">entity</a> in XML is a named body of data, usually text. Entities are often used to represent single characters that cannot easily be entered on the keyboard; they are also used to represent pieces of standard ("boilerplate") text that occur in many documents, especially if there is a need to allow such text to be changed in one place only. <br />Special characters can be represented either using <a href="http://en.wikipedia.org/wiki/SGML_entity" title="SGML entity">entity</a> references, or by means of <a href="http://en.wikipedia.org/wiki/Numeric_character_reference" title="Numeric character reference">numeric character references</a>. An example of a numeric character reference is "&#x20AC;", which refers to the <a href="http://en.wikipedia.org/wiki/Euro_symbol" title="Euro symbol">Euro symbol</a> by means of its <a href="http://en.wikipedia.org/wiki/Unicode" title="Unicode">Unicode</a> codepoint in <a href="http://en.wikipedia.org/wiki/Hexadecimal" title="Hexadecimal">hexadecimal</a>. <br />An entity reference is a <a href="http://en.wikipedia.org/wiki/Placeholder" title="Placeholder">placeholder</a> that represents that entity. It consists of the entity's name preceded by an <a href="http://en.wikipedia.org/wiki/Ampersand" title="Ampersand">ampersand</a> ("&amp;") and followed by a <a href="http://en.wikipedia.org/wiki/Semicolon" title="Semicolon">semicolon</a> (";"). XML has five <a href="http://en.wikipedia.org/wiki/Predeclared" title="Predeclared">predeclared</a> entities: <br />&amp; (&amp; or "ampersand") <br />&lt; (< or "less than") <br />&gt; (> or "greater than") <br />&apos; (' or "apostrophe") <br />&quot; (" or "quotation mark") <br />Here is an example using a predeclared XML entity to represent the ampersand in the name "AT&amp;T":<company_name>AT&amp;T</company_name> <br />Additional entities (beyond the predefined ones) can be declared in the document's <a href="http://en.wikipedia.org/wiki/XML#DTD" title="XML">Document Type Definition (DTD)</a>. A basic example of doing so in a minimal internal DTD follows. Declared entities can describe single characters or pieces of text, and can reference each other.<!--xml version="1.0" encoding="UTF-8"?--><!--DOCTYPE example [ <!ENTITY copy "&#xA9;"--> <!--ENTITY copyright-notice "Copyright &copy; 2006, XYZ Enterprises"-->]><example> &amp;copyright-notice;</example> <br />When viewed in a suitable browser, the XML document above appears as: <br />Copyright &#169; 2006, XYZ Enterprises <br /><a name="Numeric_character_references"></a>[<a href="http://en.wikipedia.org/w/index.php?title=XML&amp;action=edit&amp;section=4" title="Edit section: Numeric character references">edit</a>] Numeric character references <br />Numeric character references look like entity references, but instead of a name, they contain the "<a href="http://en.wikipedia.org/wiki/Number_sign" title="Number sign">#</a>" character followed by a number. The number (in decimal or "x"-prefixed <a href="http://en.wikipedia.org/wiki/Hexadecimal" title="Hexadecimal">hexadecimal</a>) represents a Unicode code point. Unlike entity references, they are neither predeclared nor do they need to be declared in the document's DTD. They have typically been used to represent characters that are not easily encodable, such as an <a href="http://en.wikipedia.org/wiki/Arabic_language" title="Arabic language">Arabic</a> character in a document produced on a European computer. The ampersand in the "AT&amp;T" example could also be <a href="http://en.wikipedia.org/wiki/Escape_character" title="Escape character">escaped</a> like this (decimal 38 and hexadecimal 26 both represent the Unicode code point for the "&amp;" character):<company_name>AT&#38;T</company_name><company_name>AT&#x26;T</company_name> <br />Similarly, in the previous example, notice that "&#xA9;" is used to generate the &#8220;&#169;&#8221; symbol. <br />See also <a href="http://en.wikipedia.org/wiki/Numeric_character_reference" title="Numeric character reference">numeric character references</a>. <br /><a name="Well-formed_documents"></a>[<a href="http://en.wikipedia.org/w/index.php?title=XML&amp;action=edit&amp;section=5" title="Edit section: Well-formed documents">edit</a>] Well-formed documents <br />In XML, a <a href="http://en.wikipedia.org/wiki/Well-formed_element" title="Well-formed element">well-formed</a> document must conform to the following rules, among others: <br />Non-empty elements are <a href="http://en.wikipedia.org/wiki/Delimiter" title="Delimiter">delimited</a> by both a start-tag and an end-tag. <br />Empty elements may be marked with an empty-element (self-closing) tag, such as <iamempty>. This is equal to <iamempty></iamempty>. <br />All attribute values are quoted with either single (') or double (") quotes. Single quotes close a single quote and double quotes close a double quote. <br />To include a double quote inside an attribute value that is double quoted, or a single quote inside an attribute value that is single quoted, escape the inner quote mark using a <a href="http://en.wikipedia.org/wiki/Character_entity_reference" title="Character entity reference">Character_entity_reference</a>. This is necessary when an attribute value must contain both types (single and double quotes) or when you do not have control over the type of quotation a particular XML editor uses for wrapping attribute values. These character entity references are predefined in XML and do not need to be declared even when using a DTD or Schema: &quot; and &apos;. You may also use the numeric character entity references (hex) &#x0022; and &#x0027; or their equivalent decimal notations &#0034; and &#0039;. <br />Tags may be nested but must not overlap. Each non-root element must be completely contained in another element. <br />The document complies with its declared character encoding. The encoding may be declared or implied externally, such as in "Content-Type" headers when a document is transported via <a href="http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol" title="Hypertext Transfer Protocol">HTTP</a>, or internally, using explicit markup at the very beginning of the document. When no such declaration exists, a Unicode encoding is assumed, as defined by a Unicode <a href="http://en.wikipedia.org/wiki/Byte-order_mark" title="Byte-order mark">Byte Order Mark</a> before the document's first character. If the mark does not exist, UTF-8 encoding is assumed. <br />Element names are case-sensitive. For example, the following is a well-formed matching pair: <br /><step> ... </step> <br />whereas these are not <br /><step> ... </step> <br /><step> ... </step> <br />By carefully choosing the names of the XML elements one may convey the meaning of the data in the <a href="http://en.wikipedia.org/wiki/Markup_language" title="Markup language">markup</a>. This increases human readability while retaining the rigor needed for software parsing. <br />Choosing meaningful names implies the <a href="http://en.wikipedia.org/wiki/Semantics" title="Semantics">semantics</a> of elements and attributes to a human reader without reference to external documentation. However, this can lead to verbosity, which complicates <a href="http://en.wikipedia.org/wiki/Authoring" title="Authoring">authoring</a> and increases <a href="http://en.wikipedia.org/wiki/File_size" title="File size">file size</a>. <br /><a name="Automatic_verification"></a>[<a href="http://en.wikipedia.org/w/index.php?title=XML&amp;action=edit&amp;section=6" title="Edit section: Automatic verification">edit</a>] Automatic verification <br />It is relatively simple to verify that a document is well-formed or validated XML, because the rules of well-formedness and validation of XML are designed for portability of tools. The idea is that any tool designed to work with XML files will be able to work with XML files written in any XML language (or XML application). Here are some examples of ways to verify XML documents: <br />load it into an XML-capable browser, such as <a href="http://en.wikipedia.org/wiki/Mozilla_Firefox" title="Mozilla Firefox">Firefox</a> or <a href="http://en.wikipedia.org/wiki/Internet_Explorer" title="Internet Explorer">Internet Explorer</a> <br />use a tool like xmlwf (usually bundled with <a href="http://en.wikipedia.org/wiki/Expat_(XML)" title="Expat (XML)">expat</a>) <br />parse the document, for instance in <a href="http://en.wikipedia.org/wiki/Ruby_programming_language" title="Ruby programming language">Ruby</a>: irb> require "rexml/document"irb> include REXMLirb> doc = Document.new(File.new("test.xml")).root <br /><a name="Valid_documents:_XML_semantics"></a>[<a href="http://en.wikipedia.org/w/index.php?title=XML&amp;action=edit&amp;section=7" title="Edit section: Valid documents: XML semantics">edit</a>] Valid documents: XML semantics <br />By leaving the names, allowable hierarchy, and meanings of the elements and attributes open and definable by a customizable <a href="http://en.wikipedia.org/wiki/XML_schema" title="XML schema">schema</a> or <a href="http://en.wikipedia.org/wiki/Document_Type_Definition" title="Document Type Definition">DTD</a>, XML provides a syntactic foundation for the creation of purpose-specific, XML-based markup languages. The general syntax of such languages is rigid &#8212; documents must adhere to the general rules of XML, ensuring that all XML-aware software can at least read and understand the relative arrangement of information within them. The schema merely supplements the syntax rules with a set of constraints. Schemas typically restrict element and attribute names and their allowable containment hierarchies, such as only allowing an element named 'birthday' to contain one element named 'month' and one element named 'day', each of which has to contain only character data. The constraints in a schema may also include <a href="http://en.wikipedia.org/wiki/Data_type" title="Data type">data type</a> assignments that affect how information is processed; for example, the 'month' element's character data may be defined as being a month according to a particular schema language's conventions, perhaps meaning that it must not only be formatted a certain way, but also must not be processed as if it were some other type of data. <br />An XML document that complies with a particular schema/DTD, in addition to being well-formed, is said to be valid. <br />An XML schema is a description of a type of XML document, typically expressed in terms of <a href="http://en.wikipedia.org/wiki/Constraints" title="Constraints">constraints</a> on the structure and content of documents of that type, above and beyond the basic constraints imposed by XML itself. A number of standard and proprietary XML schema languages have emerged for the purpose of formally expressing such schemas, and some of these languages are XML-based, themselves. <br />Before the advent of generalised data description languages such as SGML and XML, software designers had to define special <a href="http://en.wikipedia.org/wiki/File_format" title="File format">file formats</a> or small languages to share data between programs. This required writing detailed <a href="http://en.wikipedia.org/wiki/Specifications" title="Specifications">specifications</a> and special-purpose parsers and <a href="http://en.wikipedia.org/wiki/Writer" title="Writer">writers</a>. <br />XML's regular structure and strict parsing rules allow software designers to leave parsing to standard tools, and since XML provides a general, <a href="http://en.wikipedia.org/wiki/Data_model" title="Data model">data model</a>-oriented framework for the development of application-specific languages, software designers need only concentrate on the development of rules for their data, at relatively high levels of abstraction. <br />Well-tested tools exist to <a href="http://en.wikipedia.org/wiki/Validate" title="Validate">validate</a> an XML document "against" a schema: the tool automatically <a href="http://en.wikipedia.org/wiki/Verify" title="Verify">verifies</a> whether the document conforms to constraints expressed in the schema. Some of these validation tools are included in XML parsers, and some are packaged separately. <br />Other usages of schemas exist: XML editors, for instance, can use schemas to support the editing process (by suggesting valid elements and attributes names, etc). <br /><a name="DTD"></a>[<a href="http://en.wikipedia.org/w/index.php?title=XML&amp;action=edit&amp;section=8" title="Edit section: DTD">edit</a>] DTD <br />Main article: <a href="http://en.wikipedia.org/wiki/Document_Type_Definition" title="Document Type Definition">Document Type Definition</a> <br />The oldest schema format for XML is the <a href="http://en.wikipedia.org/wiki/Document_Type_Definition" title="Document Type Definition">Document Type Definition</a> (DTD), inherited from SGML. While DTD support is ubiquitous due to its inclusion in the XML 1.0 standard, it is seen as limited for the following reasons: <br />It has no support for newer <a href="http://en.wikipedia.org/wiki/Feature" title="Feature">features</a> of XML, most importantly <a href="http://en.wikipedia.org/wiki/XML_Namespace" title="XML Namespace">namespaces</a>. <br />It lacks expressiveness. Certain formal aspects of an XML document cannot be captured in a DTD. <br />It uses a custom non-XML syntax, inherited from <a href="http://en.wikipedia.org/wiki/SGML" title="SGML">SGML</a>, to describe the schema. <br />DTD is still used in many applications because it is considered the easiest to read and write. <br /><a name="XML_Schema"></a>[<a href="http://en.wikipedia.org/w/index.php?title=XML&amp;action=edit&amp;section=9" title="Edit section: XML Schema">edit</a>] XML Schema <br />Main article: <a href="http://en.wikipedia.org/wiki/XML_Schema_(W3C)" title="XML Schema (W3C)">XML Schema (W3C)</a> <br />A newer <a href="http://en.wikipedia.org/wiki/XML_schema" title="XML schema">XML schema</a> language, described by the W3C as the successor of DTDs, is <a href="http://en.wikipedia.org/wiki/XML_Schema_(W3C)" title="XML Schema (W3C)">XML Schema</a>, or more informally referred to by the <a href="http://en.wikipedia.org/wiki/Acronym_and_initialism" title="Acronym and initialism">initialism</a> for XML Schema instances, XSD (XML Schema Definition). XSDs are far more powerful than DTDs in describing XML languages. They use a rich <a href="http://en.wikipedia.org/wiki/Datatype" title="Datatype">datatyping</a> system, allow for more detailed constraints on an XML document's logical structure, and must be processed in a more robust validation framework. XSDs also use an XML-based format, which makes it possible to use ordinary XML tools to help process them, although XSD implementations require much more than just the ability to read XML. <br /><a name="RELAX_NG"></a>[<a href="http://en.wikipedia.org/w/index.php?title=XML&amp;action=edit&amp;section=10" title="Edit section: RELAX NG">edit</a>] RELAX NG <br />Main article: <a href="http://en.wikipedia.org/wiki/RELAX_NG" title="RELAX NG">RELAX NG</a> <br />Another popular schema language for XML is <a href="http://en.wikipedia.org/wiki/RELAX_NG" title="RELAX NG">RELAX NG</a>. Initially specified by <a href="http://en.wikipedia.org/wiki/OASIS_(organization)" title="OASIS (organization)">OASIS</a>, RELAX NG is now also an ISO international standard (as part of <a href="http://en.wikipedia.org/wiki/DSDL" title="DSDL">DSDL</a>). It has two formats: an XML based syntax and a non-XML compact syntax. The compact syntax aims to increase readability and writability but, since there is a well-defined way to translate the compact syntax to the XML syntax and back again by means of <a href="http://en.wikipedia.org/wiki/James_Clark_(XML_expert)" title="James Clark (XML expert)">James Clark</a>'s <a href="http://en.wikipedia.org/w/index.php?title=Trang_conversion_tool&amp;action=edit&amp;redlink=1" title="Trang conversion tool (page does not exist)">Trang conversion tool</a>, the advantage of using standard XML tools is not lost. RELAX NG has a simpler definition and validation framework than XML Schema, making it easier to use and implement. It also has the ability to use <a href="http://en.wikipedia.org/wiki/Datatype" title="Datatype">datatype</a> framework <a href="http://en.wikipedia.org/wiki/Plug-in" title="Plug-in">plug-ins</a>; a RELAX NG schema author, for example, can require values in an XML document to conform to definitions in XML Schema Datatypes. <br /><a name="ISO_DSDL_and_other_schema_languages"></a>[<a href="http://en.wikipedia.org/w/index.php?title=XML&amp;action=edit&amp;section=11" title="Edit section: ISO DSDL and other schema languages">edit</a>] ISO DSDL and other schema languages <br />The ISO <a href="http://en.wikipedia.org/wiki/DSDL" title="DSDL">DSDL</a> (Document Schema Description Languages) standard brings together a comprehensive set of small schema languages, each targeted at specific problems. DSDL includes <a href="http://en.wikipedia.org/wiki/RELAX_NG" title="RELAX NG">RELAX NG</a> full and compact syntax, <a href="http://en.wikipedia.org/wiki/Schematron" title="Schematron">Schematron</a> assertion language, and languages for defining datatypes, character repertoire constraints, renaming and entity expansion, and namespace-based <a href="http://en.wikipedia.org/wiki/Routing" title="Routing">routing</a> of document fragments to different validators. DSDL schema languages do not have the vendor support of XML Schemas yet, and are to some extent a grassroots reaction of industrial publishers to the lack of utility of XML Schemas for <a href="http://en.wikipedia.org/wiki/Publishing" title="Publishing">publishing</a>. <br />Some schema languages not only describe the structure of a particular XML format but also offer limited facilities to influence processing of individual XML files that conform to this format. DTDs and XSDs both have this ability; they can for instance provide attribute defaults. RELAX NG and Schematron intentionally do not provide these; for example the <a href="http://en.wikipedia.org/wiki/Infoset" title="Infoset">infoset</a> augmentation facility. <br /><a name="International_use"></a>[<a href="http://en.wikipedia.org/w/index.php?title=XML&amp;action=edit&amp;section=12" title="Edit section: International use">edit</a>] International use <br />XML supports the direct use of almost any Unicode character in element names, attributes, comments, character data, and processing instructions (other than the ones that have special symbolic meaning in XML itself, such as the open corner bracket, "<"). Therefore, the following is a well-formed XML document, even though it includes both <a href="http://en.wikipedia.org/wiki/Chinese_character" title="Chinese character">Chinese</a> and <a href="http://en.wikipedia.org/wiki/Cyrillic_alphabet" title="Cyrillic alphabet">Cyrillic</a> characters:<!--xml version="1.0" encoding="UTF-8"?--><俄語>Китайский</俄語> <br /><a name="Displaying_XML_on_the_web"></a>[<a href="http://en.wikipedia.org/w/index.php?title=XML&amp;action=edit&amp;section=13" title="Edit section: Displaying XML on the web">edit</a>] Displaying XML on the web <br />Generally, generic XML documents do not carry information about how to display the data.<a href="http://en.wikipedia.org/wiki/XML#cite_note-4#cite_note-4" title="">[5]</a> Without using <a href="http://en.wikipedia.org/wiki/Cascading_Style_Sheets" title="Cascading Style Sheets">CSS</a> or <a href="http://en.wikipedia.org/wiki/XSLT" title="XSLT">XSLT</a>, a generic XML document is rendered as raw XML text by most <a href="http://en.wikipedia.org/wiki/Web_browser" title="Web browser">web browsers</a>. Some display it with 'handles' (e.g. + and - signs in the margin) that allow parts of the structure to be expanded or collapsed with mouse-clicks. <br />In order to style the rendering in a browser with CSS, the XML document must include a reference to the <a href="http://en.wikipedia.org/wiki/Style_sheet_(web_development)" title="Style sheet (web development)">stylesheet</a>:<!--xml-stylesheet type="text/css" href="myStyleSheet.css"?--> <br />Note that this is different from specifying such a stylesheet in HTML, which uses the <link> element. <br /><a href="http://en.wikipedia.org/wiki/XSLT" title="XSLT">XSLT</a> (<a href="http://en.wikipedia.org/wiki/XSL_Transformations" title="XSL Transformations">XSL Transformations</a>) can be used to alter the format of XML data, either into HTML or other formats that are suitable for a browser to display. <br />To specify <a href="http://en.wikipedia.org/wiki/Client-side" title="Client-side">client-side</a> XSLT, the following processing instruction is required in the XML:<!--xml-stylesheet type="application/xslt+xml" href="myTransform.xslt"?--> <br />Client-side XSLT is supported by many <a href="http://en.wikipedia.org/wiki/Web_browser" title="Web browser">web browsers</a>. Alternatively, one may use XSLT to convert XML into a displayable format on the <a href="http://en.wikipedia.org/wiki/Server_(computing)" title="Server (computing)">server</a> rather than being dependent on the <a href="http://en.wikipedia.org/wiki/End-user" title="End-user">end-user</a>'s <br /> <br />Lecture Notes &#8211;33 <br />Topc: XML Documents. <br />browser capabilities. The end-user is not aware of what has gone on 'behind the scenes'; all they see is well-formatted, displayable data. <br />See the XSLT article for <a href="http://en.wikipedia.org/wiki/XSL_Transformations#XSLT_examples" title="XSL Transformations">an example of server-side XSLT in action</a>. <br /><a name="XML_extensions"></a>[<a href="http://en.wikipedia.org/w/index.php?title=XML&amp;action=edit&amp;section=14" title="Edit section: XML extensions">edit</a>] XML extensions <br /><a href="http://en.wikipedia.org/wiki/XPath" title="XPath">XPath</a> makes it possible to refer to individual parts of an XML document. This provides random access to XML data for other technologies, including XSLT, XSL-FO, XQuery etc. XPath expressions can refer to all or part of the text, data and values in XML elements, attributes, processing instructions, comments etc. They can also access the names of elements and attributes. XPaths can be used in both valid and well-formed XML, with and without defined namespaces. <br /><a href="http://en.wikipedia.org/wiki/XInclude" title="XInclude">XInclude</a> defines the ability for XML files to include all or part of an external file. When processing is complete, the final XML infoset has no XInclude elements, but instead has copied the documents or parts thereof into the final infoset. It uses XPath to refer to a portion of the document for partial inclusions. <br /><a href="http://en.wikipedia.org/wiki/XQuery" title="XQuery">XQuery</a> is to XML and <a href="http://en.wikipedia.org/wiki/XML_database" title="XML database">XML Databases</a> what <a href="http://en.wikipedia.org/wiki/SQL" title="SQL">SQL</a> and <a href="http://en.wikipedia.org/wiki/PL/SQL" title="PL/SQL">PL/SQL</a> are to <a href="http://en.wikipedia.org/wiki/Relational_databases" title="Relational databases">relational databases</a>: ways to access, manipulate and return XML. <br /><a href="http://en.wikipedia.org/wiki/XML_Namespace" title="XML Namespace">XML Namespaces</a> enable the same document to contain XML elements and attributes taken from different vocabularies, without any <a href="http://en.wikipedia.org/wiki/Naming_collision" title="Naming collision">naming collisions</a> occurring. <br /><a href="http://en.wikipedia.org/wiki/XML_Signature" title="XML Signature">XML Signature</a> defines the syntax and processing rules for creating <a href="http://en.wikipedia.org/wiki/Digital_signatures" title="Digital signatures">digital signatures</a> on XML content. <br /><a href="http://en.wikipedia.org/wiki/XML_Encryption" title="XML Encryption">XML Encryption</a> defines the syntax and processing rules for <a href="http://en.wikipedia.org/wiki/Encryption" title="Encryption">encrypting</a> XML content. <br /><a href="http://en.wikipedia.org/wiki/XPointer" title="XPointer">XPointer</a> is a system for addressing components of XML-based internet media. <br />XML files may be served with a variety of <a href="http://en.wikipedia.org/wiki/MIME" title="MIME">Media types</a>. <a href="http://tools.ietf.org/html/rfc3023" title="http://tools.ietf.org/html/rfc3023">RFC 3023</a> defines the types "application/xml" and "text/xml", which say only that the data is in XML, and nothing about its semantics. The use of "text/xml" has been criticized as a potential source of encoding problems but is now in the process of being deprecated.<a href="http://en.wikipedia.org/wiki/XML#cite_note-5#cite_note-5" title="">[6]</a> <a href="http://tools.ietf.org/html/rfc3023" title="http://tools.ietf.org/html/rfc3023">RFC 3023</a> also recommends that XML-based languages be given media types beginning in "application/" and ending in "+xml"; for example "application/atom+xml" for <a href="http://en.wikipedia.org/wiki/Atom_(standard)" title="Atom (standard)">Atom</a>. This page discusses further <a href="http://en.wikipedia.org/wiki/XML_and_MIME" title="XML and MIME">XML and MIME</a>. <br /><a name="Processing_XML_files"></a>[<a href="http://en.wikipedia.org/w/index.php?title=XML&amp;action=edit&amp;section=15" title="Edit section: Processing XML files">edit</a>] Processing XML files <br />Three traditional techniques for processing XML files are: <br />Using a programming language and the <a href="http://en.wikipedia.org/wiki/Simple_API_for_XML" title="Simple API for XML">SAX</a> <a href="http://en.wikipedia.org/wiki/Application_programming_interface" title="Application programming interface">API</a>. <br />Using a programming language and the <a href="http://en.wikipedia.org/wiki/DOM_(XML_API)" title="DOM (XML API)">DOM</a> <a href="http://en.wikipedia.org/wiki/Application_programming_interface" title="Application programming interface">API</a>. <br />Using a transformation engine and a filter <br />More recent and emerging techniques for processing XML files are: <br />Pull Parsing <br />Non-Extractive Parsing (i.e. in-situ parsing) <br />Data binding <br /><a name="Simple_API_for_XML_.28SAX.29"></a>[<a href="http://en.wikipedia.org/w/index.php?title=XML&amp;action=edit&amp;section=16" title="Edit section: Simple API for XML (SAX)">edit</a>] Simple API for XML (SAX) <br /><a href="http://en.wikipedia.org/wiki/Simple_API_for_XML" title="Simple API for XML">SAX</a> is a <a href="http://en.wikipedia.org/wiki/Lexical_analysis" title="Lexical analysis">lexical</a>, <a href="http://en.wikipedia.org/wiki/Event-driven_programming" title="Event-driven programming">event-driven</a> interface in which a document is read serially and its contents are reported as "<a href="http://en.wikipedia.org/wiki/Callback" title="Callback">callbacks</a>" to various <a href="http://en.wikipedia.org/wiki/Method_(computer_science)" title="Method (computer science)">methods</a> on a <a href="http://en.wikipedia.org/wiki/Event_handler" title="Event handler">handler object</a> of the user's design. SAX is fast and efficient to implement, but difficult to use for extracting information at random from the XML, since it tends to burden the application author with keeping track of what part of the document is being processed. It is better suited to situations in which certain types of information are always handled the same way, no matter where they occur in the document. <br /><a name="DOM"></a>[<a href="http://en.wikipedia.org/w/index.php?title=XML&amp;action=edit&amp;section=17" title="Edit section: DOM">edit</a>] DOM <br /><a href="http://en.wikipedia.org/wiki/Document_Object_Model" title="Document Object Model">DOM</a> is an <a href="http://en.wikipedia.org/wiki/Interface" title="Interface">interface</a>-oriented <a href="http://en.wikipedia.org/wiki/Application_Programming_Interface" title="Application Programming Interface">Application Programming Interface</a> that allows for navigation of the entire document as if it were a tree of "<a href="http://en.wikipedia.org/wiki/Node_(computer_science)" title="Node (computer science)">Node</a>" <a href="http://en.wikipedia.org/wiki/Object_(computer_science)" title="Object (computer science)">objects</a> representing the document's contents. A DOM document can be created by a parser, or can be generated manually by users (with limitations). Data types in DOM Nodes are abstract; implementations provide their own <a href="http://en.wikipedia.org/wiki/Programming" title="Programming">programming</a> language-specific <a href="http://en.wikipedia.org/wiki/Binding" title="Binding">bindings</a>. DOM implementations tend to be <a href="http://en.wikipedia.org/wiki/Memory" title="Memory">memory</a> intensive, as they generally require the entire document to be loaded into memory and constructed as a tree of objects before access is allowed. DOM is supported in Java by several packages that usually come with the standard libraries. As the DOM specification is regulated by the <a href="http://en.wikipedia.org/wiki/World_Wide_Web_Consortium" title="World Wide Web Consortium">World Wide Web Consortium</a>, the main interfaces (Node, Document, etc.) are in the package org.w3c.dom.*, as well as some of the events and interfaces for other capabilities like serialization (output). The package com.sun.org.apache.xml.internal.serialize.* provides the serialization (output capacities) by implementing the appropriate interfaces, while the javax.xml.parsers.* package parses data to create DOM XML documents for manipulation.<a href="http://en.wikipedia.org/wiki/XML#cite_note-6#cite_note-6" title="">[7]</a> <br /><a name="Transformation_engines_and_filters"></a>[<a href="http://en.wikipedia.org/w/index.php?title=XML&amp;action=edit&amp;section=18" title="Edit section: Transformation engines and filters">edit</a>] Transformation engines and filters <br />A <a href="http://en.wikipedia.org/wiki/Filter_(software)" title="Filter (software)">filter</a> in the <a href="http://en.wikipedia.org/wiki/Extensible_Stylesheet_Language" title="Extensible Stylesheet Language">Extensible Stylesheet Language</a> (XSL) family can transform an XML file for <a href="http://en.wikipedia.org/wiki/Display" title="Display">displaying</a> or <a href="http://en.wikipedia.org/wiki/Computer_printer" title="Computer printer">printing</a>. <br /><a href="http://en.wikipedia.org/wiki/XSL-FO" title="XSL-FO">XSL-FO</a> is a declarative, XML-based <a href="http://en.wikipedia.org/wiki/Page_layout" title="Page layout">page layout</a> language. An XSL-FO processor can be used to convert an XSL-FO document into another non-XML format, such as <a href="http://en.wikipedia.org/wiki/PDF" title="PDF">PDF</a>. <br /><a href="http://en.wikipedia.org/wiki/XSLT" title="XSLT">XSLT</a> is a declarative, XML-based document transformation language. An XSLT processor can use an XSLT stylesheet as a guide for the conversion of the data tree represented by one XML document into another tree that can then be <a href="http://en.wikipedia.org/wiki/Serialize" title="Serialize">serialized</a> as XML, HTML, plain text, or any other format supported by the processor. <br /><a href="http://en.wikipedia.org/wiki/XQuery" title="XQuery">XQuery</a> is a W3C language for <a href="http://en.wikipedia.org/wiki/Query" title="Query">querying</a>, constructing and transforming XML data. <br /><a href="http://en.wikipedia.org/wiki/XPath" title="XPath">XPath</a> is a DOM-like node tree <a href="http://en.wikipedia.org/wiki/Data_model" title="Data model">data model</a> and <a href="http://en.wikipedia.org/wiki/Path_expression" title="Path expression">path expression</a> language for selecting data within XML documents. XSL-FO, XSLT and XQuery all make use of XPath. XPath also includes a useful <a href="http://en.wikipedia.org/wiki/Function_library" title="Function library">function library</a>. <br /><a name="Pull_parsing"></a>[<a href="http://en.wikipedia.org/w/index.php?title=XML&amp;action=edit&amp;section=19" title="Edit section: Pull parsing">edit</a>] Pull parsing <br />Pull parsing<a href="http://en.wikipedia.org/wiki/XML#cite_note-7#cite_note-7" title="">[8]</a> treats the document as a series of items which are read in sequence using the Iterator design pattern. This allows for writing of <a href="http://en.wikipedia.org/wiki/Recursive_descent_parser" title="Recursive descent parser">recursive-descent parsers</a> in which the structure of the code performing the parsing mirrors the structure of the XML being parsed, and intermediate parsed results can be used and accessed as local variables within the methods performing the parsing, or passed down (as method parameters) into lower-level methods, or returned (as method return values) to higher-level methods. Examples of pull parsers include <a href="http://en.wikipedia.org/wiki/StAX" title="StAX">StAX</a> in the <a href="http://en.wikipedia.org/wiki/Java_(programming_language)" title="Java (programming language)">Java</a> programming language, SimpleXML in PHP and System.Xml.XmlReader in .NET. <br />A pull parser creates an iterator that sequentially visits the various elements, attributes, and data in an XML document. Code which uses this 'iterator' can test the current item (to tell, for example, whether it is a start or end element, or text), and inspect its attributes (local name, namespace, values of XML attributes, value of text, etc.), and can also move the iterator to the 'next' item. The code can thus extract information from the document as it traverses it. The recursive-descent approach tends to lend itself to keeping data as typed local variables in the code doing the parsing, while SAX, for instance, typically requires a parser to manually maintain intermediate data within a stack of elements which are parent elements of the element being parsed. Pull-parsing code can be more straightforward to understand and maintain than SAX parsing code. <br /><a name="Non-extractive_XML_Processing_API"></a>[<a href="http://en.wikipedia.org/w/index.php?title=XML&amp;action=edit&amp;section=20" title="Edit section: Non-extractive XML Processing API">edit</a>] Non-extractive XML Processing API <br />Non-extractive XML Processing API is a new and emerging category of parsers that aim to overcome the fundamental limitations of DOM and SAX. The most representative is <a href="http://en.wikipedia.org/wiki/VTD-XML" title="VTD-XML">VTD-XML</a>, which abolishes the object-oriented modeling of XML hierarchy and instead uses 64-bit Virtual Token Descriptors (encoding offsets, lengths, depths, and types) of XML tokens. VTD-XML's approach enables a number of interesting features/enhancements, such as high performance, low memory usage <a href="http://en.wikipedia.org/wiki/XML#cite_note-8#cite_note-8" title="">[9]</a>, ASIC implementation <a href="http://en.wikipedia.org/wiki/XML#cite_note-9#cite_note-9" title="">[10]</a>, incremental update <a href="http://en.wikipedia.org/wiki/XML#cite_note-10#cite_note-10" title="">[11]</a>, and native XML indexing <a href="http://en.wikipedia.org/wiki/XML#cite_note-11#cite_note-11" title="">[12]</a> <a href="http://en.wikipedia.org/wiki/XML#cite_note-12#cite_note-12" title="">[13]</a>. <br /><a name="Data_binding"></a>[<a href="http://en.wikipedia.org/w/index.php?title=XML&amp;action=edit&amp;section=21" title="Edit section: Data binding">edit</a>] Data binding <br />Another form of XML Processing API is <a href="http://en.wikipedia.org/wiki/XML_data_binding" title="XML data binding">data binding</a>, where XML data is made available as a hierarchy of custom, strongly typed classes, in contrast to the generic objects created by a <a href="http://en.wikipedia.org/wiki/DOM" title="DOM">Document Object Model</a> parser. This approach simplifies code development, and in many cases allows problems to be identified at compile time rather than run-time. Example data binding systems include the <a href="http://en.wikipedia.org/wiki/Java_(programming_language)" title="Java (programming language)">Java</a> <a href="http://en.wikipedia.org/wiki/Architecture" title="Architecture">Architecture</a> for XML Binding (<a href="http://en.wikipedia.org/wiki/JAXB" title="JAXB">JAXB</a>)<a href="http://en.wikipedia.org/wiki/XML#cite_note-13#cite_note-13" title="">[14]</a>, Liquid XML Data Binder for <a href="http://en.wikipedia.org/wiki/C%2B%2B" title="C++">C++</a>, <a href="http://en.wikipedia.org/wiki/Java_(programming_language)" title="Java (programming language)">Java</a>, <a href="http://en.wikipedia.org/wiki/Visual_Basic" title="Visual Basic">VB</a> &amp; <a href="http://en.wikipedia.org/wiki/.NET_Framework" title=".NET Framework">.Net</a> <a href="http://en.wikipedia.org/wiki/XML#cite_note-14#cite_note-14" title="">[15]</a> and <a href="http://en.wikipedia.org/wiki/CodeSynthesis_XSD" title="CodeSynthesis XSD">CodeSynthesis XSD</a> for <a href="http://en.wikipedia.org/wiki/C%2B%2B" title="C++">C++</a><a href="http://en.wikipedia.org/wiki/XML#cite_note-15#cite_note-15" title="">[16]</a><a href="http://en.wikipedia.org/wiki/XML#cite_note-16#cite_note-16" title="">[17]</a>. <br /><a name="Specific_XML_applications_and_editors"></a>[<a href="http://en.wikipedia.org/w/index.php?title=XML&amp;action=edit&amp;section=22" title="Edit section: Specific XML applications and editors">edit</a>] Specific XML applications and editors <br />The <a href="http://en.wikipedia.org/wiki/Native_and_foreign_format" title="Native and foreign format">native file format</a> of <a href="http://en.wikipedia.org/wiki/OpenOffice.org" title="OpenOffice.org">OpenOffice.org</a>, <a href="http://en.wikipedia.org/wiki/AbiWord" title="AbiWord">AbiWord</a>, and <a href="http://en.wikipedia.org/wiki/Apple_Computer" title="Apple Computer">Apple</a>'s <a href="http://en.wikipedia.org/wiki/IWork" title="IWork">iWork</a> applications is XML. Some parts of <a href="http://en.wikipedia.org/wiki/Microsoft_Office" title="Microsoft Office">Microsoft Office</a> 2007 are also able to edit XML files with a user-supplied schema (but not a DTD), and Microsoft has released a file format compatibility kit for Office 2003 that allows previous versions of Office to save in the new XML based format. There are dozens of other <a href="http://en.wikipedia.org/wiki/XML_editor" title="XML editor">XML editors</a> available. <br /><a name="History"></a>[<a href="http://en.wikipedia.org/w/index.php?title=XML&amp;action=edit&amp;section=23" title="Edit section: History">edit</a>] History <br />The versatility of <a href="http://en.wikipedia.org/wiki/SGML" title="SGML">SGML</a> for dynamic information display was understood by early digital media publishers in the late 1980s prior to the rise of the Internet.<a href="http://en.wikipedia.org/wiki/XML#cite_note-OED-17#cite_note-OED-17" title="">[18]</a><a href="http://en.wikipedia.org/wiki/XML#cite_note-multimedia-18#cite_note-multimedia-18" title="">[19]</a> By the mid-1990s some practitioners of SGML had gained experience with the then-new <a href="http://en.wikipedia.org/wiki/World_Wide_Web" title="World Wide Web">World Wide Web</a>, and believed that SGML offered solutions to some of the problems the Web was likely to face as it grew. <a href="http://en.wikipedia.org/wiki/Dan_Connolly" title="Dan Connolly">Dan Connolly</a> added SGML to the list of W3C's activities when he joined the staff in 1995; work began in mid-1996 when Sun Microsystems engineer <a href="http://en.wikipedia.org/wiki/Jon_Bosak" title="Jon Bosak">Jon Bosak</a> developed a charter and recruited collaborators. Bosak was well connected in the small community of people who had experience both in SGML and the Web. <br />XML was compiled by a <a href="http://en.wikipedia.org/wiki/Working_group" title="Working group">working group</a> of eleven members,<a href="http://en.wikipedia.org/wiki/XML#cite_note-19#cite_note-19" title="">[20]</a> supported by an (approximately) 150-member Interest Group. Technical debate took place on the Interest Group mailing list and issues were resolved by consensus or, when that failed, majority vote of the Working Group. A record of design decisions and their rationales was compiled by <a href="http://en.wikipedia.org/wiki/Michael_Sperberg-McQueen" title="Michael Sperberg-McQueen">Michael Sperberg-McQueen</a> on December 4, 1997.<a href="http://en.wikipedia.org/wiki/XML#cite_note-20#cite_note-20" title="">[21]</a> <a href="http://en.wikipedia.org/wiki/James_Clark_(XML_expert)" title="James Clark (XML expert)">James Clark</a> served as Technical Lead of the Working Group, notably contributing the empty-element "<empty/>" syntax and the name "XML". Other names that had been put forward for consideration included "MAGMA" (Minimal Architecture for Generalized Markup Applications), "SLIM" (Structured Language for Internet Markup) and "MGML" (Minimal Generalized Markup Language). The co-editors of the specification were originally <a href="http://en.wikipedia.org/wiki/Tim_Bray" title="Tim Bray">Tim Bray</a> and <a href="http://en.wikipedia.org/wiki/Michael_Sperberg-McQueen" title="Michael Sperberg-McQueen">Michael Sperberg-McQueen</a>. Halfway through the project Bray accepted a consulting engagement with <a href="http://en.wikipedia.org/wiki/Netscape_Communications_Corporation" title="Netscape Communications Corporation">Netscape</a>, provoking vociferous protests from Microsoft. Bray was temporarily asked to resign the editorship. This led to intense dispute in the Working Group, eventually solved by the appointment of Microsoft's <a href="http://en.wikipedia.org/w/index.php?title=Jean_Paoli&amp;action=edit&amp;redlink=1" title="Jean Paoli (page does not exist)">Jean Paoli</a> as a third co-editor. <br />The XML Working Group never met face-to-face; the design was accomplished using a combination of email and weekly teleconferences. The major design decisions were reached in twenty weeks of intense work between July and November 1996, when the first Working Draft of an XML specification was published.<a href="http://en.wikipedia.org/wiki/XML#cite_note-21#cite_note-21" title="">[22]</a> Further design work continued through 1997, and XML 1.0 became a <a href="http://en.wikipedia.org/wiki/W3C" title="W3C">W3C</a> Recommendation on February 10, 1998. <br />XML 1.0 achieved the Working Group's goals of Internet usability, general-purpose usability, SGML compatibility, facilitation of easy development of processing software, minimization of optional features, legibility, formality, conciseness, and ease of authoring. Like its antecedent SGML, XML allows for some redundant syntactic constructs and includes repetition of element identifiers. In these respects, terseness was not considered essential in its structure. <br /><a name="Sources"></a>[<a href="http://en.wikipedia.org/w/index.php?title=XML&amp;action=edit&amp;section=24" title="Edit section: Sources">edit</a>] Sources <br />XML is a profile of an ISO standard <a href="http://en.wikipedia.org/wiki/SGML" title="SGML">SGML</a>, and most of XML comes from SGML unchanged. From SGML comes the separation of logical and physical structures (elements and entities), the availability of grammar-based validation (DTDs), the separation of data and metadata (elements and attributes), mixed content, the separation of processing from representation (processing instructions), and the default angle-bracket syntax. Removed were the SGML Declaration (XML has a fixed delimiter set and adopts <a href="http://en.wikipedia.org/wiki/Unicode" title="Unicode">Unicode</a> as the document <a href="http://en.wikipedia.org/wiki/Character_encoding" title="Character encoding">character set</a>). <br />Other sources of technology for XML were the <a href="http://en.wikipedia.org/wiki/Text_Encoding_Initiative" title="Text Encoding Initiative">Text Encoding Initiative</a> (TEI), which defined a profile of SGML for use as a 'transfer syntax'; <a href="http://en.wikipedia.org/wiki/HTML" title="HTML">HTML</a>, in which elements were synchronous with their resource, the separation of document character set from resource encoding, the xml:lang attribute, and the <a href="http://en.wikipedia.org/wiki/HTTP" title="HTTP">HTTP</a> notion that metadata accompanied the resource rather than being needed at the declaration of a link; and the Extended Reference Concrete Syntax (ERCS), from which XML 1.0's naming rules were taken, and which had introduced hexadecimal numeric character references and the concept of references to make available all Unicode characters. <br />Ideas that developed during discussion which were novel in XML, were the algorithm for encoding detection and the encoding header, the processing instruction target, the xml:space attribute, and the new close delimiter for empty-element tags. <br /><a name="Versions"></a>[<a href="http://en.wikipedia.org/w/index.php?title=XML&amp;action=edit&amp;section=25" title="Edit section: Versions">edit</a>] Versions <br />There are two current versions of XML. The first, XML 1.0, was initially defined in 1998. It has undergone minor revisions since then, without being given a new version number, and is currently in its fifth edition, as published on November 26, 2008. It is widely implemented and still recommended for general use. The second, XML 1.1, was initially published on February 4, 2004, the same day as XML 1.0 Third Edition, and is currently in its second edition, as published on August 16, 2006. It contains features &#8212; some contentious &#8212; that are intended to make XML easier to use in certain cases<a href="http://en.wikipedia.org/wiki/XML#cite_note-22#cite_note-22" title="">[23]</a> - mainly enabling the use of line-ending characters used on <a href="http://en.wikipedia.org/wiki/EBCDIC" title="EBCDIC">EBCDIC</a> platforms, and the use of scripts and characters absent from Unicode 2.0. XML 1.1 is not very widely implemented and is recommended for use only by those who need its unique features. <a href="http://en.wikipedia.org/wiki/XML#cite_note-23#cite_note-23" title="">[24]</a> <br />Prior to the fifth edition of XML 1.0, it and XML 1.1 differed in the requirements of characters used for element and attribute names: the first four editions of XML 1.0 only allowed characters which are defined in Unicode 2.0, which includes most world scripts, but excludes those which were added in later Unicode versions. Among the excluded scripts are <a href="http://en.wikipedia.org/wiki/Mongolian_language" title="Mongolian language">Mongolian</a>, <a href="http://en.wikipedia.org/wiki/Cambodian_language" title="Cambodian language">Cambodian</a>, <a href="http://en.wikipedia.org/wiki/Amharic" title="Amharic">Amharic</a>, <a href="http://en.wikipedia.org/wiki/Burmese_language" title="Burmese language">Burmese</a>, and others. <br />Almost any Unicode character can be used in the character data and attribute values of an XML 1.1 document, even if the character is not defined, aside from having a code point, in the current version of Unicode. The approach in XML 1.1 is that only certain characters are forbidden, and everything else is allowed, whereas in older editions of XML 1.0, only certain characters were explicitly allowed, and thus prior to its fifth edition XML 1.0 could not accommodate the addition of characters in future versions of Unicode. <br />In character data and attribute values, XML 1.1 allows the use of more <a href="http://en.wikipedia.org/wiki/Control_character" title="Control character">control characters</a> than XML 1.0, but, for "robustness", most of the control characters introduced in XML 1.1 must be expressed as numeric character references. Among the supported control characters in XML 1.1 are two line break codes that must be treated as whitespace. Whitespace characters are the only control codes that can be written directly. <br />There are also discussions on an XML 2.0, although it seems unlikely[<a href="http://en.wikipedia.org/wiki/Wikipedia:Manual_of_Style#Unnecessary_vagueness" title="Wikipedia:Manual of Style">vague</a>] if such will ever come about. <a href="http://en.wikipedia.org/w/index.php?title=XML-SW&amp;action=edit&amp;redlink=1" title="XML-SW (page does not exist)">XML-SW</a> (SW for <a href="http://en.wikipedia.org/wiki/Skunk_works" title="Skunk works">skunk works</a>), written by one of the original developers of XML, contains some proposals for what an XML 2.0 might look like: elimination of DTDs from syntax, integration of <a href="http://en.wikipedia.org/wiki/Namespace_(computer_science)#XML" title="Namespace (computer science)">namespaces</a>, <a href="http://en.wikipedia.org/wiki/XML_Base" title="XML Base">XML Base</a> and <a href="http://en.wikipedia.org/wiki/XML_Information_Set" title="XML Information Set">XML Information Set</a> (infoset) into the base standard. <br />The World Wide Web Consortium also has an XML Binary Characterization Working Group doing preliminary research into use cases and properties for a binary encoding of the XML infoset. The working group is not chartered to produce any official standards. Since XML is by definition text-based, ITU-T and ISO are using the name <a href="http://en.wikipedia.org/wiki/Fast_Infoset" title="Fast Infoset">Fast Infoset</a> for their own binary infoset to avoid confusion (see ITU-T Rec. X.891 ISO/IEC 24824-1). <br /><a name="Patent_claims"></a>[<a href="http://en.wikipedia.org/w/index.php?title=XML&amp;action=edit&amp;section=26" title="Edit section: Patent claims">edit</a>] Patent claims <br />In October 2005 the small company <a href="http://en.wikipedia.org/wiki/Scientigo" title="Scientigo">Scientigo</a> publicly asserted that two of its patents, <a href="http://patft.uspto.gov/netacgi/nph-Parser?patentnumber=5842213" title="http://patft.uspto.gov/netacgi/nph-Parser?patentnumber=">U.S. Patent 5,842,213</a><a href="http://www.pat2pdf.org/pat2pdf/foo.pl?number=5842213" title="http://www.pat2pdf.org/pat2pdf/foo.pl?number="> </a> and <a href="http://patft.uspto.gov/netacgi/nph-Parser?patentnumber=6393426" title="http://patft.uspto.gov/netacgi/nph-Parser?patentnumber=">U.S. Patent 6,393,426</a><a href="http://www.pat2pdf.org/pat2pdf/foo.pl?number=6393426" title="http://www.pat2pdf.org/pat2pdf/foo.pl?number="> </a>, apply to the use of XML. The patents cover the "modeling, storage and transfer [of data] in a particular non-hierarchical, non-integrated neutral form", according to their applications, which were filed in 1997 and 1999. Scientigo CEO Doyal Bryant expressed a desire to "monetize" the patents but stated that the company was "not interested in having us against the world." He said that Scientigo was discussing the patents with several large corporations.<a href="http://en.wikipedia.org/wiki/XML#cite_note-24#cite_note-24" title="">[25]</a> <br />XML users and independent experts responded to Scientigo's claims with widespread skepticism and criticism. Some derided the company as a <a href="http://en.wikipedia.org/wiki/Patent_troll" title="Patent troll">patent troll</a>. <a href="http://en.wikipedia.org/wiki/Tim_Bray" title="Tim Bray">Tim Bray</a> described any claims that the patents covered XML as "ridiculous on the face of it".<a href="http://en.wikipedia.org/wiki/XML#cite_note-25#cite_note-25" title="">[26]</a> <br /><a name="Critique_of_XML"></a>[<a href="http://en.wikipedia.org/w/index.php?title=XML&amp;action=edit&amp;section=27" title="Edit section: Critique of XML">edit</a>] Critique of XML <br />Commentators have offered various critiques of XML, suggesting circumstances where XML provides both advantages and potential disadvantages.<a href="http://en.wikipedia.org/wiki/XML#cite_note-CriticSeeAlso-26#cite_note-CriticSeeAlso-26" title="">[27]</a> <br /><a name="Advantages_of_XML"></a>[<a href="http://en.wikipedia.org/w/index.php?title=XML&amp;action=edit&amp;section=28" title="Edit section: Advantages of XML">edit</a>] Advantages of XML <br />It allows for transfer of data across different networked applications. <br />It supports <a href="http://en.wikipedia.org/wiki/Unicode" title="Unicode">Unicode</a>, allowing almost any information in any written human language to be communicated. <br />It can represent common <a href="http://en.wikipedia.org/wiki/Computer_science" title="Computer science">computer science</a> <a href="http://en.wikipedia.org/wiki/Data_structure" title="Data structure">data structures</a>: <a href="http://en.wikipedia.org/wiki/Record_(computer_science)" title="Record (computer science)">records</a>, <a href="http://en.wikipedia.org/wiki/List_(computing)" title="List (computing)">lists</a> and <a href="http://en.wikipedia.org/wiki/Tree_data_structure" title="Tree data structure">trees</a>. <br />Its <a href="http://en.wikipedia.org/wiki/Self-documenting" title="Self-documenting">self-documenting</a> format describes <a href="http://en.wikipedia.org/wiki/Structure" title="Structure">structure</a> and <a href="http://en.wikipedia.org/wiki/Field_name" title="Field name">field names</a> as well as specific values. <br />The strict <a href="http://en.wikipedia.org/wiki/Syntax" title="Syntax">syntax</a> and <a href="http://en.wikipedia.org/wiki/Parsing" title="Parsing">parsing</a> requirements make the necessary <a href="http://en.wikipedia.org/wiki/Parser" title="Parser">parsing algorithms</a> extremely simple, efficient, and consistent. <br />XML is heavily used as a format for document storage and processing, both online and offline. <br />It is based on <a href="http://en.wikipedia.org/wiki/International_standards" title="International standards">international standards</a>. <br />It can be updated incrementally. <br />It allows validation using schema languages such as <a href="http://en.wikipedia.org/wiki/XSD" title="XSD">XSD</a> and <a href="http://en.wikipedia.org/wiki/Schematron" title="Schematron">Schematron</a>, which makes effective unit-testing, firewalls, acceptance testing, contractual specification and software construction easier. <br />The <a href="http://en.wikipedia.org/wiki/Hierarchy" title="Hierarchy">hierarchical</a> structure is suitable for most (but not all) types of documents. <br />It is platform-independent, thus relatively immune to changes in technology. <br />Forward and backward compatibility are relatively easy to maintain despite changes in DTD or Schema. <br />Its predecessor, <a href="http://en.wikipedia.org/wiki/SGML" title="SGML">SGML</a>, has been in use since 1986, so there is extensive experience and software available. <br /><a name="Disadvantages_of_XML"></a>[<a href="http://en.wikipedia.org/w/index.php?title=XML&amp;action=edit&amp;section=29" title="Edit section: Disadvantages of XML">edit</a>] Disadvantages of XML <br />Difficult for the end-user to understand its capabilities. <br />XML syntax is redundant or large relative to binary representations of similar data,<a href="http://en.wikipedia.org/wiki/XML#cite_note-Elliotte001-27#cite_note-Elliotte001-27" title="">[28]</a> especially with <a href="http://en.wikipedia.org/wiki/Table_(information)" title="Table (information)">tabular</a> data. <br />The redundancy may affect application efficiency through higher storage, transmission and processing costs.<a href="http://en.wikipedia.org/wiki/XML#cite_note-Elliotte000-28#cite_note-Elliotte000-28" title="">[29]</a><a href="http://en.wikipedia.org/wiki/XML#cite_note-However000-29#cite_note-However000-29" title="">[30]</a> <br />XML syntax is verbose, especially for human readers, relative to other alternative 'text-based' data transmission formats.<a href="http://en.wikipedia.org/wiki/XML#cite_note-Bierman000-30#cite_note-Bierman000-30" title="">[31]</a><a href="http://en.wikipedia.org/wiki/XML#cite_note-VerbRebut000-31#cite_note-VerbRebut000-31" title="">[32]</a> <br />The <a href="http://en.wikipedia.org/wiki/Hierarchical_model" title="Hierarchical model">hierarchical model</a> for representation is limited in comparison to an <a href="http://en.wikipedia.org/wiki/Object_oriented" title="Object oriented">object oriented</a> <a href="http://en.wikipedia.org/wiki/Graph_(mathematics)" title="Graph (mathematics)">graph</a>.<a href="http://en.wikipedia.org/wiki/XML#cite_note-TreeLimit000-32#cite_note-TreeLimit000-32" title="">[33]</a><a href="http://en.wikipedia.org/wiki/XML#cite_note-Lim000-33#cite_note-Lim000-33" title="">[34]</a> <br />Expressing overlapping (non-hierarchical) node relationships requires extra effort.<a href="http://en.wikipedia.org/wiki/XML#cite_note-Searle000-34#cite_note-Searle000-34" title="">[35]</a> <br />XML namespaces are problematic to use and namespace support can be difficult to correctly implement in an XML parser.<a href="http://en.wikipedia.org/wiki/XML#cite_note-Names000-35#cite_note-Names000-35" title="">[36]</a> <br />XML is commonly depicted as "<a href="http://en.wikipedia.org/wiki/Self-documenting" title="Self-documenting">self-documenting</a>" but this depiction ignores critical ambiguities.<a href="http://en.wikipedia.org/wiki/XML#cite_note-selfdesc000-36#cite_note-selfdesc000-36" title="">[37]</a><a href="http://en.wikipedia.org/wiki/XML#cite_note-37#cite_note-37" title="">[38]</a> <br />The distinction between content and attributes in XML seems unnatural to some and makes designing XML data structures harder.<a href="http://en.wikipedia.org/wiki/XML#cite_note-XMLSuck8-38#cite_note-XMLSuck8-38" title="">[39]</a> <br />Transformations, even identity transforms, result in changes to format (whitespace, attribute ordering, attribute quoting, whitespace around attributes, newlines). These problems can make <a href="http://en.wikipedia.org/wiki/Diff" title="Diff">diff</a>-ing the XML source very difficult except via <a href="http://en.wikipedia.org/wiki/Canonical_XML" title="Canonical XML">Canonical XML</a>. <br />Encourages non-relational data structures (data non-normalized) <br />XML is very concrete and highly non-canonical. It introduces a very strong coupling between the actual representation chosen and the processing program (unlike relational storage and SQL)[<a href="http://en.wikipedia.org/wiki/Wikipedia:Citation_needed" title="Wikipedia:Citation needed">citation needed</a>] <br /><a name="XML_in_business"></a>[<a href="http://en.wikipedia.org/w/index.php?title=XML&amp;action=edit&amp;section=30" title="Edit section: XML in business">edit</a>] XML in business <br /><a href="http://en.wikipedia.org/wiki/XBRL" title="XBRL">XBRL</a> (eXtensible Business Reporting Language), which is based on XML, is widely used in the financial world. Due to increased demand for financial transparency, XBRL technology helps facilitate the processes of transferring data and reporting financial results. Notably, many Japanese banks use XBRL in their daily business.[<a href="http://en.wikipedia.org/wiki/Wikipedia:Citation_needed" title="Wikipedia:Citation needed">citation needed</a>] <br />Web publishing uses XML to have a single source for creating and updating all content, which significantly saves time and reduce cost for companies when printing documents. In addition, governments in the world use XML for large documentation and printing maps. Airplane and car manufactures may use XML technology to print maintenance booklets.[<a href="http://en.wikipedia.org/wiki/Wikipedia:Citation_needed" title="Wikipedia:Citation needed">citation needed</a>] <br /><a name="Standardization"></a>[<a href="http://en.wikipedia.org/w/index.php?title=XML&amp;action=edit&amp;section=31" title="Edit section: Standardization">edit</a>] Standardization <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br />Lecture Notes &#8211;34 <br />Topc: Introduction to Java scripting <br />JavaScript <br />JavaScript is the scripting standard for HTML. <br />DHTML is about using JavaScript to control, access and manipulate HTML elements. <br />You can read more about this in the next chapter of this tutorial. <br />HTML DOM <br />The HTML DOM is the W3C standard Document Object Model for HTML. <br />The HTML DOM defines a standard set of objects for HTML, and a standard way to access and manipulate them. <br />DHTML is about using the DOM to access and manipulate HTML elements. <br />You can read more about this in a later chapter of this tutorial. <br />HTML Events <br />The W3C HTML Event Model is a part of the HTML DOM. <br />It defines a standard way to handle HTML events. <br />DHTML is about creating web pages that reacts to (user)events. <br />You can read more about this in a later chapter of this tutorial. <br />CSS <br />CSS is the W3C standard style and layout model for HTML. <br />CSS allows web developers to control the style and layout of web pages. <br />HTML 4 allows dynamic changes to CSS. <br />DHTML is about using JavaScript and DOM to change the style and positioning of HTML elements. <br />You can read more about this in a later chapter of this tutorial <br />JavaScript can create dynamic HTML content: <br />Date: <br />document.write(Date()); <br />Fri Dec 26 12:04:44 2008 <br />JavaScript Alone <br />If you have studied JavaScript, you already know that the statement: <br />document.write() <br />can be used to display dynamic content to a web page. <br />Example <br />Using JavaScript to display the current date: <br /><html><body> <script type="text/javascript">document.write(Date());</script> </body></html> <br /><a href="http://www.w3schools.com/dhtml/tryit.asp?filename=trydhtml_date" target="_blank">Try it Yourself</a> <br />JavaScript and the HTML DOM <br />With HTML 4, JavaScript can also be used to change the inner content and attributes of HTML elements dynamically. <br />To change the content of an HTML element use: <br />document.getElementById(id).innerHTML=new HTML <br />To change the attribute of an HTML element use: <br />document.getElementById(id).attribute=new value <br />You will learn more about JavaScript and the HTML DOM in the next chapter of this tutorial. <br />JavaScript and HTML Events <br />New to HTML 4 is the ability to let HTML events trigger actions in the browser, like starting a JavaScript when a user clicks on an HTML element. <br />To execute code when a user clicks on an element, use the following event attribute: <br />onclick=JavaScript <br />You will learn more about JavaScript and HTML Events in a later chapter. <br />JavaScript and CSS <br />With HTML 4, JavaScript can also be used to change the style of HTML elements. <br />To change the style of an HTML element use: <br />document.getElementById(id).style.property=new style <br />You will learn more about JavaScript and CSS in a later chapter of this tutorial. <br /> <br />More About JavaScript, DOM, And CSS <br />For a full overview please refer to the complete <a href="http://www.w3schools.com/htmldom/dom_obj_style.asp">DOM Style Object Reference</a> in our HTML DOM tutorial. <br />To learn more about CSS, find the complete CSS tutorial on our <a href="http://www.w3schools.com/default.asp">Home Page</a>. <br />JavaScript Examples <br />Learn by 100 examples! <br />With our editor, you can edit the source code, and click on a test button to view the result. <br /><a href="http://www.w3schools.com/JS/js_examples.asp">JavaScript Examples</a> <br /><a href="http://www.w3schools.com/JS/js_examples_2.asp">JavaScript Object Examples</a> <br /><a href="http://www.w3schools.com/JS/js_examples_3.asp">JavaScript HTML DOM Examples</a> <br />JavaScript Quiz Test <br />Test your JavaScript skills at W3Schools! <br /><a href="http://www.w3schools.com/JS/js_quiz.asp">Start JavaScript Quiz!</a> <br />JavaScript References <br />DHTML is about combining HTML, JavaScript, DOM, and CSS. <br />DHTML is a Term <br />In this tutorial you have learned that DHTML is only a term used to describe the different combinations of HTML, JavaScript, DOM, and CSS that can be used to create more dynamic web pages. <br /><a href="http://www.w3schools.com/dhtml/dhtml_examples.asp">More DHTML examples</a> <br />JavaScript <br />JavaScript is the standard scripting language for the Internet. <br />Everyone serious about web development should have a full understanding of JavaScript. <br />Visit our <a href="http://www.w3schools.com/js/default.asp">JavaScript tutorial</a>, and our complete <a href="http://www.w3schools.com/jsref/default.asp">JavaScript reference</a>. <br />The HTML DOM <br />HTML 4 supports the HTML Document Object Model (DOM). <br />The HTML DOM is the official (standard and browser independent) way to access HTML elements. It works in all browsers. <br />Only by using the HTML DOM you can make interactive web pages that will work in all modern browsers. <br />If you are serious about web development, study our <a href="http://www.w3schools.com/htmldom/default.asp">HTML DOM tutorial</a>, and our complete <a href="http://www.w3schools.com/htmldom/dom_reference.asp">HTML DOM reference</a>. <br />Dynamic CSS <br />There is no such thing as dynamic CSS. <br />However, with JavaScript and the HTML DOM you can dynamically change the CSS style of any HTML element. <br />Server Side Scripting <br />In this tutorial you have seen that web pages can be made dynamic by using scripts on the client (in the browser). <br />Web pages can also be made more dynamic by using scripts on the server. <br />With server scripting you can edit, add, or change any web page content. You can respond to data submitted from HTML forms, access data or databases and return the results to a browser, and customize pages for individual users. <br />At W3Schools you can study the following server side scripting tutorials: <br />JavaScript Alone <br />If you have studied JavaScript, you already know that the statement: <br />document.write() <br />can be used to display dynamic content to a web page. <br />Example <br />Using JavaScript to display the current date: <br /><html><body> <script type="text/javascript">document.write(Date());</script> </body></html> <br /> <br /> <br />Lecture Notes &#8211;35 <br />Topc: Active Server Pages (ASP) <br />Are you sick of static HTML pages? Do you want to create dynamic web pages? Do you want to enable your web pages with database access? If your answer is &#8220;Yes&#8221;, ASP might be a solution for you. In May 2000, Microsoft estimated that there are over 800,000ASP developers in the world (1). You may come up with a question what the heck ASP is.Don&#8217;t worry. After reading this paper, you will know what it is, how it works and what it can do for you. Are you ready? Let&#8217;s get to know ASP together. <br />What is ASP? <br />ASP stands for Active Server Pages. Microsoft introduced Active Server Pages in December 1996, beginning with Version 3.0. Microsoft officially defines ASP as: &#8220;Active Server Pages is an open, compile-free application environment in which you can combine HTML, scripts, and reusable ActiveX server components to create dynamic and powerful Web-based business solutions. Active Server pages enables server side scripting for IIS with native support for both VBScript and Jscript.&#8221; (2). In other words, ASP is a Microsoft technology that enables you to create dynamic web sites with the help of server side script, such as VBScript and Jscript. ASP technology is supported on all Microsoft Web servers that are freely available. If you have Window NT 4.0 Server installed, you can download IIS (Internet Information Server) 3.0 or 4.0. If you are using Window 2000, IIS 5.0 comes with it as a free component. If you have Window 95/98, you can download Personal Web Server (PWS), which is a smaller version of IIS, from Window 95/98 CD. You can also download these products for free from Microsoft&#8217;s web site. Well, you have learned what the ASP technology is. Next, you will learn about an ASP file. Is it the same as HTML file? Let&#8217;s explore it. <br /> <br /> <br /> <br /> <br /> <br /> <br />Lecture Notes &#8211;36 <br />Topc: ASP file <br />What is an ASP file? <br />An ASP file is quite like an HTML file. It contains text, HTML tags and scripts, which are executed on the server. The two widely used scripting languages for an ASP page are VBScript and JScript. VBScript is pretty much like Visual Basic, whereas Jscript is the Microsoft&#8217;s version of JavaScript. However, VBScript is the default scripting language for ASP (3). Besides these two scripting languages, you can use other scripting language with ASP as long as you have an ActiveX scripting engine for the language installed, such as PerlScript. The difference between an HTML file and an ASP file is that an ASP file has the &#8220;.asp&#8221; extension. Furthermore, script delimiters for HTML tags and ASP code are also different. A script delimiter is a character that marks the starting and ending of a unit. HTML tags begins with lesser than (<) and greater than (>) brackets, whereas ASP script typically starts with <% and ends with %>. In between the delimiters are the server-side scripts. <br />To write an ASP script, you don&#8217;t need any additional software because it can be written with any HTML editor, such as Notepad. Nonetheless, if you feel bored with the plain text and would like to use some special software, you can use Microsoft visual InterDev, which helps you to easily create an ASP page by giving you nice highlights and debugging dialogue boxes (4). I hope that you already have an idea of what an ASP file is and how it is different from an HTML file. In the next step, you will learn how ASP works. Let&#8217;s go. <br />How does it work? <br />As you have learned, scripts in an ASP file are server-side scripts, which means that the scripts are processed on the server and then the result of the scripts will be converted to HTML before sending to the web browser. To illustrate, let&#8217;s take a look at this table to compare the process of retrieving an HTML page and an ASP page (5). <br />HTML process ASP process <br />1. A user requests a web page (i.e., <br />http://www.utexas.edu/index.html <br />in the web browser. <br />2. The browser finds the appropriate <br />web server, and asks for the <br />required page. <br />3. The web server locates the required <br />page and sends it back to the <br />browser as HTML text. <br />4. The browser executes the client side <br />scripting (like JavaScripts) <br />determining how to display the <br />results <br />1. A user requests a web page (i.e., <br />http://www.devasp.com/test.asp <br />in the web browser. <br />2. The browser finds the appropriate <br />web server (like IIS or PWS), and <br />asks for the required page. <br />3. The web server locates the required <br />page, and parses out the ASP code <br />within the ASP script delimiters <br />(<%&#8230;%>), produces a standard <br />HTML page. The server sends that <br />HTML page back to the browser, so <br />the user cannot see ASP code. <br />4. The browser executes the client side <br />scripting (like JavaScripts) <br />determining how to display the <br />results <br />As you can see, the whole process of the two is quite similar. Since ASP is a server-side technology, the required page is executed on the server before the HTML is created and <br />served to the client. To make it clearer, Figure1 shows the processing behind a browser request to an ASP page (6). For example, a client types in a URL into your browser. The browser requests the ASP page from the web server. The server proceeds the file with &#8220;.asp&#8221; extension to ASP Engine in which Objects or ActiveX Components can be used to extend the web server with application-specific functionality. In addition, ASP will use ADO to connect to a Client Browser Server <br />ASP Engine <br />ADO <br />ODBC <br />Object / <br />Component <br />Provider <br />Driver <br />ASP Script <br />Oracle <br />SQL <br />Server <br />Access <br />FoxPro <br />Request ASP page <br />Send result <br />Request ASP page <br />Send result <br />Creating <br />HTML file <br />Recognize <br />&#8220;.asp&#8221; extension <br />Call for ADO <br />(ActiveX Data Object) <br />Call for Call for <br />Connect to data access <br />database (SQL, Access, Oracle, etc.) to pull out the relevant data, such as the current weather in specific area. Thus, a different page is generated according to the area specified and time that the page is accessed. Then, the server generates HTML tags before sending it back to the client. Therefore, when you view the source of an ASP file, you will not see any different from a standard HTML file. <br />ASP includes five build-in objects (7): <br />&#61623;&#61472;Request &#8211; to get information from the user that is passed along with an HTTP <br />request <br />&#61623;&#61472;Response &#8211; to output data to the requesting client <br />&#61623;&#61472;Server &#8211; to control the Internet Information Server <br />&#61623;&#61472;Session &#8211; to store variables associated with a given user session <br />&#61623;&#61472;Application &#8211; to store information that remain active for the lifetime of an <br />application, such as a page counter. <br />You have already known how ASP technology works. Isn&#8217;t it fascinating? You <br />probably want to know how to make it work and what you need to make it work. In the <br />next section, you will find the way to do it. <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> Lecture Notes &#8211;37 <br />Topc:1. ASP work <br />2. Advantage of ASP <br /> <br />1. ASP work <br />How to make it work? <br />First of all, you need to set up an environment where you can execute your scripts; that is you need to set up your web server (IIS or PWS). If you have an NT Server, IIS is already installed for you. If you Window 95/98, you can install PWS from the add-ons directory on the Win 98 CD. If you are using UNIX, you can Chili!Soft (www.chilisoft.com) instead of Microsoft web server. Next, you will need to install some database software, such as MS Access, Microsoft SQL Server (in case you want to connect to database). Last but not least, you need an ASP editor. It can be any text editor, like NotePad or Microsoft Visual InterDev. To create an ASP file, you may need to know a scripting language, such as VBScript or JScript. If you want to deal with a database, you also need to have some knowledge about database, such as creating a Data Source Name (DSN), creating a recordset. You can learn more about database access from this web site: <br />http://www.builder.com/Programming/ASPIntro/ss06.html <br />2. Advantage of ASP <br />What are its advantages? <br />While learning about ASP technology in previous sections, you might ask yourself all the time what it can do for you. Certainly, ASP must have some strength; otherwise, it won&#8217;t be popular as such. Let&#8217;s count on its strong points and functionality. 1. Dynamic web page &#8211; Since ASP supports scripting languages, which run on the web server, your web page can be dynamically created. For example, you can create your web page so as to greeting each user when they log into your page <br />differently. 2. Browser independent &#8211; ASP is browser independent because all the scripting code <br />runs on the server. The browser only gets the results from the server in a standard HTML page <br />3. Database Access &#8211; One of the greatest assets of ASP is database connectivity. ASP enables you to easily build rich database functionality into your web site, such as form processing. <br />4. Building-in objects &#8211; The five built-in objects that come with ASP facilitate Web application development. For example, you can use Request object to retrieve browser request information. <br />5. Free availability &#8211; Yes, it&#8217;s free. You can download web server (IIS or PWS) for free from Microsoft&#8217;s web site. You don&#8217;t even have to have any special tool to write an ASP file. In other words, you can simply use any text editor, like NotePad. <br />Conclusion <br />ASP technology is a server-side programming developed to enable dynamic web pages. With its build-in objects and ActiveX components, ASP developers can create a dynamic web page and make use of database access. Now that you have basic knowledge about ASP, it is better to have a hands-on experience, which is the best way to learn to learn how to write an ASP file. There are tons of ASP tutorials out there on the Web. You can also find some tutorials included at the end of this paper. Enjoy and good luck! <br /> <br /> <br /> <br /> <br />Lecture Notes &#8211;38 <br />Topc:. ASP Tags: <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br />Lecture Notes &#8211;39 <br />Topc: ASP Server Object: <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br />Lecture Notes &#8211;40 <br />Topc:. ASP Querying Collection: <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br />Lecture Notes &#8211;41 <br />Topc: Introduction to Electronic Commerce <br />Ecommerce marketers will have to learn new skills and attitudes to be successful. <br />by <a href="mailto:bob@textor.com">Bob Browning</a> <br />Electronic commerce is growing rapidly and many merchants are asking themselves how they can benefit from this new technology. This paper provides a grounding in the basic concepts. September 02, 1999 <br />The Internet is a different sales medium with some direct sales and some retail characteristics. Marketers will have to learn new skills and attitudes to be successful. <br />Any e-commerce solution needs to be functional and secure. Planning is vital, and other papers in this series discuss the business and technical issues that have to be addressed at the planning stage. <br /><a name="_Toc424612136">Introduction</a> <br />Electronic commerce over the Internet is predicted to grow at an ever-increasing rate over the next few years, with on-line sales already heading for several billion. Many companies are using this new sales channel, and a few retailers now have established major on-line sales sites. There have been some successes, particularly in technology, business-to-business and niche markets. <br />This paper has been produced to summarise the basics of electronic commerce, covering on-line catalogues and on-line purchasing. We have not attempted to review the subject of Electronic Document Interchange (EDI) which is clearly related, but is a very specialised subject. <br />The following sections cover: <br /> The Market, Its Size and Characteristics <br /> <a href="http://www.webdevelopersjournal.com/columns/ecommerce1.html#_Toc424612140#_Toc424612140">Selling vs. Marketing on the Internet</a> <br /> <a href="http://www.webdevelopersjournal.com/columns/ecommerce1.html#_Toc424612146#_Toc424612146">The Basic On-line Catalogue Mechanism</a> <br /> <a href="http://www.webdevelopersjournal.com/columns/ecommerce1.html#_Toc424612150#_Toc424612150">Payment Processing</a> <br /><a name="_Toc424612137">The Market</a> <br /><a name="_Toc424612138">The Market in the UK and Europe</a> <br />The NOP organisation publishes survey results on its Web site. The following quotes are from that source: <br /> Home shopping is still at an embryonic stage in all three countries. Approximately 10% of Web users in France, Germany and Britain have shopped on-line in the last four weeks. <br /> The NOP findings indicate that, as a proportion of the total adult population, the percentage of people who have used the Web in the past four weeks is highest in Britain and Germany. France currently has the smallest proportion of Internet users with relation to its total adult population - 6 per cent (2.87 million people). <br />The criterion of having purchased in the last four weeks is a little rigid, and most users will purchase less frequently than this. US studies indicate up to 25% of Internet users shop on-line, so the UK 'Internet Shopping' population is probably in the half to one million range. <br />However the potential figure may be much higher. In a recent survey more than half of those surveyed said they had no qualms about using their credit card over the Internet. So there may be as many as 2-3 million people who would purchase if the sales proposition is right. This research was carried out by leading market research company BMRB, for Internet Monitor. <br />In mass-market terms this may still be considered low, and merchants selling low-value items may not find that they generate the volume of business to justify a large expensive site. Any projects of this nature, such as supermarket sites, are experimental at this stage. <br />There are however profitable e-commerce operations in niche areas, business-to-business or export markets. For example, Textor Webmasters Ltd has been building a successful business intelligence site in partnership with CMS. Initial growth was slow, but now with the benefit of three years of experience and development the enterprise is profitable and has a very respectable turnover. <br />We recommend that merchants with the right product line and a good business plan start now with this sales channel. In most cases expect slow but steady growth rather than an immediate 'pot of gold'. Establish a beachhead now and grow your Internet presence so that when the market takes off - which it will - you are ready to take advantage of the opportunities that present themselves. <br /><a name="_Toc424612139">The Worldwide Market</a> <br />The total figure worldwide for Internet users is in the region of 200 million. Clearly anyone wishing to export should look at the Internet as a sales channel. <br /><a name="_Toc424612140">Selling on the Internet</a> <br /><a name="_Toc424612141">Marketing versus Selling Web Sites</a> <br />Most companies with an Internet presence have a straightforward marketing site. The objective of the site is to supplement traditional marketing activities, perhaps give additional information, and generally promote the company. There is often a reluctance to give complete product details because the objective is to induce visitors to call or write to the company for more information and thus establish contact. <br />A selling site is different. The objective is to close the sale electronically with payment (and sometimes delivery) made over the Internet. This type of site will be designed to include comprehensive product information, as visitors will be expected to make a purchasing decision based on the information presented. <br />Such sites generally have three sections: <br />Marketing and added value information. This is aimed at attracting customers, giving them a feel for the contents, and giving them confidence in the retailer. <br />The catalogue. Detailed information on product benefits, specifications, and pricing. <br />Order processing. This will include a method for specifying and paying for the order. More advanced systems may have a method for the customer to go back into the system to check progress and delivery of the order. <br />The true electronic commerce site will have all three components in some degree. <br />Such sites may be stand-alone, or may form part of a larger retailing site called a &#8216;shopping mall&#8217;. <br /><a name="_Toc424612142">Shopping Malls</a> <br />Internet shopping malls were set up early in the development of Internet commerce. A shopping mall has a standardised environment into which several merchants are held in a single Web site. They offer advantages to a new on-line merchant: <br /> A standard environment for setting up the catalogue and arranging payment. <br /> Someone else is arranging for promotion of the mall as a whole. <br /> In the UK, where the payment processing has historically been a problem, it has meant a trouble-free credit card collection mechanism. <br />However these benefits have not generally materialised. Malls work in the real world because there is something that attracts visitors, generally a large department store. Once visitors arrive, park their cars and start shopping, it is convenient for them to shop at other merchants in that same locality. The Internet is not like this. It is as easy to visit another shop anywhere in the world as the &#8216;next&#8217; shop in a virtual mall. People shopping for books are going to search for book sites. If they browse, it is the list of matches to their requirements from a search engine, not an on-line shopping mall. <br />From the infrastructure point of view, catalogue software and payment processing is now more widely available. Many merchants who started out in a shopping mall have graduated to a stand-alone site. <br /><a name="_Toc424612143">Building the Business</a> <br />It is not enough to simply set up the catalogue and electronic commerce programs. &#8216;Build it and they will come&#8217; has never been an approach that works on the Internet. The site must be promoted both on the Internet and via traditional means. <br />The biggest single source of visitors is also the cheapest. Registering with a few major search engines will generate over half, perhaps as much as 75% of your potential total traffic. <br />Other techniques are: <br /> Negotiating links with other Web sites <br /> Traditional marketing and PR <br /> Advertising on search engines and other high traffic sites <br /> Associate programs by which sites that refer visitors get a commission on sales. <br />Once the site is built and registered, look for other ways of building business such as special offers. Visitors can be tracked through the site and offers customised to their interests. These techniques are in their infancy, but are being developed rapidly. <br /><a name="_Toc424612144">Is this direct marketing?</a> <br />Direct marketers should also not make the mistake of equating the number of Internet users with a mail shot of that size. If there are 2-3 million potential Internet shoppers in the UK, a Web site is not like a mail shot to 2 million people. Only a tiny fraction of these will ever find your Web site and see it at all. The good news is that they will by and large be people interested in your product. <br />Direct marketers often have difficulty with handling on-line catalogues. The fact of the matter is that if someone visits a site and decides not to purchase, then that is the end of the matter. If someone is sent a catalogue or brochure and decides not to purchase, the merchant can re-send the catalogue, follow up by telephone, and so on. Direct marketers are often loath to lose the degree of control implied in traditional methods. <br /><a name="_Toc424612145">Is this one-to-one marketing?</a> <br />Internet selling is not retail, and it is not direct marketing. It has its own characteristics, which are evolving as the technology develops. As we learn more about this area we are finding new innovative ways of building the business, which are unique to this medium. Increasingly we are looking for ways to build a one-to-one relationship with customers using the Internet. <br />This has to be done carefully. You might be able to tell that you have a repeat visitor, but that person might not like the idea that someone is keeping track of his or her movements. By all means keep records of visitors' preferences so you can present them with the right special offers when they return. But be subtle - or preferably do this by consent. <br />This new world will require new attitudes and new marketing skills. It may not be right for every business, but when it is appropriate it can offer a low-cost channel with an outlet in virtually every country in the world. <br /><a name="_Toc424612146">The On-line Catalogue</a> <br />The key to a good electronic commerce site is to provide an environment that makes it easy for the customer to navigate through the catalogue of products and ultimately make a purchase. How does this work? In the following section, we take a look at the purchase cycle from the point of view of the customer. <br /><a name="_Toc424612147">Navigation</a> <br />The customer must be able to find the product they need without going through endless levels of indexes or menus. The visitor should be able to get to the product they need with very few clicks. <br />80% of visitors to any site will take one look at the page they arrive on and then leave. It has been estimated that you lose 20% of visitors every time you ask them to link to a new page. Good navigation is essential. <br />The information must be comprehensive once the customer has located the product of interest. Provide pictures and diagrams to help the customer understand what is being offered. <br /><a name="_Toc424612148">The Shopping Cart</a> <br />When the catalogue is small (say less than 20 items), a simple order form will often do the job. However on larger sites the customer will flag products during this browsing session to be added to an electronic &#8216;shopping cart&#8217;. At any point the customer can review the contents of the cart, the cost and so on. This makes it easy for the customer to browse the site selecting products as they go. <br /><a name="_Toc424612149">Check-out</a> <br />When the shopping session is complete, the customer clicks on a hyperlink which takes him or her to the checkout page. <br />At this stage the customer is presented with a list of the goods marked for purchase, the total cost, shipping, handling, tax, etc. The customer can then add shipping instructions, name, address and so on. <br />The customer is normally given a range of payment options, and some of the more common are discussed in more detail below. The most common is to use a credit card, and the customer enters the card number, name on the card and expiry date. <br />At this stage the Web site should switch to secure mode. The technology normally used is called SSL (Secure Socket Layer). This means that all communication with the server is encrypted in such a way that eavesdroppers cannot (without disproportionate difficulty) steal the credit card information. We shall discuss this further later, but it is important for customer confidence that the site switches to secure mode as soon as credit card information is requested. <br />The customer will get visual warning from his or her Web browser that they are in secure mode, a blue key and blue line in Netscape or a padlock symbol in Internet Explorer. There are some older browsers that don&#8217;t support SSL but most do. <br />This technology is widely used and quite well understood by Internet users. Most articles on e-commerce rightly emphasise the need for customers only to give up confidential information in a secure session, and users will look out for it. We believe that it is essential. <br />It is worth mentioning at this stage that a secure server is not absolutely necessary for bank-approved (and indeed very secure) e-commerce. There is a new British e-commerce product that uses its own Java-based encryption, and therefore does not need a secure server environment. <br />There are practical benefits here, and the solution is very cost effective and does not require more than a standard Internet server operation. The important issue here in our opinion is that the customer does not get the visual feedback from a secure session (the blue key in Netscape or the padlock symbol in Explorer). So irrespective of the technical merits of this solution, we believe it could adversely affect customer confidence. <br />It is not enough to be secure - you must be seen to be secure. <br />More on security later. <br /><a name="_Toc424612150">Payment and Order Processing</a> <br />There are a number of catalogue Web sites being run by UK companies, varying from large sophisticated book retailers to small &#8216;mom and pop&#8217; operations. The most popular payment mechanism is payment by credit card, and clearly such payments must be secure. However in a review of a number of such sites, we found that only a minority offered credit card payment over a secure link. <br />Other options are: <br /> Credit cards over an insecure link <br /> Purchase orders only <br /> Purchaser contacted later by phone or post <br /> Purchaser prints form and faxes it <br />The few sites that accept credit card information over an insecure link are almost certainly in violation of their agreement with the bank that is accepting their payments. They are also taking on the business risk of fraud. The risk does not stop at the bank but gets passed on to the merchant. . <br />Issues for these sites are: <br /> Perceived non-availability of secure payment methods. We discuss payment methods and security issues below. <br /> Inability of the design shop that developed the Web site to implement a complex catalogue or secure payment system. <br /> Difficulty in finding a commercial Web site hosting operation that will offer a suitable secure environment. <br /> Perceived cost of setting up a merchant server. <br />Most of these issues are perception rather than reality. There is no reason why a merchant should not be able to offer a fully functional catalogue site with a proper secure payment mechanism. This can be done very cost-effectively. <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /><a name="_Toc424612151">Lecture Notes &#8211;42</a> <br />Topic: Credit card processing <br /> <br />What is involved in credit card processing? <br />The steps in credit card processing are as follows. <br />Authorisation <br />The merchant must first obtain authorisation for the charge from the merchant&#8217;s credit card processing company. Authorisation simply means that the card has not been reported stolen, and there is sufficient credit on the card. It results in the customer&#8217;s credit limit being temporarily reduced by the value of the transaction. <br />There are two ways in which authorisation may be obtained: <br />Manual: The merchant downloads details of the sale from the computer that is acting as Web server. The merchant then requests authorisation using their normal method such as a point of sale (POS) terminal or PC program. <br />Automatic: The server software communicates directly with the credit card processing company computer and arranges authorisation on-line. <br />Clearly option 2 is preferred, but this is more complex and the costs are greater. <br />Capture <br />The final stage is for the credit card to be debited. This can happen at the same time as authorisation, provided that the merchant guarantees that delivery will take place within a certain fixed time. Otherwise capture should take place when the goods are shipped. <br />If the merchant's business is such that capture can take place immediately, then this can also happen automatically. Otherwise a second manual process is required. <br />Chargeback <br />Regretably, there is sometimes a further stage at which the customer is dissatisfied and arranges for the transaction to be cancelled. Because many Internet sales are made to overseas customers, many banks perceive that there is an increased risk of chargebacks. It has been reported that some merchants will not accept orders to Russia because of the frequency of chargeback. <br />Note that the fact that a payment has been authorised by the bank does not provide any protection against chargeback. <br /><a name="_Toc424612152">Other Payment Methods</a> <br />The discussion above has concentrated on credit card payments because they are the most efficient for most purchases. <br />However there are a number of alternatives, and you should offer as many of these on your site if you can, for example fax and telephone ordering should almost always be offered. <br />Fax <br />Simply printing an order form and faxing it to the merchant is feasible and reasonably secure. The form can be the secure order form - simply offer this as an option in the text. <br />Telephone Order <br />Offer customers the option of calling in their order, using the order form as a prompt. Many will prefer this, and the order form will be useful in confirming product codes and prices. <br />Micro-payments: <br />Whereas credit cards are fine for significant purchases, they are not efficient for a purchase of only a few pence (a micro-payment). There are systems being developed which operate like an electronic purse which can be recharged using traditional payment mechanisms. The purse can be depleted without formality for these small payments. Micro-payment systems are seen as a significant future development. The main players are: <br /> Mondex &#8211; originally developed in the UK but now operated by Master Card. This relies on the use of SmartCards to hold the value, and payments can be made from card to card without any intermediary. This makes the Mondex card a powerful substitute for cash, and with cheap smart card readers becoming available for PCs, a very acceptable Internet payment method. <br /> Visa Cash has been developed by Visa. <br /> Cybercash already has an electronic wallet concept to retain credit card information and pass it securely to a merchant (see below). This concept can readily be extended to electronic cash for micro-payments. <br /> Ecash is an early cash system, which is unlikely to survive in competition with giants like Visa. <br />Remember that micro-payment systems are often seen as less secure than other payment methods. For example the smart card can be stolen, like a real wallet. A trade off against security is part of the concept. For this reason there will normally be an upper limit to transaction and wallet sizes. <br />Proprietary Payment Systems <br />These were developed before secure server technology was widely available. They operate in different ways. <br /> Cybercash uses an &#8216;electronic wallet&#8217; to hold credit card details and to transmit them securely using their own encryption software. <br /> First Virtual uses a system of e-mail messages to confirm the sale. <br />The problem with all of these proprietary systems is that they require the user to do something to set themselves up, either to install special software or to register with the organisation. <br />Electronic Cheques <br />These are quite possible and are in use in the USA. <br />Purchase Orders <br />For business purchases a purchase order would be appropriate. <br />Electronic Commerce - Security issues <br />Merchants can have the results of on-line orders processed in several ways: <br />Sent to them via email <br />Stored on the server <br />Sent to an on-line credit card processing company <br />Security Issues --- Orders sent via email <br />FrontPage offers no encryption or other technology for sending encrypted email. Not all store front and shopping cart software offer the option to send encrypted email. Unless the email is encrypted (on the server) prior to being sent, the email will travel through potentially hundreds of servers and be accessible on all of them. <br />The Microsoft Anti-Trust law suit has proven that email, no matter how old, can be retrieved from various servers. <br />So it is crucial, if the merchant chooses to have the order results handled via email, the email be encrypted using technology like <a href="http://www.pgp.com/">PGP</a>. <br />There are many development companies -- we are one such company -- which have the programming staff and experience to enable this technology for merchants. <br />See <a href="http://www.msfrontpage.net/fp/ht-pgpemail.htm">How to use PGP to send encrypted email</a> if you want to tackle the process yourself. <br />Security Issues --- Orders stored on the server <br />Make sure the directory where the file(s) are stored is password protected. <br />Make sure the password for this directory is changed often. <br />Make sure the order information is not kept on the server for long periods of time. <br />If the order information is stored on a database on the server, make sure the billing information has been encrypted. <br />We have seen several cases where the merchant stores the order information in a publicly accessible part on the server where anyone can browse the file. <br />It is crucial that all information be kept on the server only as long as necessary and in a password protected area. If the information will be kept on the server for a long time, then the information should be encrypted. <br />Security Issues --- Orders sent to an on-line credit card processing company <br />This is often the most secure way to handle on-line orders as you, the merchant, never receive the actual credit card number and the monies are automatically deposited to the merchant account. <br />Note: Just as care and planning should be taken when establishing a merchant account, care and planning should be taken when picking an on-line credit card processing company. <br />Dynamic Net, Inc. has worked with several companies, and we are willing to share our experiences with you if you ask. <br />Security Issues --- Digital ID <br />Personally, and as a company, we spend a large amount of money on-line, and will not ever shop at a place that does not use SSL with their own digital id.As an outside instructor at a local college for several Internet courses, I often ask students their on-line shopping habits, and I've yet to encounter one that will shop at an on-line business that does not use SSL (though they are not often as picky in terms of the site having their own digital id).Importance of not using a shared id:Some hosting companies will outright take on the liability of offering to share their ID (yes, there is a huge liability issue here) with their customers in terms of reducing the investment to do business on-line.While the pro is often reduced cost to the on-line business, the cons are many: <br />Company owning the digital id is responsible for any and all fraud committed against the id or any site sharing the id when such fraud involves the id / SSL. <br />The owner of the digital id may not have any policies against sharing the id with companies in the adult industry or other industries where there are huge amounts of credit card fraud. <br />A digital id on the server is one part of a two part key to encrypt the data. When sharing a digital id, how many companies have a copy of this one-part of the key? <br />Some hosting companies put up to 2,000 domains on one server (HiWay is but one example of such a company, and also a company that accepts companies in the adult industry). If but 10% of them are sharing the id, then you have 200 people with a key to the shop (so to speak). Not very secure. <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br />Lecture Notes &#8211;43 <br />Topc: 1. Legal issues in Electronic Commerce <br />There is a widely perceived risk attached to payments made via the Internet, and this perception is in some circumstances justified. This is not like making a phone call or sending a fax. The information sent from the customer to the Web server may pass through many different stages before being delivered. The information is in digital form, and at any stage an unauthorised individual may scan every message looking for credit card numbers (which are easily identified). <br />The difference between this process and a telephone call or fax is that the scanning process can be automated. It is as easy to check every message as to check a single one. <br /><a name="_Toc424612155">Secure Socket Layer (SSL)</a> <br />It is therefore essential that traffic be scrambled (or encrypted), and the standard SSL protocol developed by Netscape provides a high level of protection. The US government views encryption technology as munitions, and therefore the only version of SSL available worldwide is the relatively weak 40-bit version. However, this version is quite strong enough to protect against automated scanning as described above, as it takes over an hour to crack one message. <br />Browsers that support this technology indicate that a secure session is in progress by showing a dialog box, or in the case of Netscape Navigator by showing a blue key on the screen. <br /><a name="_Toc424612156">Beyond the Blue Key</a> <br />Even if the customer is protected by SSL technology, it is clearly important that the information remain secure. <br />Once stored on the Web server, and before being passed to the merchant, the information is at risk from someone breaching security on the server and examining the files. Protection from this can be provided by either: <br /> Encrypting the information stored on the server <br /> Using a &#8216;firewall&#8217; to protect the information. A firewall is a device (or a piece of software) which limits access to a server to specific types, such as &#8216;Web traffic only&#8217;. An important UK acquiring bank (Barclays) insists that credit card data be held behind a firewall. <br />The further stage of sending the information to the credit card processor, and to the merchant must similarly be protected. <br />The &#8216;blue key&#8217; which Netscape Navigator provides to show that a secure session is under way is therefore no guarantee of total security, and the reputation of the merchant (or the payment process) is also important. <br />In an attempt to overcome these weaknesses, the industry has developed the SET specification. SET stands for Secure Electronic Transactions. <br />The SET standard has been developed to protect payment instructions in transit. A discussion of SET is outside the scope of this document, and we recommend that anyone interested in this subject download the SET business description document from (e.g.) the Visa site (http://www.visa.com). <br />SET is expected to become operational in 1998. However progress is slow. For SET to provide the ultimate level of security it will be necessary for each cardholder to be issued a &#8216;digital certificate&#8217; by their credit card issuer. This presents significant logistical problems, and is unlikely to be rolled out in less than 3-4 years. There are a number of unresolved issues here which deserve a paper of their own! <br /><a name="_Toc424612158">Online Authorisation</a> <br /><a name="_Toc424612159">Payment Gateways</a> <br />You may need authorisation to be made on-line: <br /> Because you are delivering the product immediately over the Internet. <br /> Because you want to bypass the manual effort of keying the information into your bank terminal. <br /> Because you want to protect yourself from fraud. Much credit card fraud happens at the merchant. If the credit card information is handled by the computer, the chance of in-house fraud is reduced. <br />Generally speaking, connecting your computer directly to the bank is a very expensive option. You can however go through a number of payment gateway services. There is a list of services on the Textor Webmasters Ltd Web site. <br />These services interface with your Web application in some way to create the link to the bank. <br />If you are using shopping cart software then it has to be interfaced to the gateway. For example we have interfaced our primary product (Shopsite) to a leading gateway service (DataCash). This is never going to be a straightforward thing to do and it is important that when signing up to a gateway service you are sure that they can interface to the software you need. <br /><a name="_Toc424612160">Form-based Services</a> <br />If you are using a simple order form with no shopping cart then the form can possibly be handled by the gateway operation, eliminating the need for a secure server. <br /><a name="_Toc424612161">Commerce Service Providers</a> <br />The most complex type of service is called a Commerce Service Provider (CSP). A CSP offers a complete shopping cart and back-office environment which can be used by a catalogue running on their or another server. The CSP uses complex &#8216;industry strength&#8217; software - of which the market leader is Transact, produced by Open Market. <br />The unique feature of Transact is that the product details can all be held on a catalogue Web site on a non-secure server. They do not have to be held in a product file on the secure server, but can just be built into the Web site. When the customer presses the order button, all the product details are passed to the shopping cart software at that time. What is more, they are passed in a fraud-resistant way guaranteed by security keys. <br />This has a number of important implications, and is one reason why Transact carries a six-figure price tag. One of these implications is that a company can offer Transact as a utility that can be tapped by merchants without requiring any sort of complex product file-maintenance operation. The merchant needs only a simple piece of software to create a security key against each product offering. <br />A further implication is that there is no real upper limit on the size of a catalogue, and because of the strong software involved there are no real transaction volume limits. <br />There are about four Transact-based CSPs in the UK today. Because of the significant investment required these are all major organisations. <br />Shopsite, offered by Textor Webmasters Ltd, is an Open Market product and an interface between Shopsite and Transact is promised. This gives merchants who purchase Shopsite a good expected upgrade path to Transact if the volume of business warrants or if they feel that a major support organisation is required. <br /><a name="_Toc424612162">Planning</a> <br />Planning is an important part of this type of business, as it is with any business venture. We suggest three main stages to the process: <br />Business Requirements <br />Make sure you understand the market, and that you understand the business processes that you need to implement. Select a project manager and ensure that project disciplines are in place. Produce a first-cut budget. <br />Technical Requirements <br />Identify the technical requirements you will need to satisfy. Draw up short lists of products and services. Refine the budget. <br />Selection / Procurement <br />Finally, select the products and services you need to start the project. <br />It may seem obvious but it is important to procure products and services only after the business and technical investigations have been complete. Businesses that start by (for example) selecting a software product or a service provider before the business requirements are clear are in danger of not meeting those requirements. <br /> <br /> <br /> <br /> <br />Lecture Notes &#8211;44 <br />Topc: 1. Security issues in Electronic Commerce <br />Activity Policies, Intelligent Agents and Ethical Transactions <br />Chris W. Higgins, Attorney at LawLane Powell Spears Lubersky LLP <br />Abstract: <br />As governments continue to consider how best to enforce their interests in electronic transactions, technical standards and traditional contract principles will provide a private source of law to govern online transactions. Conceptual, operational and intellectual property issues are raised by electronic commerce, and organizations actively involved in intelligent transactions must conduct legal risk assessment in order to document their information policies. The Activity Policy Association Facilities of ISO/IEC 10744:1997 ("HyTime") represent an international, content-neutral, owner-based system for self-regulating intelligent transactions. <br />Introduction <br />When the World Wide Web Consortium (W3C) officially adopted the eXtensible Markup Language (XML) in mid-February 1998, the Internet finally came to what users of SGML have been enjoying for years, a meta-grammatical modeling language. The difference is, these newcomers brought millions and millions of dollars and users with them. At a time when Microsoft and Sun Microsystems fought each other in a courtroom over the future of Java, the two companies co-sponsored the 1998 XML Conference in Seattle, Washington. Wait a minute. What's going on here? and why? <br />Electronic commerce is what is going on, and there appears to be enough for everyone.<a href="http://www.hytime.org/papers/higgins1.html#ELGIN1#ELGIN1"> [ELGIN1]</a> Companies like Microsoft, Sun Microsystems and Adobe are finally working with SGML veterans like SoftQuad, ArborText and Inso because the information needs generated by electronic commerce are powerfully addressed with the modeling tools of SGML. The whole global information infrastructure could benefit from this fact, as first commercial transactions and then non-commercial transactions are transformed into a rich, rapid and accurate intelligent information stream, and structural innovation becomes a competitive market instead of a birthright of the next business cycle. <br />As the mechanism for mediating the exchange of value between intelligent information systems, meta-grammatical standards like XML and SGML play the same role that the law plays in mediating the exchange of value in the real world. As such, these technical standards act as a source of law for online transactions and therefore must be reliable, detailed and accurate enough for real jurisdictions to rely upon them in addressing the multitude of legal issues generated by electronic transactions. <br />With the passage of only one statute, digital signature laws, the way humans do business will never be the same. For as secure digital commerce evolves, the sheer number of transactions possible predicts that entities will be party to dramatically more contracts. Where there are contracts, there is litigation. Whether, when and how legal institutions will give legal effect to online transactions is the greatest challenge confronting both the legal and technological communities. Even though there are no clear answers, there are some important questions that need to be asked. This paper will attempt to spot the important legal issues generated by electronic commerce, detail the benefit of an international, content-neutral regulatory structure and suggest a semantic framework for understanding and distributing the risks of electronic transactions. <br />Most importantly, it is my desire to generate a dialogue among participants regarding these issues before the next generation of innovation for the global information infrastructure moots many of these questions. There is a tension between governmental bodies and the private sector over the development of a legally-sufficient electronic infrastructure, and so far, governments are waiting to see what the private sector does first. SGML/XML and the standards-based content interchange industry have an opportunity to provide that infrastructure in a vendor-neutral way, while at the same time creating a whole new and level market for semantic information tools. The Internet is a new economic environment with tremendous potential. But if the economic infrastructure of the Internet only mimics current markets, then much of that potential will be lost, and in the end, your PC will look more like the soda machine down the hall then the intelligent access point it can be. <br />Activity Policies: Technical Standards as Private Law <br />All information has structure which may be described in terms of objects and properties, and yet there is never a definitive description of information because users conceptualize information in different ways. Since these differences produce difficulties in exchanging information, exploitation of information regardless of its descriptive structures can only be achieved either by having everyone use the same grammar to conceptualize their information, or by somehow overcoming the structural differences in order to directly access the content. The former approach was attempted with HTML, but finally had to be abandoned because confining all current and future information to a single grammatical structure placed more emphasis on the structure of the processing software system than on the value of the content. The Internet's migration to SGML technology demonstrates that the difficulty of providing ordinary users the ability to individually conceptualize information structures is far outweighed by the value of the free market of ideas created by such expressions. The key value of this technology is its ability to insure that the flexibility and innovation of notation systems necessitated by such expressions is supportable and universally understandable. Now that the freedom to contract electronically is real, the ability to strategically organize information assets is also having a liberating effect on the information processing software market. <br />Since markup is only used to facilitate usage of the content, it should be the power of the appeal of the content and not the particular markup used that determines information's utilization. For this very reason, the most important substantive legal issues raised by specific electronic transactions will be those customarily seen in the same substantive off-line transactions. New issues of first impression are primarily being created by transactions whose online nature in some legally significant way alters the characteristics of the progenitor off-line transaction, as well as transactions which have no off-line progenitor equivalent. Since automatic, or semi-automatic intelligent commercial transactions will be enabled by metadata exchange structures, it is those metadata structures and the data they mark up which will be the focus of legal decision makers' attempts to characterize the nature of these transactions in any dispute resolution. Examining these structures will provide both the context and substance of a specific transaction, whereas a comparison of a party's particular metadata exchange structure versus their usual structure will provide meaningful evidence regarding the intent of the parties. <br />Managers looking to understand, assess and plan for the legal risks of intelligent electronic transactions must incorporate legal risk assessment into a general structural analysis of the information flow in their organization. This analysis will highlight the points of concern and provide an opportunity to design structures that will minimize liability or maximize gain by associating an organization's policies with particular objects, properties or multiple combinations thereof. In this way, an organization can specify what are its policies regarding the whole range of information transactions in which it participates. In HyTime jargon, such policies, regardless of the notations in which they may be expressed, are known as Activity Policies (APs)<a href="http://www.hytime.org/papers/higgins1.html#HYACT#HYACT"> [HyTime1]</a>; each AP is a set of terms and conditions under which an an asset or user may participate in one or more classes of transactions. <br />The more complicated information is, the more detailed its structure and relations will be and the more choices there will be for representing that information and its structure in markup. One task faced by information managers is determining which properties of their information are intrinsic and therefore independent of specific processing applications. For example, the font style and size of this text are independent processing considerations which are not intrinsic to the information, but if this paper contained trademarked symbols, their formatting information might well be intrinsic. Representation strategies normally depend on the expected uses of information assets, on performance considerations, and other factors, and they will vary depending on the specific needs of the informationn owner. When information is encapsulated with meta-information that identifies it, its relations, and its significance, the whole package can be reused for any purpose, in any context and by any application at any future time.<a href="http://www.hytime.org/papers/higgins1.html#HYACT#HYACT">[HyTime1]</a> The principle of inheritance also allows information objects to inherit the structural and semantic characteristics of any other object, so non-intrinsic information is easily and intuitively changed. (This includes easily exchanging complex sets of APs for a certain information asset, e.g. after the sale of a piece of intellectual property the asset could be integrated into the buyer's portfolio by means of inheritance of activity policies from existing assets in that portfolio.) <br />Thus, SGML and XML empower an infinitely rich information stream by creating an object-oriented, value-based process for expressing real language preferences in such a way as to be machine readable. This richness provides the means for an organization not only to plan, track and regulate its transactions through real language directives, but if constructed correctly, the information stream will also establish those directives and the structures which maintain them as a potential source of law for any dispute arising out of any transaction conducted on such a system. Since the starting point of discovery in any litigation is always any expressions by either party regarding their intentions with respect to the transaction in question, organizations with Activity Policies associated with the underlying metadata structure of the actual transaction will fare much better in court than organizations without such structures. Digital signature laws only establish the legality of such transactions by establishing default presumptions and burdens of proof for the humanistic aspects of contract formation, execution or breach in a particular state. But it will be an individual entity's Activity Policies which become the facts upon which these presumptions are established or refuted. Therefore Activity Policies may be the single most valuable tool in addressing legal risks associated with specific electronic transactions. <br />As languages for the creation of notations for expressing Activity Policies, SGML and XML allow for the evolutionary differentiation of information architectures while at the same time removing the obstacles to the free flow of information created by such differentiation. As such, SGML and XML codify the principles of free expression and equal access which are critical to the integrity of cyberspace. By conceptualizing information as objects it is not only possible to provide easier and quicker exchanges of information, but by specifying the types of objects and types of properties associated with each type of object in a model, it becomes possible to provide more specific and more personalized access to information. The power is tremendous, and the opportunities for fraud numerous. Participants must compete with each other, and yet they must each also retain some obligations to maintain the integrity of the structures which facilitate electronic commercial transactions. These structures must describe diverse and dynamic transactions depending on context or user-specified preferences, and yet still allow pinpoint access based on semantic queries to specific transactional records. By providing reliable and accurate access to proper documentation, organizations may use Activity Policy Associations to more effectively address the legal issues raised by the transactions in which they are involved. In this way, the technical standards used to describe the meta-grammatical models for the expression and exchange of information can become a source of law. <br /> <br />--------------------------------------- The End ---------------------------------------------- <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <div style='clear: both;'></div> </div> <div class='post-footer'> <div class='post-footer-line post-footer-line-1'> <span class='post-author vcard'> Posted by <span class='fn' itemprop='author' itemscope='itemscope' itemtype='http://schema.org/Person'> <meta content='https://www.blogger.com/profile/11555648113844076167' itemprop='url'/> <a class='g-profile' href='https://www.blogger.com/profile/11555648113844076167' rel='author' title='author profile'> <span itemprop='name'>Harish</span> </a> </span> </span> <span class='post-timestamp'> at <meta content='http://kumarharishji.blogspot.com/2009/01/web-tools-cource-material.html' itemprop='url'/> <a class='timestamp-link' href='http://kumarharishji.blogspot.com/2009/01/web-tools-cource-material.html' rel='bookmark' title='permanent link'><abbr class='published' itemprop='datePublished' title='2009-01-29T22:09:00-08:00'>10:09&#8239;PM</abbr></a> </span> <span class='post-comment-link'> </span> <span class='post-icons'> <span class='item-control blog-admin pid-2074053637'> <a href='https://www.blogger.com/post-edit.g?blogID=3024449944147478995&postID=4730415215854353603&from=pencil' title='Edit Post'> <img alt='' class='icon-action' height='18' src='https://resources.blogblog.com/img/icon18_edit_allbkg.gif' width='18'/> </a> </span> </span> <div class='post-share-buttons goog-inline-block'> </div> </div> <div class='post-footer-line post-footer-line-2'> <span class='post-labels'> </span> </div> <div class='post-footer-line post-footer-line-3'> <span class='post-location'> </span> </div> </div> </div> <div class='comments' id='comments'> <a name='comments'></a> <h4>No comments:</h4> <div id='Blog1_comments-block-wrapper'> <dl class='avatar-comment-indent' id='comments-block'> </dl> </div> <p class='comment-footer'> <a href='https://www.blogger.com/comment/fullpage/post/3024449944147478995/4730415215854353603' onclick=''>Post a Comment</a> </p> </div> </div> </div></div> </div> <div class='blog-pager' id='blog-pager'> <span id='blog-pager-newer-link'> <a class='blog-pager-newer-link' href='http://kumarharishji.blogspot.com/2009/01/question-bank-of-web-tools.html' id='Blog1_blog-pager-newer-link' title='Newer Post'>Newer Post</a> </span> <span id='blog-pager-older-link'> <a class='blog-pager-older-link' href='http://kumarharishji.blogspot.com/2009/01/syllabus-of-web-tools.html' id='Blog1_blog-pager-older-link' title='Older Post'>Older Post</a> </span> <a class='home-link' href='http://kumarharishji.blogspot.com/'>Home</a> </div> <div class='clear'></div> <div class='post-feeds'> <div class='feed-links'> Subscribe to: <a class='feed-link' href='http://kumarharishji.blogspot.com/feeds/4730415215854353603/comments/default' target='_blank' type='application/atom+xml'>Post Comments (Atom)</a> </div> </div> </div></div> </div> <div id='sidebar-wrapper'> <div class='sidebar section' id='sidebar'><div class='widget BlogArchive' data-version='1' id='BlogArchive1'> <h2>Blog Archive</h2> <div class='widget-content'> <div id='ArchiveList'> <div id='BlogArchive1_ArchiveList'> <ul class='hierarchy'> <li class='archivedate expanded'> <a class='toggle' href='javascript:void(0)'> <span class='zippy toggle-open'> &#9660;&#160; </span> </a> <a class='post-count-link' href='http://kumarharishji.blogspot.com/2009/'> 2009 </a> <span class='post-count' dir='ltr'>(5)</span> <ul class='hierarchy'> <li class='archivedate expanded'> <a class='toggle' href='javascript:void(0)'> <span class='zippy toggle-open'> &#9660;&#160; </span> </a> <a class='post-count-link' href='http://kumarharishji.blogspot.com/2009/01/'> January </a> <span class='post-count' dir='ltr'>(5)</span> <ul class='posts'> <li><a href='http://kumarharishji.blogspot.com/2009/01/tutorial-web-tools.html'>Tutorial Web-Tools</a></li> <li><a href='http://kumarharishji.blogspot.com/2009/01/assignment-of-web-tools.html'>Assignment of Web-Tools</a></li> <li><a href='http://kumarharishji.blogspot.com/2009/01/question-bank-of-web-tools.html'>Question Bank of Web-Tools</a></li> <li><a href='http://kumarharishji.blogspot.com/2009/01/web-tools-cource-material.html'>Web-Tools Cource Material</a></li> <li><a href='http://kumarharishji.blogspot.com/2009/01/syllabus-of-web-tools.html'>Syllabus of Web-Tools</a></li> </ul> </li> </ul> </li> </ul> <ul class='hierarchy'> <li class='archivedate collapsed'> <a class='toggle' href='javascript:void(0)'> <span class='zippy'> &#9658;&#160; </span> </a> <a class='post-count-link' href='http://kumarharishji.blogspot.com/2008/'> 2008 </a> <span class='post-count' dir='ltr'>(4)</span> <ul class='hierarchy'> <li class='archivedate collapsed'> <a class='toggle' href='javascript:void(0)'> <span class='zippy'> &#9658;&#160; </span> </a> <a class='post-count-link' href='http://kumarharishji.blogspot.com/2008/11/'> November </a> <span class='post-count' dir='ltr'>(1)</span> </li> </ul> <ul class='hierarchy'> <li class='archivedate collapsed'> <a class='toggle' href='javascript:void(0)'> <span class='zippy'> &#9658;&#160; </span> </a> <a class='post-count-link' href='http://kumarharishji.blogspot.com/2008/07/'> July </a> <span class='post-count' dir='ltr'>(3)</span> </li> </ul> </li> </ul> </div> </div> <div class='clear'></div> </div> </div><div class='widget Profile' data-version='1' id='Profile1'> <h2>About Me</h2> <div class='widget-content'> <a href='https://www.blogger.com/profile/11555648113844076167'><img alt='My photo' class='profile-img' height='45' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHTd3a0aSA_nmMN26GPy2-6pvhh6vbMpOpFjBfRq_us-3X9Hs7vk_yM_sd6H2IjRiZ8HLaGPgr_uZjoyP0KhdWK4CMHweUr6-Fjm-WuK6zlOnExETnWCM0kNzkL5GFaJ0/s220/Harish2.bmp' width='80'/></a> <dl class='profile-datablock'> <dt class='profile-data'> <a class='profile-name-link g-profile' href='https://www.blogger.com/profile/11555648113844076167' rel='author' style='background-image: url(//www.blogger.com/img/logo-16.png);'> Harish </a> </dt> <dd class='profile-textblock'>Simple nature, able to live in different situation.</dd> </dl> <a class='profile-link' href='https://www.blogger.com/profile/11555648113844076167' rel='author'>View my complete profile</a> <div class='clear'></div> </div> </div></div> </div> <!-- spacer for skins that want sidebar and main to be the same height--> <div class='clear'>&#160;</div> </div> <!-- end content-wrapper --> <div id='footer-wrapper'> <div class='footer no-items section' id='footer'></div> </div> </div></div> <!-- end outer-wrapper --> <script type="text/javascript" src="https://www.blogger.com/static/v1/widgets/387437488-widgets.js"></script> <script type='text/javascript'> window['__wavt'] = 'AEUoTZpLZ3ZwwySOAINc67swS0eh:1781010814904';_WidgetManager._Init('//www.blogger.com/rearrange?blogID\x3d3024449944147478995','//kumarharishji.blogspot.com/2009/01/web-tools-cource-material.html','3024449944147478995'); _WidgetManager._SetDataContext([{'name': 'blog', 'data': {'blogId': '3024449944147478995', 'title': 'Harish', 'url': 'http://kumarharishji.blogspot.com/2009/01/web-tools-cource-material.html', 'canonicalUrl': 'http://kumarharishji.blogspot.com/2009/01/web-tools-cource-material.html', 'homepageUrl': 'http://kumarharishji.blogspot.com/', 'searchUrl': 'http://kumarharishji.blogspot.com/search', 'canonicalHomepageUrl': 'http://kumarharishji.blogspot.com/', 'blogspotFaviconUrl': 'http://kumarharishji.blogspot.com/favicon.ico', 'bloggerUrl': 'https://www.blogger.com', 'hasCustomDomain': false, 'httpsEnabled': true, 'enabledCommentProfileImages': true, 'gPlusViewType': 'FILTERED_POSTMOD', 'adultContent': false, 'analyticsAccountNumber': '', 'encoding': 'UTF-8', 'locale': 'en', 'localeUnderscoreDelimited': 'en', 'languageDirection': 'ltr', 'isPrivate': false, 'isMobile': false, 'isMobileRequest': false, 'mobileClass': '', 'isPrivateBlog': false, 'isDynamicViewsAvailable': true, 'feedLinks': '\x3clink rel\x3d\x22alternate\x22 type\x3d\x22application/atom+xml\x22 title\x3d\x22Harish - Atom\x22 href\x3d\x22http://kumarharishji.blogspot.com/feeds/posts/default\x22 /\x3e\n\x3clink rel\x3d\x22alternate\x22 type\x3d\x22application/rss+xml\x22 title\x3d\x22Harish - RSS\x22 href\x3d\x22http://kumarharishji.blogspot.com/feeds/posts/default?alt\x3drss\x22 /\x3e\n\x3clink rel\x3d\x22service.post\x22 type\x3d\x22application/atom+xml\x22 title\x3d\x22Harish - Atom\x22 href\x3d\x22https://www.blogger.com/feeds/3024449944147478995/posts/default\x22 /\x3e\n\n\x3clink rel\x3d\x22alternate\x22 type\x3d\x22application/atom+xml\x22 title\x3d\x22Harish - Atom\x22 href\x3d\x22http://kumarharishji.blogspot.com/feeds/4730415215854353603/comments/default\x22 /\x3e\n', 'meTag': '', 'adsenseHostId': 'ca-host-pub-1556223355139109', 'adsenseHasAds': false, 'adsenseAutoAds': false, 'boqCommentIframeForm': true, 'loginRedirectParam': '', 'isGoogleEverywhereLinkTooltipEnabled': true, 'view': '', 'dynamicViewsCommentsSrc': '//www.blogblog.com/dynamicviews/4224c15c4e7c9321/js/comments.js', 'dynamicViewsScriptSrc': '//www.blogblog.com/dynamicviews/ff2fc60a7f0d8424', 'plusOneApiSrc': 'https://apis.google.com/js/platform.js', 'disableGComments': true, 'interstitialAccepted': false, 'sharing': {'platforms': [{'name': 'Get link', 'key': 'link', 'shareMessage': 'Get link', 'target': ''}, {'name': 'Facebook', 'key': 'facebook', 'shareMessage': 'Share to Facebook', 'target': 'facebook'}, {'name': 'BlogThis!', 'key': 'blogThis', 'shareMessage': 'BlogThis!', 'target': 'blog'}, {'name': 'X', 'key': 'twitter', 'shareMessage': 'Share to X', 'target': 'twitter'}, {'name': 'Pinterest', 'key': 'pinterest', 'shareMessage': 'Share to Pinterest', 'target': 'pinterest'}, {'name': 'Email', 'key': 'email', 'shareMessage': 'Email', 'target': 'email'}], 'disableGooglePlus': true, 'googlePlusShareButtonWidth': 0, 'googlePlusBootstrap': '\x3cscript type\x3d\x22text/javascript\x22\x3ewindow.___gcfg \x3d {\x27lang\x27: \x27en\x27};\x3c/script\x3e'}, 'hasCustomJumpLinkMessage': false, 'jumpLinkMessage': 'Read more', 'pageType': 'item', 'postId': '4730415215854353603', 'postImageUrl': 'constr4.gif', 'pageName': 'Web-Tools Cource Material', 'pageTitle': 'Harish: Web-Tools Cource Material'}}, {'name': 'features', 'data': {}}, {'name': 'messages', 'data': {'edit': 'Edit', 'linkCopiedToClipboard': 'Link copied to clipboard!', 'ok': 'Ok', 'postLink': 'Post Link'}}, {'name': 'template', 'data': {'isResponsive': false, 'isAlternateRendering': false, 'isCustom': false}}, {'name': 'view', 'data': {'classic': {'name': 'classic', 'url': '?view\x3dclassic'}, 'flipcard': {'name': 'flipcard', 'url': '?view\x3dflipcard'}, 'magazine': {'name': 'magazine', 'url': '?view\x3dmagazine'}, 'mosaic': {'name': 'mosaic', 'url': '?view\x3dmosaic'}, 'sidebar': {'name': 'sidebar', 'url': '?view\x3dsidebar'}, 'snapshot': {'name': 'snapshot', 'url': '?view\x3dsnapshot'}, 'timeslide': {'name': 'timeslide', 'url': '?view\x3dtimeslide'}, 'isMobile': false, 'title': 'Web-Tools Cource Material', 'description': 'Lecture Notes \u2013 1\r Topic: Unit -I\r Unit -II\r Unit -III\r \r \r Unit \u2013 I\r Introduction to HTML/DHTML/XML/Java scriptin...', 'featuredImage': 'https://lh3.googleusercontent.com/blogger_img_proxy/AEn0k_sk0FkNrENAykwO578h6y1_UFijlBAVwpyNUDkhqWQAo80C1sSmCkmPK_WRIKOM6Kwv3Q', 'url': 'http://kumarharishji.blogspot.com/2009/01/web-tools-cource-material.html', 'type': 'item', 'isSingleItem': true, 'isMultipleItems': false, 'isError': false, 'isPage': false, 'isPost': true, 'isHomepage': false, 'isArchive': false, 'isLabelSearch': false, 'postId': 4730415215854353603}}]); _WidgetManager._RegisterWidget('_NavbarView', new _WidgetInfo('Navbar1', 'navbar', document.getElementById('Navbar1'), {}, 'displayModeFull')); _WidgetManager._RegisterWidget('_HeaderView', new _WidgetInfo('Header1', 'header', document.getElementById('Header1'), {}, 'displayModeFull')); _WidgetManager._RegisterWidget('_BlogView', new _WidgetInfo('Blog1', 'main', document.getElementById('Blog1'), {'cmtInteractionsEnabled': false, 'lightboxEnabled': true, 'lightboxModuleUrl': 'https://www.blogger.com/static/v1/jsbin/1053750561-lbx.js', 'lightboxCssUrl': 'https://www.blogger.com/static/v1/v-css/828616780-lightbox_bundle.css'}, 'displayModeFull')); _WidgetManager._RegisterWidget('_BlogArchiveView', new _WidgetInfo('BlogArchive1', 'sidebar', document.getElementById('BlogArchive1'), {'languageDirection': 'ltr', 'loadingMessage': 'Loading\x26hellip;'}, 'displayModeFull')); _WidgetManager._RegisterWidget('_ProfileView', new _WidgetInfo('Profile1', 'sidebar', document.getElementById('Profile1'), {}, 'displayModeFull')); </script> </body> </html>