Professional Documents
Culture Documents
Core Java
Core Java
INTRODUCTION:
Gosling’s solution to the problems of C++ was a new language called Oak. Oak
preserved the familiar syntax of C++ but omitted the potentially dangerous features like
explicit resources references, pointer arithmetic, and operator overloading. Oak
incorporated memory management directly into language, freeing the programmer to
concentrate on the tasks to be performed by the program. To be successful as on
embedded systems programming language, Oak needed to be able to respond to real
- world events within microseconds. It also needed to be portable; that is, it had to be
able to run on a number of different microprocessor chips and environments. This
hardware independence would allow a toaster manufacturer to change the chip used to
run the toaster without changing the software. The manufacture could also use some of
the same code that the toaster to run a similar appliance, such as a toaster oven. This
would cut down on development and hardware cost, as well as increase reliability.
As Oak matured, the Word Wide was growing dramatically, and the development
team at sun realized Oak was perfectly suited to Internet programming. Thus in 1994,
they completed work on a product known as Web Runner, an early Wed browser
written in Oak. Web Runner was later renamed Hot Java, and it demonstrated the
power of Oak as an Internet development tool. Hot java is well versions under
development.
Finally, in 1995, Oak was renamed java (for marketing and legal reason) and
introduced at Sun Word 95, since then, java’s rise in popularity has been meteoric.
Even before the first release of the java compiler in January ‘96, the development
community considered java a standard for Internet development.
JAVA ARCHITECTURE:
1
As write other programming languages, java uses a compiler to convert human
readable code into executable program.
This byte code can be executed by java virtual machine JVM, which is java
architecture implemented in s/w rather than h/w. java byte files are called class files,
because they contain single java class. To execute java byte codes, the JVM uses
class reader to files the byte code from a disk or a n/w. each class files is fed to byte
code verifier to check the class whether it is formatted correctly. The execution unit of
JVM carries out the instructions specified in the byte code. This is an interpreter, which
is a program that reads the byte codes, interprets that meaning and then performs the
associated function.
2
OOPS
Class Inheritance:
- Like technique, we can inherit both data members and methods of an thirsty
class into newly creation class.
- The class, which is inherited, is called super class and the newly derived class is
called as sub class.
- We can early override the method of the parent class by regularly the method in
the sub class.
3
- For this inheritance, we use extends clause such as
- Class B extends A
- {
- }
- For a given sub class, there should be only one super class i.e. only hierarchical
inheritance is allowed in Java.
1. Members reference
- Local variable is a method can share the same houses as instance variables as
class variables.
- Sub class can define there own instance variables to hide those defined in this
super class.
- Sub classes can also define methods to override the method, defined in their
super class.
- Two special reference variables are available inside any instance method to
allow for accessing the hidden or overridden methods of that instance i.e.
- This - it is used to refer the object, the method is called upon i.e. owner
object.
- Super - it is used to access the methods or data members defined in the
super class.
Ex. – 1. class A
{ int x1;
int x2;
void f1 (int x 1, int x2)
{this .x1= x1;
this .x2= x2;
4
}
}
Ex : 2 – class B extends A
{
int x1;
int x2:
void f2(int x 1, int x2)
{
this .x1=super.x1=1;
this .x2=super .x2= x2;
}
}
2. As constructor reference
Ex: class A
{
int x1;
int x2;
A (int x 1)
}
this .x1=x1;
{
A (int x1, int x2)
{
5
this (x1);
this .x2= x2;
}
}
{
int x3;
B (int x 1, int x2, int x3)
{
super(x1, x2);
this .x3= x3;
}
}
oa .f1 () // A’s x ()
oa .f1 () // B’s x ()
oa = ob;
oa.f1 // B,s f1 ()
6
Constructors and Finalize
1. Constructors, i.e. the method, that return new instance of the class. If we do not
define a constructor, we can use the default constructor to create an instance of a
class.
2. Finalizers i.e the that are called just before an object in garbage collected.
- The default constructor is automatically added to the class by the java compiler.
- The constructor allocates storage memory for any member variables that are declared
on Java’s built in data types.
- A contractor is defined in the same way as an ordinary method, but it must have the
same name as the class and have no return type.
- We can define any number of constructors for a class by having different parameter list
and this called constructors overloading.
- We can define any no. of methods for a class with the same and different parameter list
and this is called method overloading.
- Java virtual machine automatically retains the memory used by an object when no
variable is referred to that object.
- The process is know as garbage collection.
- Finalizers are used in a situation where a class needs to clean itself before the garbage
collection.
- Finalizers are called just before an object of class is garbage collector.
- Finalizers are used to close opened files or connections to unsure that the related taxes
are completed before the object is garbage collected.
- Example definition of this finalize method is
Void finalize ()
{
-
-
-
}
7
Examples 1:
Class A
{
int x 1 = 10;
int x 2 = 20;
int x3 = 30;
A(int x1)
{
this.x1=x1 ;
}
A(int x 1,int x2)
{
this(x1);
this.x2=x2;
}
A(int x 1 ,int x2, int x3)
{
this(x1,x2);
this.x3=x3;
}
void display()
{
system.out.println(x1);
system.out.println(x2);
system.out.println(x3);
}
public static void main(String args[]}
{
A oa1=new A(11);
Oa1 .display();
A oa2=new A(11, 12);
Oa2 .display();
A oa3=new A(11,12,13);
Oa3 .display();
}
}
8
Examples 2:
Class A
{
int x 1 = 10;
int x 2 = 20;
int x3 = 30;
void display()
{
system.out.println(x1);
system.out.println(x2);
system.out.println(x3);
}
void adisplay()
{
System.out.println(‘this is A display”);
display();
}
}
class B extends B extends A
{
int x 1 = 40;
int x 2 = 50;
int x3 = 60;
void display()
{
system.out.println(x1);
system.out.println(x2);
system.out.println(x3);
}
void bdisplay()
}
System.out.println(‘this is A display”);
display();
}
}
class overrideex2
9
{
public static void min ( String args[])
{
A oa=new A();
oa. display();
oa. adisplay();
System.out.printin(oa.x1);
System.out.printin(oa.x2);
oa=new B();
oa.dispaly ();
oa.adispaly ();
//oa.bdisplay();
system.out.println(oa.x1);
system.out.println(oa.x2);
system.out.println(oa.x3);
system.out.println(oa.x4);
A oa1=new A();
oal .adisplay B();
B ob=new B()
ob. adisplay();
ob. bdisplay();
}
}
Examples 3:
Class A
{
int ax1= 10;
class B
}
int bx1= 20;
void display()
}
system.out.println(ax1);
system.out.println(bx1);
}
}
10
static class C
{
int cx1= 30;
void display()
{
system.out.println(cx1);
//system.out.println(ax1);
}
}
static void adisp()
{
C oc=new C ();
Oc.cdisplay();
B ob=new B();
System.out.println(ax1);
//System.out.println(bx1);
}
}
class staticdemo2
{
public static void main(String args[])
{
A oa=new A ();
Oa.adisplay();
//B ob=new B();
//A.B oab=new A.B();
A.C oc=new A.C()
oc.cdisplay();
}
}
Examples 4:
abstract class A
{
int ax1= 10;
int ax1= 20;
void set(int p1,int p2)
11
{
ax1=p1;
ax1=p2;
}
abstract void display();
}
class B extends A
{
int bx1=30;
void display()
{
system.out.println(ax1);
system.out.println(ax2);
system.out.println(bx1);
}
}
class C extends A
{
int cx1=40;
int cx2=50;
void display()
{
system.out.println(ax1);
system.out.println(ax2);
system.out.println(cx1);
system.out.println(cx2);
}
}
class abstractex
{
public static void main(String args[])
{
//A oa=new A();
B ob=new B();
Ob.display();
C oc=new C();
oc.display();
12
}
}
A modifier is a keyword that affects either the lifetime or the accessibility of a class, a
variable, or a member function.
The following sections describe the storage and lifetime modifiers: abstract, static,
synchronized, native,
When applied to a class, the abstract modifier indicates that the class has not been fully
implemented and the that is should not be instantiated. If applied to member function
declaration, the abstract modifier means that the function will be implemented in a subclass.
Since the function has no implantation, the class cannot be instantiated and must be declared
as abstract. Interfaces are abstract by default.
Ordinarily, each instance of a class has its own copy of any member variables.
However, it is possible to designate a member variable as belonging to the class itself,
independent of any objects of that class. Such member variables are called static members
and are declared with the static modifier keyword. Static member variables are called static
members and are declared with the static modifier keyword. Static member variables are often
used when tracking global information about the instances of a class. The following class
tracks the number of itself using a static member variable called instance Count:
13
// increment the instance counter is called,
InstanceCoung++;
}
static {
instanceCount = 0 :
}
}
A synchronized member function allows only one thread to execute the function at a
time. This prevents two threads of execution from undoing each other’s work.
For example, suppose you have two threads, called threads A and B, responsible for
updating a bank balance. Suppose also that the account has $100 in it. Now, simultaneously,
thread a tries to deposit $50 in the account, while thread B find it at $100. Each of them,
independently, adds its deposit to the old sum and $150. If thread B finishes last, the account
should have $225 in it (100+50+75). The problem is that both threads tried to execute the
same code (querying and changing the balance) at the same time. This exactly the scenario
that scenario that the synchronized keyword can prevent.
Synchronized methods are not static by default, but they may be declared as static.
The synchronized modifier does not apply to classes or member variables.
Native methods are implemented in other Language, such as C, so they have no code
block. Many of the classes in Core API are native because they need to access operating
system-specific routines, such as those for drawing graphics on the screen here in an excerpt
from the API’S math class:
/**
* Returns the trigonometric sine of an angles.
* @ param a an assigned angle that is measured in radians
*/
public static native double sin (double a);
14
This declaration calls a function in a native code libarary that calculates the sine of the
angle a. on a Intel x86platfrom, the native code would call the sine function in the x86
processor’s floating-point unit or coprocessor. On other platforms, the native code function
may do the computation with software instead. This function also happens to be declared with
the public and static modifiers also.
A volatile variable is one whose value may change independent of the Java program
itself. Typically, volatile represent input from the outside world, such as a variable that denotes
the time of day. They are also used to flag variables that could be changed by other threads of
execution. The volatile keyword will prevent the compiler from attempting to track changes to
the variable. The variable will always be assumed by the compiler to have a (potentially) new
value each it is accessed by Java code. Use of this modifier is rare but necessary, as Just-in-
time (JIT) compliers are now common in most Java Virtual Machines.
One of the changes introduced with Java 1.1.is adding meaning to the transient
modifier. Java 1.0 supported the modifier, but it had no purpose. It is used in conjunction with
serialization to provide for persistent objects. These objects can be aved to disk and restored
on another machine or on the same machine. For more information about serialization, see
Chapter 19. The transient modifier means not to save the variable.
Most languages have a way to declare a variable as consuant (that is, unchangeable),
which is true of Java as well. The final keyword indicates that a local variable or member
variable connot be altered. The main use of final variables is as symbolic constants. You can
refer to a constant by name and define that name in a singe location in you code. If you later
need to change the number in your code, you need only make the change at the final variable
is defined.
Note that if you declare a variable a variable as final, you must also initialize it at the
same time:
final int MAX_PAGES =23;
15
16
Accessibility Modifiers:
Java has other modifiers used to change the accessibility of classes and their members
to there classes. By default, a class, its member functions, and its variables are know only to
other classes in the same package. For simple applets, this means that a class is accessible
only to other classes in the same directory.
Public:
A public class can be accessed by any other class. When a VM is asked to execute a
new applet, the class for the applet must be public. However, any other classes required by
the applet need not be public, long as they are accessible.
Methods and member variables of public classes that are themselves declared as
public can be accessed by code form other classes. Public members can also be accessed by
JavaScript, VBScript, and ActiveX controls operating outside the VM. If members of a
nonpublic (default) class are declared as public, those members will be accessible to all other
classes within the same package. As a guideline, a void defining, methods and variables as
public unless it is necessary.
If no modifier is specified, the default accessibility for the class, variable, or method is
assumed.
Private:
The private modifier restricts access to member of a class so that no other classes can call
member functions or directly access member variables.
Protected:
A protected member is similar to default access but gives preferred access for subclasses in
other packages. Member functions are some times created within a class for utility purposes
(that is, to be used only within the class itself and not for general consumption). Declaring
utility functions as protected allows them to be used not only be the class itself but its
subclasses as well.
17
Object Memory Model:
- The dynamically charging part of a program can divided into two areas i.e
1. Stack memory area, which is used to store the local variables in methods, and blocks.
2. These variables are popped out of this stack upon list from the enclosing method or
blocks.
3. A heap memory area, which is used the store the memory for dynamically allocated
objects.
- References to the objects are placed in the Stack area, but the space for his data
members of the objects will reside in this heap area.
- Whenever a block of memory calls is no longer referred by any reference variables,
then these unused calls can be freed or garbage collected.
Static Members:
- It is possible to care a member that can be used by itself, without reference to specific
instance i.e. object of a class.
- To create such a member, we precede its declaration with the keyword static.
- When a member is declared static, it can be accessed before any objects of its class
are created and without reference to any object.
- We can declare both methods and variables as object.
- The variables, which are declared inside the class, are called class variables, which are
the global variables.
- All the instances of the class share tha same static variables.
- The method, declared as static have the following restrictions i.e
1. They can only call other static methods.
2. They must only access static data.
3. They cannot refr to this or super variables.
- We can declare a static block inside the class, which gets executed once when the
class is loaded.
- Outside the class, the static method, and static variable can be used in dependant of
any object.
- These static members can also be referenced through an object reference.
- But, non-static members must be referenced through only object reference. i.e. they
cannot be referenced with the class name.
- Inside the non-static methods, we can refer both static and non static members of the
class.
18
Nested and Inner Classes:
- It is possible to define a class within another class; such classes are called as nested
classes or inner classes.
- The scope of a nested class is bonded by the scope of its enclosing classes. I.e. if B
class is defined inside the class A, then B is know to only A, but not outside of A.
- A nested class has access to the members of its unclosing class including private
member.
- But enclosing class i.e. outer class does not have access to the members of the nested
class i.e. inner class.
- There are two types of nested classes. I.e static and non static.
- The static nested class is the one, which is defined with static modifier.
- The static nested class cannot refer the members of outer class directing i.e. it must
access through an object.
- The non-static nested class can access all the method, and variable of its outer class
directly.
- If the inner class is static, then it must be used to create an object outside of the
containing class with a hierarchical name as shown below.
- If the inner class is static, then it must be used to create an object outside of the
containing class with a hierarchical name as show below.
class A
{
static class B
{
}
}
class C
{
public static void main (String args [])
{
A. B ob=new A.B ();
-----
-----
}
}
19
Abstract class and abstract methods
- Some times we want create a super class that only defines generated from that will be
sha ed by all of its sub class to fill the details.
- Such a class, which is not complete with the implementation of all the methods, is
called abstract and it should specify with abstract modifier.
- The methods which are incomplete in the abstract class are called abstract methods.
- The declaration of abstract methods in the abstract class be specified with abstract as
shown below.
abstract class A
{
void f1 ()
{
------------
----------
------------
}
abstract void f2();//abstract method
}
- If there is one single abstract method is a class, then the class must be declared as
abstract.
- These abstract classes cannot be used to create the objects. But they can be used to
inherit into the subclasses.
- The subclasses must compulsorily implement the abstract methods of the inherited
super class; otherwise the sub class will be come as abstract class.
- We cannot declare constructors and abstract static methods of a class.
- Abstract classes can be used to create object references, so that the super class
reference variable can refer to can objects of its subclasses.
-- This final modifier is used to prevent overriding i.e the methods declared as final cannot be
overridden in the subclass. As shown below.
20
class A
{
---
----
-----
}
}
class B extends A
{
void f1() //error, can’t override
{
}
}
final class A
{
------
. ------
------
}
21
JAVA BULT – INDATA TYPES
The compiler itself, without reference to any libraries or the Core APL, understands
Java’s built-in data types can be classified into numeric, Boolean, and character data types.
Before using my variable, it must first be declared. A variable declaration specifies the
data type, the variable name, and, optionally, the default value for the variable. The following
sections describe variable declarations, each of the basic datatypes, and classes declarations.
Variable Declarations:
Int I;
Int j;
Int k;
J=1;
J=2;
K=3
or in an abbreviated from:
int I=2,j=2,k=3;
You can place variable declarations any where in your code, as long as they precede
the first use of the variable. However, it is common practice to place the declarations at the top
of each block of code. This makes your code easier to read, especially for programmers who
are used to languages that requages that require you to declare your variables at the
beginning of functions. Later in this chapter, you will see how to limit across to a variable in its
declaration.
22
The Numeric Datatypes:
Java has six numeric datatypes that differ in the size and precision of the numbers they can
hold. The basic numeric datatypes are listed in the table.
TABLE
23
Interfaces
-- By using the keyword interface, we can specify what a class must do, but not how to do
-- Interfaces are syntactically similar to classes, but no in variables and methods are declared
without any body.
-- Once an interface is defined, then any no. of classes can implement this interface or one
class can implement any no. of interfaces.
-- To implement an interface, a class must define or implement the complete set of methods
declared in the interface as shown below.
interface iface 1
{
void f1();
void f2();
}
interface iface2
{
void f3();
}
class A implement I face 1
{
public void f1 ()
{
--------
--------
}
public void f2 ()
}
--------
--------
}
}
class B implements I face 1, iface2
{
public void f1 ()
{
24
-------
------
}
public void f2()
{
--------
--------
public void f3()
{
--------
--------
}
}
-- The method, that implement a interface must be declared public and also the type signature
of the implementing method must math abstractly with the type signature specified in the
interface declaration.
-- If class implements two interfaces that declare the same methods, then the same method
will be used of with either the interface.
-- We can declare variables of the interfaces as object references that use an interface.
-- Any instance of any class that implements the interface can be stored in such interface
variables.
-- When we call a method through the interface reference variable, the correct version will be
called based on the actual instance of the interface being referenced to and this one of the
key feature of interface.
-- This process is similar to using a super class reference to access a sub class object.
-- If a class implements interface, butt does not fully implement this method, deferred by that
interface, then that class must be declared as abstract.
-- We can use this variables in the interface, to have shared constants onto multiple classes by
simply declaring an interface that contains variables which are initiated to the desired to the
desired value.
-- One interface can inherit another by using the key and extend, such as
-- Interface iface3 extends iface , iface2.
-- Interface iface3 extends ifacel 1 , iface 2.
-- When a class implements such interface, it must provide implementation for all the methods
defined which the interface inheritance chain.
25
Examples 1:
interface iface 1
{
void display();
}
interface iface 2
{
void display();
void decrx();
}
interface iface 3 extends iface 1, iface 2
{
void display(int p1);
}
class A implements iface 3
}
Int x1 = 10;
Int x2=20;
Public void display()
{
System.out.println(x1);
System.out.println(x2);
}
public void set(intp 1)
{
x1=x2=p1;
}
public void incrx 1()
{
x1++;
}
public void decrx 1 ()
26
{
x1++;
}
}
class interfaceex2
{
public static void main(Sting args[]}
{
Iface3 face3;
face3=new A();
face3.incrx1;
face3.display;
face3.set(11);
face3.dispaly();
face3.decraxl();
face3.display();
27
//face3=new B();
iface 1 face1;
B ob=new B();
face1=ob;
face1 .dispaly();
//face1.incrx1();
iface2 face2;
face2=ob;
face2.incrx1();
ob.display();
28
Packages
- Packages are containers for classes that are used to keep the class name
space.
- A unique same has to be used for each class to child name continues in the
same name open.
- The package is a both name visibility control mechanism.
- We can define classes inside a package that are not accessible by code outside
that package.
- We can also define member, that are only exposed to other members of the
same package.
- This allows the classes to have knowledge of each other, but not expose that
knowledge to the rest of the world.
- To create a package, we simply include package as the first statement in a java
source file.
- Any classes declared within that file belong to the specified package, i.e.
package statement defines a name space in which class are stored.
- If we omit the package statement, the class names are placed in the default
package.
- Example for this package statement is
Package my pack;
- Java uses file system directories to store packages and the directory name must
match with the package name.
- Here that one file can include the same package statement i.e the package
statement simply specifies to which package the classes defined in a file belong
to package.
- The package hierarchy must reflect the file system i.e. the java had to be stored
in Naidul my pack directory.
- CLASS PATH environmental variable is used to specify the specific location that
the java compiler to combine for the class reference.
- The default current working directory is usually set for the class path
environmental variable defined for the java runtime system.
- In Java, we use import statement to being certain class or package into visibility
such as
29
Import packagename;
- This package name can be single name or hierarchal name.
- This import statement should be given used to package statements if any and
before any class definitions.
- The examples of this import statement are
Import mypack .A; // for class A of package mypack
Import Naidu.*;// for all the classes of package Naidu
- If a class with the same name consists in two different packages that we import,
then the compiler will generate an error when we try to use that class import,
then the compiler will generate an error when we try to use that class name. in
such class we should specify the fully qualified name such as
Mypack.A oa = new mypack.A();
- When a package is imported, only those items which the package are declared
as public. Will be available in the import code.
- Java’s access controlled mechanism is performed with access specifier of
private, public, protected and default.
- Private member of a class can be accused only in other member of a class.
- Packages add another dimension to those access specifies and they are shown
in the following table.
Same pack
Non sub class No Yes Yes
Yes
Same pack
Sub class No Yes Yes
Yes
Different park
Non sub class No Yes Yes
Yes
Different pack
Sub class No Yes Yes
Yes
30
Examples 1:
package temp2;
public class A
{
int x 1 =30;
public void display()
{
System.out.println(x1);
}
}
import temp2.A;
class usea
{
public static void main(String args[])
{
A oa=new A (11,12);
oa.pubdisplay();
//oa.pubdisplay();
//oa.pubdisplay();
//oa.pubdisplay();
}
}
Example 2:
package temp2;
public class A
{
int x1=10();
int x2=20();
public A()
{
31
public A (int p1,int p2)
{
x1=p1;
x2=p2;
}
public void pubdisplay()
{
System.out.println(x1);
System.out.println(x2);
}
Protected void prodisplay()
{
System.out.println(x1);
System.out.println(x2);
}
void defdisplay()
{
System.out.println(x1);
System.out.println(x2);
}
}
class subusea
{
32
public static void main(String args[])
{
B ob=new B();
Ob.bdisolay();
}
}
import temp2.A;
class usea
{
public static void main(String args[])
{
A oa=new A (11,12);
oa.pubdisplay();
//oa.pubdisplay();
//oa.pubdisplay();
//oa.pubdisplay();
}
}
33
Arrays
An array is a group of variables of the same type referable by a common. The types
can be either a primitive datatype like int or an object type like String. For example, you can
define an array of integers to hold the daily high temperature in a month as:
Int dailyhigh [ ]
On the other hand, ushing the point class defined in the java.awt package, you can
define an array to hold a list of two-dimensional points as:
Point points [ ]
The following example gives you a close look at the memory model of an array. First, a
class to hold a point in two-dimensional graphics is defined as follows:
class point {
int x;
int y;
Point (int x, int y) { // constructor
this.x=x;
this.y=y;
}
}
34
Accessing Array Elements:
Java follows normal C-style indexing for accessing an array element; that is, you attach
an integer-valued expression between square brackets after the name of the array. the array
index starts with zero. Therefore, to get the daily high temperature of the second day of the
month, you can use the following code fragment.
DailyHigh [1]
All subscript accesses will be checked to make sure they are within the legal range;
greater than or equal to zero and less than the array length. If the value is out of bounds the
exception Array Index Out Of Bounds Exception is thrown.
Square brackets are used to declare an array type. There are two formats for declaring an
array
• Put the brackets after the datatypes.
• Put the brackets after the array name.
For example:
int[] a;
is equivalent to
int a[];
One or the tow differences between the two forms exits when multiple array variable
are declared on the same line:
int[] a,b[];
Declares a one-dimensional array and two-dimensional array. while:
int a,b[];
A new statement is used to allocate the space needed for either holding the actual
values of the array elements or holding the references to array elements. For example, to
35
create an array to hold the daily high temperature for the month of January, you can use the
following statement.
To create an to hold the coordinates of the three vertices of a triangle, you can declare
an array as;
An array created by a new statement will have elements automatically initialized to the
default value of the elements types. For example, elements of int or double type will be
initialized to 0s, and elements of an object type will be set to null.
An array initialized may be used to create an array with preset values. A list of comma-
separated expressions that will each e evaluated to the array’s element type is enclosed in
curly braces. For example, to initialize an array to hold the number of days in each month of a
leap year, you can declare the array as;
After declaring an array variable, you can use an array initializer on the right side of a
normal assignment statement. The format is slightly different, but the result is the same.
int monthDays[];
Multidimensional Arrays:
36
int a() []={1,2}l;
int a1[]={3,4,5};
int a [][]=new int [2][];
a[0]= a0;
a[1]=a1;
You should be careful when you declaring a list of multidimensional arrays with different
dimensionalities. In following code fragment, b is declared as a one-dimensional array, and c
as a two-dimensional array;
int[] b, c[];
Java deals with two different types of entities: primitive types and true objects.
Numbers, Booleans, and characters behave very much like familiar equivalents of procedural
languages such as Pascal, C, or even C++. Other object-oriented language, like Smalltalk, do
not handle these primitive types in the same way. Smalltalk, for example, uses objects for
everything-numbers are objects are objects, Booleans are objects, characters are objects, and
so on.
Although Java is truly object-oriented, it does not use objects for the most primitive
types for the usual reason: performance. Manipulating primitive types without any object-
oriented is quite a bit more efficient. However, a uniform and consistent playing field, made up
of only objects, is simpler and can be significantly more powerful. Java contains many
subsystems that can work only with objects. With many of these subsystems, the need
frequently arises to have the system handle numbers, flags (Booleans), or characters. How
does Java get around this dilemma? By wrapping the primitive types up in some object sugar
coating. You can easily create a class, for example, whose sole purpose is encapsulating a
single integer. The net effect would be to obtain an integer object, giving you the universality
and power that comes with dealing with only objects (at the cost of some performance
degradation).
Package java. Lang contains such “types wrapper” class for every Java primitive type:
Class integer for primitive type int
37
Class Lang for primitive types long
Class Byte for primitive type byte
Class Short for primitive type short
Class Float for primitive type float
Class Double for primitive type double
Class Character for primitive type char
Class Boolean for primitive type Boolean
Class Void for primitive type void
Among the numeric types, class Integer, Long, Byte, Short, Float, and Double are so
similar that they all descend from an abstract super class called Number. Essentially, every
one of these allows you to create an object from the equivalent primitive type, and vice versa.
38
Exception Handling
- An excreation is the abnormal condition that disturb the normal program slow.
- There are many cases when abnormal conditions happen during proform exaction.
Such as
If those abnormal conditions are not prerelated or not handled properly then the
program will be terminated abrustly or it leads to incorrect results.
Java provides the sub classes of exceptional class to handle then abnormal conditions.
We will keep the normal statements in the try block. If there is any exception is raised,
then the JVM will through the respective exception object automatically from the point of
abnormal candidate.
This throws exception object will be caught by the catch handler which is following the
try block.
The finally block will be executed irrespective of whether the exception is thrown or not.
Catch or finally block must be followed by try block or both can be followed.
Exception propagate up the stack at run time until an exception handler catches it
otherwise the exception will be terms on to the user’s face by terminating the program.
39
The term statement is used in the program to normally throw the exception such as
- The throw, clause is used in the method definition to specify that the method may throw
the respective exception and the exception need to be handled at the caller method.
The example for this throws clause is
- Void f1 () throws IOException
{
----
----
}
You should use exception handing for several reasons. One is that error-handling code
is separated from normal program flow to increase the readability and maintainability of the
program.
Imagine how you would rewrite the example from the previous section in C of exception
handing was not available. You would need an if statement after every I/O operation to make
sure they were completed successfully. You would also need to use an if statement to check
whether the user provided enough filenames. To handle these abnormal conditions, you would
either add more code in place or use goto statements to branch to the code fragment that
handles common failures. Add a few more I/O calls, and even you, the author of the program,
will not be able to easily recognize easily recognize what the program was originally intended
to accomplish. With Java, there is no need to test if an exception happens. Adding more
handlers adding more catch clauses, but the original program flow is unaffected.
Another reason to use exception handling is so you can easily say where the exception
will be handled. Exceptions propogate up the call stack at run time-first up the enclosing try
blocks and then to the calling method – until an exception handler catches them. For example,
the previous example can be rewritten as a method with input and output filenames as the
arguments. The synopsis of this new method is as follows:
40
The caller of this method may want to handle the abnormal condition itself. For example
an application with a GUL may want to display a dialog box prompting the user for another
filename when the input file does not exist. In this case, the error handler for an I/O exception
is removed from the method and a throws clause is added to the method declaration. The
caller can then have its own error handing routines for these abnormal conditions. Here is the
modified method definition:
Here is code fragment to call this method and handle the abnormal conditions itself:
int byteCount = 0;
String inputFile = null;
String outputFile = null;
PrintWriter out = new PrintWrites(System. Out, true);
Try {
inputFile = args [0];
outputFile = args [0];
byteCount = copyFile(inputFile, outputFile);
}catch (ArrayIndexOutOfBoundsException e){
out.printIn(“Usage: java MyCopy [inputFile] [outputFile]’’);
}catch (FileNotFoundException e){
out.printIn(“Cannot open input file: “+ inputFile);
} catch (IOException e) {
Out.printIn(“I/O exception occurs!”);
} finally {
Out.printIn(byeCount + “bytes written”);
}
41
Exceptions are objects with hierarchical relationship. You can create a single exception
handler to catch all exceptions from a class and its subclass, or you create a serried of
exception handlers, each handing exceptions from individual subclasses. The MyCopy
example demonstrates another option. The second catch clause catch deals with
FileNotFoundException, and the next one catches may other IOExceptin.
FileNotFoundException is a subclass of IOException so you can check for both subclass and
superclass exceptions.
Just like nearly everything else in Java, exceptions are either objects or class instance.
Exception classes form their own class hierarchy. The root class of all exception classes is the
Throwable class, which is an immediate class to retrieve the error message associates with
the exception and to print the stack trace showing where the exception occurs (see the next
section for more details).
Class throwable has two immediate subclass: class Error and class Exception.
Subclass of class Exception have the suffix Exception. Subclasses of class Error have the
suffix Error (and then there is ThreadDeath, a subclass of Error). The sub classes of Error are
basically used for signaling abnormal system conditions. For example, on out of memory Error
signals that the Java Virtual Machine has run out of memory and that the garbage collector is
unable to claim any more free memory. A Strackover flowError gignals a stack overflow in the
interpreter. These Error exceptions are, in general, unrecoverable and should not be handled.
The subclasses of the Exception class are, in general, recoverable. For example an
EOFException signals that a file you have opened has no more date for reading. A file not
Found Exception signals singles that a file you want to open does not exit in the file system.
You can choose to handle the exceptions by using a try-catch block to enclose the statements
whose exceptional conditions will be handled.
The following example loops through four pathological cases in which the system
throws four types of Runtime Exception:
42
SrtingIndexOutofBoudsException For accessing a character of a string of String
Buffer with an index less than zero or greater than or equal to the length of the string.
43
Java.lang.ArithmeticException:
Test case #1
Java.long.NullPointerException:
Test case #2
Java.lang.ArrayIndexOutOfBoundsException: 9
Test case #3
Java.lang.StringIndexOutOfBoundsException: String index out of range: 99
Examples:
#1
class finallyex
{
public static void main(String args [])
{
int x1 = 10;
int x2 = 5;
int p = 30;
try
{
X2=x1/x2
A[p]=40;
}
Catch(ArithmeticException e)
{
System.out.println(e);
System.out.println(“this is arithmetic exception”);
}
/*catch(ArrayIdexOutOfBoundsException e)
{
System.out.println(e);
System.out.println(“this is Array exception”);
}*/
finally
}
System.out.println(“this is out of try catch block”);
44
}
}
}
#2
class A
{
Void test(int p) throws ArithmeticException
{
int x1 = 10;
try
{
X1 = x1/p
}
finally
{
System.out.println(“this is in test metnod”);
}
}
}
class throwsex
{
public static void main(String args[])
{
A oa=new A();
try {
oa.test(0);
}
catch(Exception e)
{
System.out.println(e);
System.out.printin(“exception intest method of A caught in main “);
}
System.out.println(“this is in main method”);
}
}
45
Threads & MultiThreading
In a single thread program if the program executes is breaked while waiting for the
complete of sme I/O operator. Then no other portion of the program can proceed execution.
We can run multiple tasks like animation, playing music, displaying documents etc.
parallely.
A thread is a single flow of control within a program and it is some time called as
execution control.
Each thread will have its own resources like program control, execute stack etc.
All threads, in a program may share same common resources like memory space,
opened files etc.
A thread is also called light process because it is a single flow of control like a process,
but it is easy o create and destroy than a process because less resources is involved.
Once a thread is waiting for something, then the JVM can choose another runnable
thread for execution.
When we need to create a thread in the application, then there are two ways to do this.
When we create a subclass of Thread, this subclass define its own run() method to
override run() method of Thread class.
46
The run() method is where he taks is performed i.e. the entry part of the Thread.
Just as the main() method is the first user defined method the JVM calls to start an
application, the run() method is the first user defined method the JVM calls to start the thread.
The Runnable interface remains only one method to be implemented i.e rum() method.
We first create an instance of the class with the new statement, followed by creation of
thread instance with another new another new statement and finally called to start() method to
start performing the task defined in the run() method.
The class instance must be passed as an argument is creates the thread instance, so
that when the start method is the thread instance is called, the Java run time known which
run() method has to be executed.
The second technique of creating of thread is useful when the class design run()
method new to be a subclass of another class.
Many methods define in thread class are used to control the running of thread.
The following are the some of the methods that are most commonly used.
Start()
Stop()
This is used to stop the execution of the thread no matter what the thread is doing.
The thread is considered as dead i.e. the internal status of thread are cleared.
The method have the data inconistant state, so this matter should be avoided.
47
Suspend()
All the statements and resources are retained can later be restarted by calling resume()
method.
Resume()
This is used to resume the execution of suspended thread will be scheduled to run.
This method causes dead lock, it should be avoided.
Sleep()
The static method causes the javarun time to place the caller thread to sleep for
specified time period.
Join()
This is used for caller thread to wit some thread to die by coming to the end of run()
method.
Yield()
This static method temporarily stops the caller’s thread and places the thread the end
of
the queue to wit for anther thread to be executed.
It is used to make sure that other thread of the same priority have the chance to run.
48
the same data.
Because all the thread in a program shake the same memory space, so it is possible for
the threads to access the same variable or run the same method of the same object at the
same time.
Problems may occur when multiple threads are accesing the same data concurrently.
Threads may vary each other, one thread may overwrite the data just written by another
thread.
Some mechanism is needed to block one thread access to the critical data if the data is
being accessed by another thread.
Inter-thread Communications:
Inter-thread communications allow threads to talk to or wait for each other. You can
have threads communicate with each other through shard data or by using thread-control
methods to have threads wait for each other.
All the threads in the same program share the same memory space. If the reference to
an object is visible to different threads by the syntactic rules of scopes, or explicitly passed to
different threads, these threads share access to the data members of that objects. As
explained in the previous section, synchronization is sometimes necessary to enforce
exclusive access to the data to avoid racing conditions and data corruption.
By using thread-control; methods, you can have threads communicate by waiting for
each other. For example, the joint() method can be used for the caller thread to wait for the
completion of the called thread. Also, a thread can suspend itself wait at a rendezvous point
using the suspend() method; another thread can wake it up through the waiting thread’s
resume() method, and both threads can run concurrently thereafter.
Deadlock may occur when a thread holding the key to a monitor is suspended or
waiting for another thread’s completion. If the other thread it is waiting for needs to get into the
49
same monitor, both threads will be waiting forever. This is why the suspend() and resum()
methods are now deprecated and should not be used. The wait(), notify(), and notifyAll()
methods defined in class Object of the java. Lang package can be used to solve this problem.
The wait() method will make the calling thread wait until either a time-out occurs or
another thread calls the same object’s notify() or notifyAll() method. The synopsis of the wait()
method is:
Wait()
Or
Wait (long timeout Period in Milliseconds)
The formor will wait until the thread is notified. The latter will wait until either the specified time-
out expires or the thread is notified, whichever comes first.
When a thread calls the wait() method, the key it is holding will be released for another
waiting thread to enter the monitor. The notify() method will wake up only one waiting thread, if
any. The notifyAll() method will wake up all the threads that have been waiting in the monitor.
After being notified, notified, the thread will try to renter the monitor by requesting the key
again and may need to wait for another thread to release the ky.
Note that these methods can be called only within a monitor, or synchronized block.
The thread calling an object’s notify() or notifyAll() method needs to own the key to that
object’s monitor; otherwise, legal Monitor State Exception, a type of Runtime Exception, will be
thrown.
The next example demonstrates the use of the wait() and notify() methods to solve the
classical producer and consumer problem. In this problem, the producer will generate data for
the consumer to consume. However, if the producer produces data faster than the consumer
can consume, the newly created data may be over-written before it is consumed. On the other
hand, if the consumer consumes faster than the producer can produce, the consumer may
keep using already processed data. Synchronization alone will not solve the problem because
it only guarantees exclusive access to the data, not availability.
The first implementation uses a monitor, an instance of the No Wait Monitor class, to
control the access to the data, token. The producer and consumer will set and get,
respectively, the token value in random intervals, with the maximum interval length regulated
by the speed argument passed to their constructors. The main program accepts up to two
50
command-line arguments for setting the producing and consuming speed, creates an instance
of the monitor, creates a producer and a consumer, and watches them rum for 10 seconds.
The program is listed as follows.
Import java.io*;
Public class No WaitPandC {
Static int produceSpeed = 200;
Static int consumeSpeed = 200;
Public staic void main (String args[]) {
If (args.length>0)
ProduceSpeed = Integer.parseInt (args[0]);
If (args.length > 1)
ConsumeSpeed = Integer.parseInt (args[1]);
No Wait Monitor Monitor = new No Wait Monitor();
New No Wait Producer (monitor, produce Speed);
New No Wait Consumer (monitor, Consume Speed);
Try {
Thread.sleep (1000);
} catch (Interrupted Exception e) {
}
System.exit(0);
}
}
class NoWaitMonitor {
int token = -1 ;
PrinterWriter out = new PrintWriter (System.out, true);
// get token value
Synchrozed int get () {
Out.println (*Got: ” + token);
return token;
}
// put token value
Synchronized void set (int value) {
token = value;
out.println (“Set: “ + token);
}
}
51
class NoWaitProducer implements Runnable {
No WaitMonitor monitor ;
Int speed;
No WaitProducer (No WaitMonitor monitor, int speed) {
This.monitor = monitor;
This.speed = speed;
New Thread (this, “Producer”).start();
}
public void run() {
int 1 = 0;
while (true) {
monitor.set (1++);
try {
Thread.sleep ((int) (Math.random() * speed));
} catch (Interrupted Exception e) {
}
}
}
}
class No WaitConsumer implements Runnable {
No WaitMonitor monitor;
In speed;
No WaitConsumer (No WaitMonitor monitor, int speed) {
This.monitor = monitor;
This.speed = speed
New Thread (this, “Consumar”).start();
}
public void run() {
while (true) {
monitor.get();
try {
thread.sleep(int) (Match.random() * speed ))
} catch (InnterruptedException e) []
}
}
}
52
Here is an example of the output of the program where the producer outpaces the consumer:
C:\MastringJavaCh08>java No WaitPandC 100 400
Set: 0
Got: 0
Set: 1
Set: 2
Set: 3
Set: 4
Got: 4
Set: 5
Set: 6
Set: 7
Set: 8
Set: 9
Set: 10
Got: 10
Set: 11
Set: 12
You can see there is a lot of data generated (show as Set) but overwritten before it is
processed (shown as Got).
Here is an example of the program’s output where the consumer is faster than the producer:
C:\MastringJavaCh08>java No WaitPandC 400 100
Set: 0
Got: 0
Got: 0
Got: 0
Got: 0
Got: 0
Got: 0
Set: 1
Set: 2
Got: 2
53
Set: 3
Got: 3
Got: 3
Got: 3
Got: 3
Got: 3
Got: 3
Set: 4
Got: 4
Got: 4
Got: 4
Got: 4
Got: 4
The second implementation of the sample program uses the wait() and notify() methods
to make sure all data is created and used exactly one. The program is the same as the
previous one, except for the implementation of the monitor. A Boolean variable, valueSet, is
added to indicate whether the data is ready for consumption or already used. The get()
method will first test if the data is ready for consumption. If not, the calling thread will wait until
some other thread sets the data and notifies the current thread. The Boolean variable is then
set to indicate that the data is consumed. Any thread waiting to produce new data will then be
notified to start production. If there is no thread waiting to produce, the notify()method will be
ignored. The get() method is show here:
54
Symmetrically, the set() method will first test whether the data is already used. If not,
the calling thread will wait until some thread uses the data and notifies the current thread. The
Boolean Variable is then set to indicate that the data is ready for consumption. Any thread
waiting to consume the data will then be notified to start the consumption. If there is no thread
waiting, the notify() method will be ignored. The set() method is shown here:
Imprt java.io.*;
Public class PandC {
Static int produceSpeed = 200;
Static int consumeSpeed = 200;
Public static void main (String args[]) {
If (args.length > 0)
ProduceSpeed = Integer. parseInt (args[0]);
If (args.length > 1)
ConsumeSpeed = Integer. parseInt (args[1]);
Monitor monitor = new monitor();
New Producer(monitor, produceSpeed);
New Consume(monitor, consumeSpeed);
Try {
Threads.sleep(1000);
} catch (InterruptedException e) {
}
System.exit(0);
55
}
}
Class Monitor {
PrintWriter out = new PrintWriter (System.out, true);
Int token;
Boolean valueSet = false;
//get token value
synchronized int get () {
if (! ValueSet)
try {
wait();
} catch (InterruptedException e) [
}
valueSet = false;
out.println (“Got: “ + token);
notify();
return token;
}
// set token value
synchronized void set (int value) {
if (valueSet)
try {
wait();
} catch (InterruptedException e) {
}
valueSet = true;
token = value;
out.printIn (“Set: “ + token);
notify();
}
}
class Producer implements Runnable {
Monitor monitor;
Int speed;
Producer (Monitor monitor, int speed) {
This.monitor = monitor;
This.speed = speed;
56
New thread (this, “Producer”).strat();
}
public void run() {
while (true) {
monitor.get();
try {
Thread.sleep (int) (Match.random() * speed));
} catch (InterruptedException e) {
}
}
}
}
Set: 0
Got: 0
Set: 1
Got: 1
Set: 2
Got: 2
Set: 3
Got: 3
Set: 4
Got: 4
This time, every price of data generated is consumed exactly exactly once.
Examples :
#1
class depositthread extends Thread
{
monitor mon;
int amount;
depositthread(monitor pmon,int pamt)
57
{
mon=pmon;
amount=pamt;
}
public void run()
{
mon.setblance(amount);
}
}
class monitor
{
int bal;
void setbalance(int pamt)
{
System.out.println(“getting balance….”);
bal=getbal();
bal=bal+pamt;
System.out.println(‘setting balance….”);
Setbal(bal);
{
int getbal()
{
return(Account.balance);
}
void setbal(int pamt)
{
Account. Balance=pamt;
}
}
Class Account
{
static int balance = 1000;
public static void main(String args[])
{
monitor mon=new monitor();
58
depositthread dp1 =new depositthread(mon,1000);
depositthread dp2 =new depositthread(mon,1000);
dp1.start();
dp2.star();
try
{
dp1.join();
dp2.join();
}
Catch(Exception e)
{
}
System.out.println(“total balance is… “+balance);
}
}
#2
class token
{
int data;
boolean setval=false;
synchronized void set(intp)
{
if(setval)
{
wait();
}
catch(Exception e)
{
}
System.out.println(“setting data……….”+p);
data=p;
setval=true;
59
notify();
}
synchronized void get()
{
if (! setval)
{
wait();
}
catch(Exception e)
{
}
}
System.out.println(“getting data…”+data);
setval=false;
notify();
}
}
while(true)
{
Try
{
Thread.sleep(2000);
}
60
catch(Exception e)
{
}
t.set(i++);
}
}
}
class synpandc
{
Public static void main(String args[])
{
token t=new token();
61
producer p=new producer(t);
consumer c=new consumer(t);
p.start();
c.start();
}
}
#3
class monitor
{
synchronized static void classmethod(int pid,monitor pmon, String pmsg)
{
pmon.tick(pid,pmsg);
}
synchronized static void objectmethod(int pid, String omsg)
{
tick(pid,pmsg);
}
catch(Exception E)
62
{
}
System.out.println(pid+pmsg+”tick#”+i);
}
}
}
class childthread extends Thread
{
monitor mon;
int id;
childthread(monitor pmon,int pid)
{
mon=pmon;
id=pmon;
}
public void run()
{
switch(id % 3)
{
case 0;
monitor.classmethod(id,mon,”class method “);
breack;
case 1:
mon.objectmethod(id,”object method “);
breack;
case 2:
mon.objectblock(id,” object block “);
}
}
}
class Syntypes
{
public static void main(String args[]) throws Exception
{
monitor m=new monitor();
childthread ct[]=new childthread[10];
63
for(int i=0;i<=9;i++)
{
ct[i]=new childtgread(m,i);
ct[i].start();
}
}
}
64
Graphical User Interface (GUI)
Abstract Window Toolkit (AWT)
Ever since Xexo’s pioneer work in the 1970s and Apple’s subsequent mass-market
introduction of mouse and icon-driven user interfaces, developers have needed introduction of
mousse and icon-driven user interfaces, developers have needed to pick competing GUI
“standards” and stick to them religiously. Mastering any given GUI standard is not a trivial
exercise, so it is not surprising that developer do not switch GUI APIs at the drop of a hat. Like
computer language themselves, GUIs have been thoroughly mutually incompatible. This and
the associated lack of a standard terminology, greatly segregated the various GUI schools, a
wasteful and divisive sate of affairs. Java’s GUI approach could abolish the GUI wars by
supporting a functionally equivalent set of most modern GUI components and presenting them
through a new platform-independent API.
At this point, you might ask yourself whether AWT also imposes a new look and feel on
out brave new (Java) word. If you are used to, for example the Macintosh user interface, it is
annoying to suddenly have application that stubbornly thrusts upon you a Windows 95 style
interface instead. Modern machines have personalities that they impose on us through their
native and often proprietary GUI. AWT respects these personalities by employing the
underlying machine’s native GUL API to construct its own universal components or mange its
behaviour itself. Java applications build around AWT reassuringly retain the Mac look and feel
on Macs and the Windows look and feel on PCs. Or, if you prefer, you can easily use an
interface that appears the same on all platforms. AWT is that flexible.
Since AWT consequently does not specify the exact look feel-and there-fore the
dimensions and exact pixel rendering-of your GUI elements, how do you ensure your GUIs will
look great on every platform? The AWT answer is layout managers. These fundamental AWT
classes are responsible for laying for laying out all the visual components in aesthetically
acceptable ways without requiring you to specify absolute positions. Unfortunately, this
process is not yet fully automatic. Java’s AWT does not have artificial intelligence or graphic
design experts embedded in its layout managers. Instead, your applications gives these layout
managers hints as to component placement and preferred sizes. These hints vary from quite
vague (“north,” “ Center,” or “third” placements) to quite specific (grid coordinates).
65
AWT Environment: Applets and Applications
Before embarking on you exploration of Java’s novel layout system and the
components it affects so thoroughly, you need to understand the two quite different contexts in
which a Java GUI can be embedded. Java’s AWT can be used in the following two
environments:
Both have different frameworks to respect. The AWT itself is not aware of the context
you choose to deploy it in, but the chosen context means different conding techniques,
possibilities, and limitations for your Java programs. The easiest context to start using is Java
applets.
As you’ve learned, an applet is a small program that runs embedded in Web browser’s
HTML page. As such, any applet has a drawing or work area equal to an imaginary picture
situated in the same spot, as illustrated in Figure 12.1
When applet code starts running, it can immediately use its applet area without any
further initializations or checks. For example, the first statement in an applet could be to draw
a diagonal line across its surface. The ease with which you can have an applet up and
running is what makes applets easier then applications (although it gets more complex for
nontrivial applets). It is quite possible to write simple little applets without knowing much about
the underlying AWT mechanisms.
Import java.awt.Graphics;
Public class Appletlet extends java.applet.Applet {
Public void paint (Graphics g) {
g.drawLine (0,0, 100,100);
}
}
66
As you can see, the applet has no lengthy initializations whatsoever before it starts
using AWT classes. Here you use the Graphics class to draw a line with its drawLine() method
(Chapter 10 discussed Java’s drawing primitives and the Graphics class). To write an applet,
you begin by extending (or subclassing) class Applet. That’s because the browser the browser
needs your applets to be instances of the Applet class. Any old Java program will not do. The
paint() method used in this example is an Applet method that you override.
Applet Methods
Whenever the browser needs to draw the page containing this applet, it tells the applet
to draw itself by calling the paint() method. For simplistic applets, this calling protocol between
the browser and your applet might be all that you need, but for more complex applets (for
example, those using animation), this approach is too limited. In fact, the browser calls many
Applet methods that were not overridden here, so only a small subset of the full browser-
applet protocol was used.
For starters, the browser calls the init() method to initialize an applet. There was not an
init() method in the simple example nothing needed to be initialized. But if your applet has any
initalization to do (namely, code it needs to execute only once, at the start of its execution), it
should put all this code in an overridden init() method. Here is an example that overrides the
init() method:
Import java.awt.Graphics;
Import java.unil.Date;
Public class Applet2 extend.java.applet.applet.Applet {
String message;
Public void init () {
Date date = new Date (System.currentTmeMillis());
Message = “I was born at: “+ date.toString();
}
public void paint (Graphics g) {
g.darwsting (message, 10, 20);
}
}
In addition to the paint() method responsible for the redrawing of the applet, you now
have a customized inti() method. This method will be called once by the browser before the
67
applet is displayed. In this example, the init () method records the date and time at the
moment the applet is initialized and converts this to a string that the paint() method will use to
draw this frozen time when the applet needs to be redrawn.
Graphically printing a string is done with the Graphics drawnString() method. It takes a
string and the coordinates for the string’s position.
The browser invokes three more methods on an applet during an apples’ts life cycle:
Start() When the applet’s HTML page comes into view or the browser is deiconified
Stop() When the applet’s HTML page is left or the browser is deiconified
Destroy() When the browser’s garbage collector determines the applet is no longer
necessary to keep in memory
To see the full browser-applet protocol in action, type in the following program, compile
it, and tell your favourite Wed browser to load a Wed page with the applet embedded in it.
Make sure you browser shows “Java console output.” On Net-scape’s Navigator browser, you
enable this by selecting Show Java Console in the program’s Options menu. For Internet
Explorer, it actually logs output to disk instead of sending it to the console.
68
The first time load HTML page, you should see the following output printed to the Java
console:
This means the init(),star(), and paint() methods are always called when an applet is
first loaded and run. The sequence can differ from what is listed here; due to asynchronous
aspects of the protocol, the paint() method can legally be called before the start() method. The
init() method, however, is guaranteed to be called before all others.
Now, whenever the browser window needs to repaint itself-for example, after having
been obscured by another window overlapping it-you should see an additional:
This is because the browser needed to completely redraw itself to undo the graphically
inconsistent picture it was showing.
Remember that the entire GUI desktop metaphor your machine maintains is just a
clever, graphical illusion. Any computer has a flat, uniform screen bitmap that doesn’t enforce
or care about these “overlapping” and clipping rectangular rectangular areas called “windows.”
The “nutural behaviour” of a computer screen in much more like that of a painting canvas in a
painting program: The canvas has no restrictions whatsoever. In a GUI environment, then
when windows are depth-arrange in this plain bitmap environment, it means some windows
will be partially or entirely overwritten, while others will need to redraw themselves. Since your
applet is part of a window, it too must play along to maintain the illusion. If not, your applet will
soon become graphically corrupted, or more likely, erased completely. This is why it is
important to have an applet repaint itself using the paint() method whenever the browser
commands it to (and as quickly as possible, as always).
If you load a different Wed page in your browser or just codify it, you should see your
applet print the following line:
69
You will probably wonder what this means since your applet was not executing any
code at the anyway. Think about the kind of applications can be used for-animation, real-time
updating of information fetched from an internet server, general entertainment, and so on. All
these types of applets are real-world applets, and they are very different from what has been
demonstrated so far in this book. Real-world applets usually run constantly.
To illustrate, imagine that the start() method in your last applet never ended because it
had to animate something all the time. Such an applet would be very wasteful of processor
resources if it kept on animating even after the user switched to a different page. Yet that is
exactly what would do if you didn’t take any steps to ovoid this problem; the way to avoid the
problem is by using threads.
In Chapter8, you saw how threads allow you to do several things at the same time.
Imagine all of an applet’s core processing and functionality (the animating, for example) being
run in a separate thread. This way, whenever the applet’s page is displaced by a new page,
you can simply freeze the thread, and when the applet’s page is reloaded, you can let the
thread run again; this is the real purpose of the start() and stop() methods of Applet. They
assume that all your applets are written with multithreading in the first place. In later chapters,
you will learn how to actually write applets built around a thread, but for now, just keep in mind
that start() and stop() are really meant to control applet threat so that they do not consume
processor resources while they are not in view.
If you now click on your browser’s Back button to revisit the page with our applet, or
deiconify the browser, you will see the console print:
Because the browser assumed your applet thread had been put to sleep when you
switched pages, it now asks your applet to wake up the thread again, immediately followed by
an request to repaint the applet. This is because the applet’s façade was overwritten a long
time ago by the previous page and any of its applets.
70
71