THOUSANDS OF FREE BLOGGER TEMPLATES

Friday, May 14, 2010

JAVA EXCEPTIONS

1. Class ClassNotFoundException
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.ClassNotFoundException
public class ClassNotFoundException
extends Exception
Thrown when an application tries to load in a class through its string name using:
• The forName method in class Class.
• The findSystemClass method in class ClassLoader .
• The loadClass method in class ClassLoader.
but no definition for the class with the specified name could be found.
As of release 1.4, this exception has been retrofitted to conform to the general purpose exception-chaining mechanism. The "optional exception that was raised while loading the class" that may be provided at construction time and accessed via the getException() method is now known as the cause, and may be accessed via the Throwable.getCause() method, as well as the aforementioned "legacy method."

2. Class AWTException
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.awt.AWTException
public class AWTException
extends Exception
Signals that an Absract Window Toolkit exception has occurred.


3. Class DataFormatException
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.util.zip.DataFormatException
public class DataFormatException
extends Exception
Signals that a data format error has occurred.

4.Class CertificateException
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.security.GeneralSecurityException
java.security.cert.CertificateException
public class CertificateException
extends GeneralSecurityException
This exception indicates one of a variety of certificate problems.

5.Class InvocationTargetException
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.reflect.InvocationTargetException
public class InvocationTargetException
extends Exception

InvocationTargetException is a checked exception that wraps an
exception thrown by an invoked method or constructor.
As of release 1.4, this exception has been retrofitted to conform to
the general purpose exception-chaining mechanism. The "target
exception" that is provided at construction time and accessed via
the getTargetException() method is now known as the cause,
and may be accessed via the Throwable.getCause() method, as
well as the aforementioned "legacy method."

6. Class LastOwnerException
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.security.acl.LastOwnerException
public class LastOwnerException
extends Exception
This is an exception that is thrown whenever an attempt is made
to delete the last owner of an Access Control List.

7. Class LineUnavailableException
java.lang.Object
java.lang.Throwable
java.lang.Exception
javax.sound.sampled.LineUnavailableException
public class LineUnavailableException
extends Exception
A LineUnavailableException is an exception indicating that a line
cannot be opened because it is unavailable. This situation arises most
commonly when a requested line is already in use by another application.

8.Class MimeTypeParseException
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.awt.datatransfer.MimeTypeParseException
public class MimeTypeParseException
extends Exception
A class to encapsulate MimeType parsing related exceptions

9. Class NoSuchElementException
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
java.util.NoSuchElementException

public class NoSuchElementException
extends RuntimeException
Thrown by the nextElement method of an Enumeration to
indicate that there are no more elements in the enumeration.

10.Class NumberFormatException
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
java.lang.IllegalArgumentException
java.lang.NumberFormatException
public class NumberFormatException
extends IllegalArgumentException

Thrown to indicate that the application has attempted to convert a
string to one of the numeric types, but that the string does not have
the appropriate format.

Thursday, April 22, 2010

INHERITANCE, POLYMORPHISM, INTERFACE

Inheritance
Inheritance can be defined as the process
where one object acquires the properties of another.
With the use of inheritance the information is made
manageable in a hierarchical order.

SUPERCLASS
A superclass allows for a generic interface to
include specialized functionality through the use of
virtual functions.The superclass mechanism is
extensively used in object oriented programming due
to the reusability that can be achieved: common features
are encapsulated in modular objects. Subclasses that
wish to implement special behavior can do so via virtual
methods, without having to duplicate (reimplement) the
superclass's behavior.

SUBCLASS
A subclass is a class that inherits some properties
from its superclass.

BENEFITS OF INHERITANCE
1. Code reusebility.
2. Minimise the amount of duplicate code.
3. Sharing common code amongst several subclasses.
4. Smaller, simpler and better organisation of code.
5. Make application code more flexible to change.

OVERRIDING METHODS
Method Overriding is achieved when a subclass
overrides non-static methods defined in the superclass,
following which the new method implementation in the
subclass that is executed.The new method definition
must have the same method signature (i.e., method name
and parameters) and return type. Only parameter types
and return type are chosen as criteria for matching
method signature. So if a subclass has its method
parameters as final it doesn’t really matter for
method overriding scenarios as it still holds true.
The new method definition cannot narrow the accessibility
of the method, but it can widen it. The new method
definition can only specify all or none, or a subset
of the exception classes (including their subclasses)
specified in the throws clause of the overridden method
in the super class.

FINAL METHODS AND CLASSES
Abstract Class:
Use the abstract keyword to declare a class abstract. The keyword appears in the class declaration somewhere before the class keyword.

/* File name : Employee.java */
public abstract class Employee
{
private String name;
private String address;
private int number;
public Employee(String name, String address, int number)
{
System.out.println("Constructing an Employee");
this.name = name;
this.address = address;
this.number = number;
}
public double computePay()
{
System.out.println("Inside Employee computePay");
return 0.0;
}
public void mailCheck()
{
System.out.println("Mailing a check to " + this.name
+ " " + this.address);
}
public String toString()
{
return name + " " + address + " " + number;
}
public String getName()
{
return name;
}
public String getAddress()
{
return address;
}
public void setAddress(String newAddress)
{
address = newAddress;
}
public int getNumber()
{
return number;
}
}


Notice that nothing is different in this Employee class. The class is now abstract, but it still has three fields, seven methods, and one constructor.

Now if you would try as follows:

/* File name : AbstractDemo.java */
public class AbstractDemo
{
public static void main(String [] args)
{

/* Following is not allowed and would raise error */
Employee e = new Employee("George W.", "Houston, TX", 43);

System.out.println("\n Call mailCheck using
Employee reference--");
e.mailCheck();
}
}


When you would compile above class then you would get following error:

Employee.java:46: Employee is abstract; cannot be instantiated
Employee e = new Employee("George W.", "Houston, TX", 43);
^
1 error1


Extending Abstract Class:
We can extend Employee class in normal way as follows:

/* File name : Salary.java */
public class Salary extends Employee
{
private double salary; //Annual salary
public Salary(String name, String address, int number, double
salary)
{
super(name, address, number);
setSalary(salary);
}
public void mailCheck()
{
System.out.println("Within mailCheck of Salary class ");
System.out.println("Mailing check to " + getName()
+ " with salary " + salary);
}
public double getSalary()
{
return salary;
}
public void setSalary(double newSalary)
{
if(newSalary >= 0.0)
{
salary = newSalary;
}
}
public double computePay()
{
System.out.println("Computing salary pay for " + getName());
return salary/52;
}
}


Here we cannot instantiate a new Employee, but if we instantiate a new Salary object, the Salary object will inherit the three fields and seven methods from Employee.

/* File name : AbstractDemo.java */
public class AbstractDemo
{
public static void main(String [] args)
{
Salary s = new Salary("Mohd Mohtashim", "Ambehta, UP",
3, 3600.00);
Salary e = new Salary("John Adams", "Boston, MA",
2, 2400.00);

System.out.println("Call mailCheck using
Salary reference --");
s.mailCheck();
System.out.println("\n Call mailCheck using
Employee reference--");
e.mailCheck();
}
}


This would produce following result:

Constructing an Employee
Constructing an Employee
Call mailCheck using Salary reference --
Within mailCheck of Salary class
Mailing check to Mohd Mohtashim with salary 3600.0

Call mailCheck using Employee reference--
Within mailCheck of Salary class
Mailing check to John Adams with salary 2400.



POLYMORPHISM
Polymorphism is the ability of an
object to take on many forms. The most
common use of polymorphism in OOP occurs
when a parent class reference is used
to refer to a child class object.Any java
object that can pass more than on IS-A
test is considered to be polymorphic.
In Java, all java objects are polymorphic
since any object will pass the IS-A test
for their own type and for the class Object.
It is important to know that the only possible
way to access an object is through a reference
variable. A reference variable can be of only
one type. Once declared the type of a reference
variable cannot be changed.The reference variable
can be reassigned to other objects provided that
it is not declared final. The type of the reference
variable would determine the methods that it can
invoke on the object.A reference variable can
refer to any object of its declared type or
any subtype of its declared type. A reference
variable can be declared as a class or interface type.


INTERFACE
An interface is a collection of abstract methods.
Aclass implements an interface, thereby inheriting
the abstract methods of the interface.
An interface is not a class. Writing an interface
is similar to writing a class, but they are two
different concepts. A class describes the attributes
and behaviors of an object. An interface contains
behaviors that a class implements.

Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class.

-An interface is similar to a class in the following ways:

-An interface can contain any number of methods.

-An interface is written in a file with a .java extension, with the name of the interface matching the name of the file.

-The bytecode of an interface appears in a .class file.

-Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name.

However, an interface is different from a class in several ways, including:

-You cannot instantiate an interface.

-An interface does not contain any constructors.

-All of the methods in an interface are abstract.

-An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.

-An interface is not extended by a class; it is implemented by a class.

-An interface can extend multiple interfaces.

Monday, April 19, 2010

Pass-by-value
The actual parameter (or argument expression)
is fully evaluated and the resulting value is copied
into a location being used to hold the formal parameter's
value during method/function execution. That location is
typically a chunk of memory on the runtime stack for the
application (which is how Java handles it), but other
languages could choose parameter storage differently.
Pass-by-reference
The formal parameter merely acts as an alias for
the actual parameter. Anytime the method/function
uses the formal parameter (for reading or writing),
it is actually using the actual parameter.

Working w/ the Java

Object-oriented programming (OOP) is a programming paradigm
that uses "objects" – data structures
consisting of datafields and methods together
with their interactions – to design applications
and computer programs. Programming techniques may
include features such as data abstraction, encapsulation,
modularity, polymorphism, and inheritance. It was not
commonly used in mainstream software application
development until the early 1990s.

Encapsulation is the technique of making the fields in a
class private and providing access to the fields
via public methods. If a field is declared private,
it cannot be accessed by anyone outside the class,
thereby hiding the fields within the class. For this
reason, encapsulation is also referred to as data
hiding.Encapsulation can be described as a protective
barrier that prevents the code and data being randomly
accessed by other code defined outside the class. Access
to the data and code is tightly controlled by an interface.
The main benefit of encapsulation is the ability to modify
our implemented code without breaking the code of others who
use our code.

Object - Objects have states and behaviors.
Example: A dog has states-color, name, breed as
well as behaviors -wagging, barking, eating. An object
is an instance of a class.

Class - A class can be defined as a template/ blue print
that describe the behaviors/states that object of
its type support.

Class Variables
When a number of objects are created from the same
class blueprint, they each have their own distinct
copies of instance variables. In the case of the
Bicycle class, the instance variables are cadence,
gear, and speed. Each Bicycle object has its own
values for these variables, stored in different memory
locations.
Sometimes, you want to have variables that are common
to all objects. This is accomplished with the static
modifier. Fields that have the static modifier in their
declaration are called static fields or class variables.
They are associated with the class, rather than with any
object. Every instance of the class shares a class variable,
which is in one fixed location in memory. Any object can change
the value of a class variable, but class variables can also be
manipulated without creating an instance of the class.

Class Instantiation
In programming, instantiation is the creation of a real
instance or particular realization of an abstraction or
template such as a class of objects or a computer process.
To instantiate is to create such an instance by, for example,
defining one particular variation of object within a class,
giving it a name, and locating it in some physical place

The Method Declaration
A method's declaration provides a lot of information about
the method to the compiler, the runtime system and to other
classes and objects. Besides the name of the method, the method
declaration carries information such as the return type of the
method, the number and type of the arguments required by the
method, and what other classes and objects can call the method.
While this may sound like writing a novel rather than simply
declaring a method for a class, most method attributes can be
declared implicitly. The only two required elements of a method
declaration are the method name and the data type returned by
the method. For example, the following declares a method named
isEmpty() in the Stack class that returns a boolean value
(true or false):
class Stack
{
. . .
boolean isEmpty() {
. . .
}
}



STATIC VARIABLES
A static variable is associated with the class as a whole
rather than with specific instances of a class. Each object
will share a common copy of the static variables i.e. there
is only one copy per class, no matter how many objects are
created from it. Class variables or static variables are
declared with the static keyword in a class. These are declared
outside a class and stored in static memory. Class variables are
mostly used for constants. Static variables are always called by
the class name. This variable is created when the program starts
and gets destroyed when the programs stops. The scope of the class
variable is same an instance variable. Its initial value is same as
instance variable and gets a default value when its not initialized
corresponding to the data type. Similarly, a static method is a method
that belongs to the class rather than any object of the class and
doesn’t apply to an object or even require that any objects of the
class have been instantiated.
Static methods are implicitly final, because overriding is done based
on the type of the object, and static methods are attached to a class,
not an object. A static method in a superclass can be shadowed by another
static method in a subclass, as long as the original method was not
declared final. However, you can’t override a static method with
a non-static method. In other words, you can’t change a static method
into an instance method in a subclass.

Thursday, April 15, 2010

JAVA PROGRAMMING

Arrays

An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. You've seen an example of arrays already, in the main method of the "Hello World!" application. This section discusses arrays in greater detail.

INTRODUCTION TO JAVA PROGRAMMING

A. A LITTLE BIT OF HISTORY

Java was created in 1991 by James Gosling et al. of Sun Microsystems. Initially calledOak, in honor of the tree outside Gosling's window, its name was changed to Javabecause there was already a language called Oak.The original motivation for Java was the need for platform independent language thatcould be embedded in various consumer electronic products like toasters andrefrigerators. One of the first projects developed using Java was a personal hand-heldremote control named Star 7.At about the same time, the World Wide Web and the Internet were gaining popularity.Gosling et. al. realized that Java could be used for Internet programming.

B. WHAT IS JAVA TECHNOLOGY?

1 . A programming languageAs a programming language,
Java can create all kinds of applications that you couldcreate
using any conventional programming language.

2. A development environmentAs a development
environment, Java technology provides you with a large
suite oftools: a compiler, an interpreter, a documentation
generator, a class file packaging tool,and so on.

3. An application environmentJava technology
applications are typically general-purpose
programs that run on anymachine where
the Java runtime environment (JRE) is installed.

4. A deployment environmentThere are two main
deployment environments: First, the JRE supplied
by the Java 2Software Development Kit (SDK)
contains the complete set of class files for all the
Javatechnology packages, which includes basic
language classes, GUI component classes,and
so on. The other main deployment environment
is on your web browser. Mostcommercial
browsers supply a Java technology interpreter
and runtime environment.

C. SOME FEATURES OF JAVA
1. The Java Virtual MachineThe Java Virtual Machine
is an imaginary machine that is implemented
by emulatingsoftware on a real machine. The
JVM provides the hardware platform specifications
towhich you compile all Java technology code.
This specification enables the Java softwareto
be platform-independent because the compilation
is done for a generic machineknown as the
JVM.A bytecode is a special machine language
that can be understood by the Java
VirtualMachine (JVM). The bytecode is
independent of any particular computer
hardware, soany computer with a Java
interpreter can execute the compiled Java
program, no matterwhat type of computer
the program was compiled on.

2. Garbage CollectionMany programming
languages allows a programmer to allocate
memory during runtime.However, after
using that allocated memory, there should
be a way to deallocate thatmemory block
in order for other programs to use it again.
In C, C++ and otherlanguages the programmer
is responsible for this. This can be difficult
at times sincethere can be instances wherein
the programmers forget to deallocate memory
andtherefor result to what we call memory
leaks.In Java, the programmer is freed
from the burden of having to deallocate that
memorythemselves by having what we call the
garbage collection thread. The garbagecollection
thread is responsible for freeing any memory
that can be freed. This happensautomatically
during the lifetime of the Java program.

3. Code SecurityCode security is attained in Java
through the implementation of its Java RuntimeEnvironment
(JRE). The JRE runs code compiled for a JVM and
performs class loading(through the class loader),
code verification (through the bytecode verifier)
and finallycode execution.The Class Loader is
responsible for loading all classes needed for
the Java program. Itadds security by separating
the namespaces for the classes of the local file
system fromthose that are imported from
network sources. This limits any Trojan horse
applicationssince local classes are always
loaded first. After loading all the classes, the
memorylayout of the executable is
then determined. This adds protection against
unauthorizedaccess to restricted areas of the
code since the memory layout is determined
duringruntime.After loading the class and layouting
of memory, the bytecode verifier then tests
the format of the code fragments and checks
the code fragments for illegal code that canviolate
access rights to objects.After all of these have been
done, the code is then finally executed.

D. PHASES OF A JAVA PROGRAM
The following figure describes the process of compiling
and executing a Java program.· The first step in creating a Java
program is by writing your programs in a text editor.Examples
of text editors you can use are notepad, vi, emacs, etc. This
file is stored in adisk file with the extension .java.·After creating
and saving your Java program, compile the program by using
the JavaCompiler. The output of this process is a file
of Java bytecodes with the file extension.class.·The
class file is then interpreted by the Java interpreter
that converts the bytecodesinto the machine language
of the particular computer you are using.
Task
1. Write the program
2. Compile the program
3. Run the program
Tool to use

1. Any text editor
2. Java Compiler
3. Java Interpreter
Output

1. File with .java extension2. File with .class
extension(Java Byte Codes)3. Program Output

E. DIFFERENCE BETWEEN JAVA APPLICATIONS AND JAVA APPLETS
  • Java applets are java programs that run in a web browser, it is dependent on web browsers.
    -
  • Java applications are stand alone java programs. It can be run in a console window or a graphical user interface.

F. WHAT MAKES JAVA AN OBJECT ORIENTED LANGUAGE

Thursday, July 2, 2009

Operating System Services

Program execution – system capability to load a program into
memory and to run it
I/O operations – since user programs cannot execute I/O
operations directly, the operating system must provide some
means to perform I/O
File-system manipulation – program capability to read, write,
create, and delete files
Communications – exchange of information between processes
executing either on the same computer or on different systems
tied together by a network. Implemented via shared memory or
message passing
Error detection – ensure correct computing by detecting errors
in the CPU and memory hardware, in I/O devices, or in user
programs

Sytem Components

Process Management
A process is a program in execution
A process needs certain resources, including CPU time,
memory, files, and I/O devices, to accomplish its task
The operating system is responsible for the following
activities in connection with process management
Process creation and deletion
Process suspension and resumption
Provision of mechanisms for:
process synchronizationprocess communication
Main-Memory Management
Memory is a large array of words or bytes, each with its
own address
It is a repository of quickly accessible data shared by the
CPU and I/O devices
Main memory is a volatile storage device. It loses its
contents in the case of system failure
The operating system is responsible for the following
activities in connections with memory management
Keep track of which parts of memory are currently being
used and by whom
Decide which processes to load when memory space
becomes available
Allocate and deallocate memory space as needed
File Management
A file is a collection of related information defined by its creator
Commonly, files represent programs (both source and object forms)
and data
The operating system is responsible for the following activities in
connections with file management:
File creation and deletion
Directory creation and deletion
Support of primitives for manipulating files and directories
Mapping files onto secondary storage
File backup on stable (nonvolatile) storage media
I/O System Management
The I/O system consists of:
A buffer-caching system
A general device-driver interface
Drivers for specific hardware devices

Secondary-Storage Management
Since main memory (primary storage) is volatile and too
small to accommodate all data and programs
permanently, the computer system must provide
secondary storage to back up main memory
Most modern computer systems use disks as the
principle on-line storage medium, for both programs and
data
The operating system is responsible for the following
activities in connection with disk management:
Free space management
Storage allocation
Disk scheduling

Protection System
Protection refers to a mechanism for controlling access by
programs, processes, or users to both system and user
resources
The protection mechanism must:
distinguish between authorized and unauthorized usage
specify the controls to be imposed
provide a means of enforcement

Command-Interpreter System
Many commands are given to the operating system by control
statements which deal with:

Process creation and management
I/O handling
Secondary-storage management
Main-memory management
File-system access
Protection
Networking
The program that reads and interprets control statements is
called variously:
command-line interpreter
shell (in UNIX)
Its function is to get and execute the next command statement