Tuesday, December 29, 2009

Core Java Tutorial

Java Versions:
JDK 1.0 :
Codename Oak. Initial release.
JDK 1.1 :
• inner classes added to the language
• JavaBeans
JDK 1.2 :
• strictfp keyword
• reflection which supported Introspection only, no modification at runtime was possible.
• the Swing graphical API was integrated into the core classes
• Sun's JVM was equipped with a JIT compiler for the first time
• Java Plug-in
• Java IDL, an IDL implementation for CORBA interoperability
• Collections framework

JDK 1.3 :
• HotSpot JVM included (the HotSpot JVM was first released in April, 1999 for the J2SE 1.2 JVM)
• RMI was modified to support optional compatibility with CORBA
• JavaSound
• Java Naming and Directory Interface (JNDI) included in core libraries (previously available as an extension)
• Java Platform Debugger Architecture (JPDA)

JDK 1.4 :
• assert keyword (Specified in JSR 41.)
• regular expressions modeled after Perl regular expressions
• exception chaining allows an exception to encapsulate original lower-level exception
• Internet Protocol version 6 (IPv6) support
• non-blocking NIO (New Input/Output) (Specified in JSR 51.)
• logging API (Specified in JSR 47.)
• image I/O API for reading and writing images in formats like JPEG and PNG
• integrated XML parser and XSLT processor (JAXP) (Specified in JSR 5 and JSR 63.)
• integrated security and cryptography extensions (JCE, JSSE, JAAS)
• Java Web Start included (Java Web Start was first released in March, 2001 for J2SE 1.3) (Specified in JSR 56.)
JDK 5.0 :
• Generics: Provides compile-time (static) type safety for collections and eliminates the need for most typecasts (type conversion). (Specified by JSR 14.)
• Metadata: Also called annotations; allows language constructs such as classes and methods to be tagged with additional data, which can then be processed by metadata-aware utilities. (Specified by JSR 175.)
• Autoboxing/unboxing: Automatic conversions between primitive types (such as int) and primitive wrapper classes (such as Integer). (Specified by JSR 201.)
• Enumerations: The enum keyword creates a typesafe, ordered list of values (such as Day.MONDAY, Day.TUESDAY, etc.). Previously this could only be achieved by non-typesafe constant integers or manually constructed classes (typesafe enum pattern). (Specified by JSR 201.)
• Swing: New skinnable look and feel, called synth.
• Varargs: The last parameter of a method can now be declared using a type name followed by three dots (e.g. void drawtext(String... lines)). In the calling code any number of parameters of that type can be used and they are then placed in an array to be passed to the method, or alternatively the calling code can pass an array of that type.
• Enhanced for each loop: The for loop syntax is extended with special syntax for iterating over each member of either an array or any Iterable, such as the standard Collection classes, using a construct of the form:
void displayWidgets (Iterable widgets) {
for (Widget w: widgets) {
This example iterates over the Iterable object widgets, assigning each of its items in turn to the variable w, and then calling the Widget method display() for each item. (Specified by JSR 201.)
• Fix the previously broken semantics of the Java Memory Model, which defines how threads interact through memory.
• Automatic stub generation for RMI objects.
• static imports
• 1.5.0_18 (5u18) is the last release of Java to officially support the Microsoft Windows 9x line (Windows 95, Windows 98, Windows ME). [1] Unofficially, Java SE 6 Update 7 ( is the last version of Java to be shown working on this family of operating systems. [2]
• The concurrency utilities in package java.util.concurrent.[11]
• Scanner class for parsing data from various input streams and buffers.
Java SE 6:
1. Improved Web Service support through JAX-WS (JSR 224)

2. JDBC 4.0 support (JSR 221)..
3. Support for pluggable annotations

Java is a programming language originally developed by Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ .Java applications are typically compiled to bytecode that can run on any Java virtual machine (JVM) regardless of computer architecture.
Why Java Is Important to the Internet:
In a network, two very broad categories of objects are transmitted between the server and your personal computer: passive information and dynamic, active programs. For example, when you read your e-mail, you are viewing passive data. Even when you download a program, the program's code is still only passive data until you execute it. However, a second type of object can be transmitted to your computer: a dynamic, self-executing program
Java Applets and Applications
An application is a program that runs on your computer.it is Java's ability to create applets that makes it important.An applet is an application designed to be transmitted over the Internet and executed by
a Java-compatible Web browser
Prior to Java, most users did not download executable programs frequently, and those who did scanned them for viruses prior to execution.

When you use a Java-compatible Web browser, you can safely download Java applets without fear of viral infection or malicious intent. Java achieves this protection by Confining a Java program to the Java execution environment and not allowing it access to other parts of the computer.
many types of computers and operating systems are connected to the Internet. For programs to be
dynamically downloaded to all the various types of platforms connected to the Internet,
some means of generating portable executable code is needed.
Byte Code java’s magic for solve above to problems(security and portability):
The output of a Java compiler is not executable code. Rather, it is bytecode. Bytecode is a highly optimized set of instructions designed to be executed by the Java run-time system, which is called the Java Virtual Machine (JVM). That is, in its standard form, the JVM is an interpreter for bytecode only the JVM needs to be implemented for each platform. Once the run-time package exists for a given system, any Java program can run on it. Remember, although the details of the JVM will differ from
platform to platform, all interpret the same Java bytecode.

The Java Buzzwords
Although the fundamental forces that necessitated the invention of Java are portability
and security, other factors also played an important role
Simple: Java was designed to be easy for the professional programmer to learn and use effectively
Robust : memory management mistakes and mishandled exceptional conditions
Multithreaded: Java supports multithreaded programming, which allows you to write programs that do many things simultaneously
Architecture-neutral : there was no garantee today programs run and tomorrow also run in same manner. Java achieved.
Interpreted & High performance: Java enables the creation of cross-platform programs by compiling into an intermediate representation called Java bytecode. This code can be interpreted
on any system that provides a Java Virtual Machine
java was designed to perform well on very low-power CPUs.
Distributed: Java is designed for the distributed environment of the Internet, because it handles
TCP/IP protocols.
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.
process-oriented model: a program can be conceptually organized around its code then code is acting on data,
Object-oriented programming: a program can be conceptually organized around its data.

The four OOP Principles:

Abstraction: Humans manage complexity through abstraction. Hiding inner details of system how its been working internally. Only explore the essentials details to end user. Ex car-breaks-engine etc.
Java Example : Any application that defines a Class that is used to instantiate objects is one that demonstrates abstraction.
Encapsulation: Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse.

Java Example:
class Box {
// what are the properties or fields
private int length,width,height;
public setters for above all.
public int displayVolume()
{return (length*width*height);}
class BoxVol {
public static void main (String args[]) {
Box ob1 = new Box();
double vol;
vol = ob1.displayVolume();
System.out.println("Volume is : " + vol);

private keyword use top specifies encapsulated. By keeping your data private(encapsulated)out side entity(other methods and data) can not misuse it or alter it with out your permission. you can only allow them to use it but you will not allow them to alter it.

Inheritance: Inheritance is the process by which one object acquires the properties of another object.
Different kinds of objects often have a certain amount in common with each other.
Object-oriented programming allows classes to inherit commonly used state and behavior from other classes.
Polymorphism: Polymorphism (from the Greek, meaning "many forms") same name with different forms.
Overloaded methods are methods with the same name signature but either a different number of parameters or different types in the parameter list.
Overridden methods are methods that are redefined within an inherited or subclass. They have the same signature and the subclass definition is used.
Polymorphism is the capability of an action or method to do different things based on the object that it is acting upon. This is the third basic principle of object oriented programming. Overloading and overriding are two types of polymorphism . Now we will look at the third type: dynamic method binding.

Covariant Returns: when subclass want to change its overridden method’s return type it should match its inherited vertion exactly.
dynamic method binding, when the compiler can't determine which method implementation to use in advance, the runtime system (JVM) selects the appropriate method at runtime, based on the class of the object
Assume that three subclasses (Cow, Dog and Snake) have been created based on the Animal abstract class, each having their own speak() method.

Notice that although each method reference was to an Animal (but no animal objects exist), the program is able to resolve the correct method related to the subclass object at runtime. This is known as dynamic (or late) method binding.

What is an object?
An object is a software construct that encapsulates data, along with the ability to use or modify and inspect that data, into a software entity.
What is a class?
A class is a blueprint for objects: A class is a collection of data and methods that operate on that data. it defines a type of object according to the data the object can hold and the operations the object can perform.
The Object Class
The Object class is the highest superclass (ie. root class) of Java. All other classes are subclasses (children or descendants) of the Object class. The Object class includes methods such as:
clone() equals() copy(Object src) finalize() getClass()
hashCode() notify() notifyAll() toString() wait()
The equals() method of java.lang.Object acts the same as the == operator; that is, it tests for object identity rather than object equality
If you override equals(), you must override hashCode() as well.
Hash code is not an unique number for an object.If two objects are equals(means ob1.equals(ob2))then these two objects return same hash code.so we have to implement hashcode() of a class in such way that if two objects are equals(that is compared by equal() of that class)then those two objects must return same hash code.
Key facts to remember about comparing variables:
1. The rule is the same for reference variables and primitive variables:
== returns true if the two bit patterns are identical.
2. Primitive variables must use ==; they cannot use the equals() method.
3. For reference variables, == means that both reference variables are referring to the same object.
4.The StringBuffer class has not overridden equals().
5. The String and wrapper classes are final and have overridden equals().
However,it’s important to know that all of the wrapper class’ equals() methods only return true if both the primitive values and the wrapper’s classes are the same.

What It Means if You Don’t Override equals()
There’s a potential limitation lurking here: if you don’t override the
equals() method, you won’t be able to use the object as a key in a hashtable.(ex car and person in hasMap then after some time u r searching for car)
Cloning of Java objects
Parent class of all objects in java is "Object" which contains several important methos. One of them is "clone" which is used to make a copy of object. There are certain things which you should keep in mind while using clone method of Object.
- implement "Cloneable" interface to each class which you want to clone. If a class does not implement "Cloneable" interface, clone method will throw "CloneNotSupportedException". This interface does not contain any methods. It is just a marker interface
Obtain cloned object through super.clone
- if your class only contains primitive attributes or wrappers of primitive attributes or immutable object like String, then you don't need to do anything special inside clone method because clone method automatically makes a copy of primitive data types and immutable objects.
by default clone method makes shallow copy and does not make a Deep copy of object. Deep copy means if your object contains references of other objects or collections, it will not make a copy of those referenced objects and will use same references as was in origianl objects.
In order to do deep copy of Book class, we will aso have to implement "Cloneable" interface in "Publisher" class + override clone method in Publisher class too. If Publisher class contains references of other objects, then you will have to implement Cloneable interface + override clone method in each sub class too.
Perpose of Marker interfaces:
Marker interfaces are also called "tag" interfaces since they tag all the derived classes into a category based on their purpose. For example, all classes that implement the Cloneable interface can be cloned (i.e., the clone() method can be called on them). The Java compiler checks to make sure that if the clone() method is called on a class and the class implements the Cloneable interface. For example, consider the following call to the clone() method on an object o:

SomeObject o = new SomeObject();
SomeObject ref = (SomeObject)(o.clone());
If the class SomeObject does not implement the interface Cloneable (and Cloneable is not implemented by any of the superclasses that SomeObject inherits from), the compiler will mark this line as an error. This is because the clone() method may only be called by objects of type "Cloneable." Hence, even though Cloneable is an empty interface, it serves an important purpose.
Class Access: access means visibility
Default Access: has no modifier preceding it in the declaration. Think of default access as package-level access, because a class with default access can be seen only by classes within the same package
Public Access: all classes in the Java Universe (JU) have access to a public class. Don't forget, though, that if a public class you're trying to use is in a different package from the class you're writing, you'll still need to import the public class.

Non access Class Modifiers: You can modify a class declaration using the keyword final, abstract, or strictfp. These modifiers are in addition to whatever access control is on the class,
so you could, for example, declare a class as both public and final. But you can't
always mix nonaccess modifiers. You're free to use strictfp in combination with
final, for example, but you must never, ever, ever mark a class as both final and

Final Classes When used in a class declaration, the final keyword means the class can't be subclassed
Why class need to make final?
You should make a final class only if you need an absolute guarantee that none of the methods in that class will ever be overridden.

Abstract Classes An abstract class can never be instantiated. Its sole purpose, mission in life, raison d'ĂȘtre, is to be extended (subclassed).
Why class need to make abstract?.
For example, imagine you have a class Car that has generic methods common to all vehicles. But you don't want anyone actually creating a generic, abstract Car object.

Strictfp: The strictfp keyword may be applied to both classes and methods. For classes (and interfaces), it specifies that all of the expressions in all of the methods of the class are FP-strict. For methods, it specifies that all of the expressions in that method are FP-strict.

FP-strict? If you do not specify strictfp then the compiler & runtime system may be more aggressive in how they deal with floating-point expressions. Whereas with strictfp specified, the compiler and runtime system must toe the line very precisely according to the IEEE-754 floating-point specification.
If u mention strictfp : floating-point expressions in your code are fast or predictable in all platforms(consistent across multiple platforms).


When you create an interface, you're defining a contract for what a class can do, without saying anything about how the class will do it. An interface is a contract.

• All interface methods are implicitly public and abstract.
• All variables defined in an interface must be public, static, and final(constants)
• Interface methods must not be static.
• Because interface methods are abstract, they cannot be marked final,strictfp, or native.
• An interface can extend one or more other interfaces.
• An interface cannot extend anything but another interface.
• An interface cannot implement another interface or class.

Access Modifiers: Access modifiers in Java determine the visibility and the scope of the Java elements.

Whereas a class can use just two of the four access control levels (default or public), members can use all four:
n public: Public modifier achieves the highest level of accessibility. When a method or variable member is declared public, it means all other classes, regardless of the package they belong to, can access the member (assuming the class itself is visible).

n protected & n default: The protected and default access control levels are almost identical, but with one critical difference. A default member may be accessed only if the class accessing the member belongs to the same package, whereas a protected member can be accessed (through inheritance) by a subclass even if the subclass is in a different package.

n private: Members marked private can't be accessed by code in any class other than the
class in which the private member was declared
For better understanding, member level access is formulated as a table:
Access Modifier Same Class Same Package Subclass Other packages
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N

Nonaccess Member Modifiers:

Final Methods
The final keyword prevents a method from being overridden in a subclass. For example, the Thread
class has a method called isAlive() that checks whether a thread is still active. If you extend the Thread class, though, there is really no way that you can correctly implement this method yourself (it uses native code, for one thing), so the designers have made it final. Just as you can't subclass the String class (because we need to be able to trust in the behavior of a String object), you can't override many of the
methods in the core class libraries.(String class in ‘final)

Final Arguments:
which of course means it can't be modified within the method. In this case, "modified" means reassigning a new value to the variable.

Final Variables
Declaring a variable with the final keyword makes it impossible to reinitialize that
variable once it has been initialized with an explicit value

A reference variable marked final can't ever be reassigned to refer to a different object. The data within
the object can be modified, but the reference variable cannot be changed. In other words, a final reference still allows you to modify the state of the object it refers to, but you can't modify the reference variable to make it refer to a different object.

Abstract Methods
An abstract method is a method that's been declared (as abstract) but not implemented
The first concrete subclass of an abstract class must implement all abstract
methods of the superclass.

It is illegal to have even a single abstract method in a class that is not explicitly
declared abstract

Synchronized Methods
The synchronized keyword indicates that a method can be accessed by only one thread at a time.

Synchrozing is process of control the mothed by access multiple threads at a same time.

Native Methods
The native modifier indicates that a method is implemented in platform-dependent code, that native can be applied only to methods—not classes, not variables, just methods.Note that a native method's body must be a semicolon (;) (like abstract methods),indicating that the implementation is omitted.

Strictfp Methods: same as strictfp class.

Methods with Variable Argument Lists (var-args):
As of 5.0, Java allows you to create methods that can take a variable number of arguments.
arguments The things you specify between the parentheses when you're
invoking a method:
doStuff("a", 2); // invoking doStuff, so a & 2 are arguments
n parameters The things in the method's signature that indicate what the
method must receive when it's invoked:
void doStuff(String s, int a) { }

Rules:1) you must specify the type of the argument(s) this parameter of your method can receive.
2) you follow the type with an ellipsis (...), a space, and then the name of the array that will
hold the parameters received.
3) It's legal to have other parameters in a method that uses a var-arg.
4) The var-arg must be the last parameter in the method's signature, and you only one var-arg in a method.

Constructor Declarations:
Every time you make a new object, at least one constructor is invoked. Every class has a constructor, although if you don't create one explicitly, the compiler will build one for you.


Variable Declarations: There are two types of variables in Java:
Primitives A primitive can be one of eight types:
A reference variable is used to refer to (or access) an object.
Type Bits Bytes min max its Bytes Minimum Range Maximum Range
byte 8 1 -27 27-1
short 16 2 -215 215-1
int 32 4 -231 231-1
long 64 8 -263 263-1
float 32 4 n/a n/a
double 64 8 n/a n/a

Declaring Reference Variables
Reference variables can be declared as static variables, instance variables, method parameters, or local variables.
Instance Variables
Instance variables are defined inside the class, but outside of any method, and are only initialized when the class is instantiated.
Final Variables
Declaring a variable with the final keyword makes it impossible to reinitialize that
variable once it has been initialized with an explicit value

A reference variable marked final can't ever be reassigned to refer to a different object. The data within
the object can be modified, but the reference variable cannot be changed. In other words, a final reference still allows you to modify the state of the object it refers to, but you can't modify the reference variable to make it refer to a different object.

Array Declarations
In Java, arrays are objects that store multiple variables of the same type, or variables that are all subclasses of the same type. Arrays can hold either primitives or object references, but the array itself will always be an object on the heap, even if the array is declared to hold primitive elements. In other words, there is no such thing as a primitive array, but you can make an array of primitives.
Declaring an Array of Primitives
int[] key; // Square brackets before name (recommended)
int key []; // Square brackets after name (legal but less
// readable)
Declaring an Array of Object References
Thread[] threads; // Recommended
Thread threads []; // Legal but less readable

• An array can only hold one type of objects (including primitives).
• Arrays are fixed size.

Transient Variables
If you mark an instance variable as transient, you're telling the JVM to skip (ignore) this variable when you attempt to serialize the object containing it.

Volatile Variables
If you are working with the multi-threaded programming, the volatile keyword will be more useful. When multiple threads using the same variable, each thread will have its own copy of the local cache for that variable. So, when it's updating the value, it is actually updated in the local cache not in the main variable memory. The other thread which is using the same variable doesn't know anything about the values changed by the another thread. To avoid this problem, if you declare a variable as volatile, then it will not be stored in the local cache. Whenever thread are updating the values, it is updated to the main memory. So, other threads can access the updated value.
(or) volatile is used to indicate that a variable's value will be modified by different threads.
the variable is modified asynchronously by concurrently running threads.The volatile keyword is used on variables that may be modified simultaneously by other threads.

Static Variables and Methods
The static modifier is used to create variables and methods that will exist independently of any instances created for the class. In other words, static members exist before you ever make a new instance of a class, and there will be only one copy of the static member regardless of the number of instances of that class.
All static members belong to the class, not to any instance.

Static imports:
Definition: The normal import declaration imports classes from packages, so that they can be used without package reference. Similarly the static import declaration imports static members from classes and allowing them to be used without class reference.

Static blocks: The code in a static block is loaded/executed only once i.e. when the class is first initialized. A class can have any number of static blocks. Static block is not member of a class they do not have a return statement and they cannot be called directly. Cannot contain this or super. They are primarily used to initialize static fields.

Great explanation: http://www.codestyle.org/java/faq-Static.shtml

Declaring Enums : variable restriction
As of 5.0, Java lets you restrict a variable to having one of only a few pre-defined values—in other words, one value from an enumerated list. (The items in the enumerated list are called, surprisingly, enums.)

An enum is NOT a String or an int; an enum constant's type is the enum type. For example, WINTER, SPRING, SUMMER, and FALL

enum constructors can NEVER be invoked directly in code. They are always called automatically when an enum is initialized.

An enum declared outside a class must NOT be marked static, final, abstract, protected, or private.

Stack and Heap:
the various pieces (methods, variables, and objects) of Java programs live in one of two places in memory: the stack or the heap.
• Instance variables and objects live on the heap.
• n Local variables live on the stack.


Auto-boxing and Auto-Unboxing enables the primitive types to be converted into respective wrapper objects and the other way around.
Boxing, ==, and equals():
The API developers decided that for all the wrapper classes, two objects are equal if they are of the same type and have the same value. It shouldn't be surprising that
Integer i1 = 1000;
Integer i2 = 1000;
if(i1 != i2) System.out.println("different objects");
if(i1.equals(i2)) System.out.println("meaningfully equal");
Produces the output:
different objects
meaningfully equal

It's just two wrapper objects that happen to have the same value. Because they have the same int value, the equals() method considers them to be "meaningfully equivalent", and therefore returns true. How about this one:
Integer i3 = 10;
Integer i4 = 10;
if(i3 == i4) System.out.println("same object");
if(i3.equals(i4)) System.out.println("meaningfully equal");
This example produces the output:
same object
meaningfully equal
Yikes! The equals() method seems to be working, but what happened with ==
and != ? Why is != telling us that i1 and i2 are different objects, when == is saying
that i3 and i4 are the same object? In order to save memory, two instances of the
following wrapper objects will always be == when their primitive values are the same:

garbage collection :

In computing, garbage collection (also known as GC) is a form of automatic memory management. The garbage collector or collector attempts to reclaim the memory used by objects that will never be accessed again by the application or mutator. Garbage collection was invented by John McCarthy around 1959 to solve the problems of manual memory management in his recently devised Lisp programming language.

There are several basic strategies for garbage collection: reference counting, mark-sweep, mark-compact, and copying.

Forcing Garbage Collection:

Cleaning Up Before Garbage Collection—the finalize() Method:
Every class inherits finalize() method from Object class and method is usually called by the garbage collector when ensures that no more references to the object exist .object class finalize method performs no significant action so normally it is overridden by a java class for clean up code e.g. close a file, closing database connections etc.

The purpose of finalization is to give an unreachable ob
ject the opportunity to perform any cleanup processing before the object is garbage collected.
protected void finalize() throws Throwable {}

break and continue:
The break statement causes the program to stop execution of the innermost looping and start processing the next line of code after the block.

The continue statement causes the program to stop execution of the current iteration(no statements will get execute after countinue statement).control will return to next iteration.


An exception is an event that occurs during the execution of a program, JVM encounters an abnormal condition, which disturb the normal flow of execution.

Exceptions are of two types:
1. Compiler-enforced exceptions, or checked exceptions
2. Runtime exceptions, or unchecked exceptions
3. Error exceptions
checked exceptions:Compiler-enforced (checked) exceptions are instances of the Exception class or one of its subclasses -- excluding the RuntimeException branch. The compiler expects all checked exceptions to be appropriately handled.
When a method throws a checked exception (e.g., IOException), the client code must handle the exception by either defining a try-caught block or delay the exception handling by propagating the exception event up to a higher level code.
ClassNotFoundException, CloneNotSupportedException, NoSuchFieldException, NoSuchMethodException, IOException, InterruptedException, ParseException

Unchecked exceptions are all exceptions that subclass from java.lang.RuntimeException. Unlike the checked exceptions, when a method throws unchecked exceptions, the client code is not required to define explicit code to handle the exceptions — e.g., NullPointerException.
With an unchecked exception, however, the compiler doesn't force client programmers either to catch the exception or declare it in a throws clause. In fact, client programmers may not even know that the exception could be thrown. eg, StringIndexOutOfBoundsException thrown by String's charAt() method.
ArithmeticException, ArrayIndexOutOfBoundsException, ClassCastException, IllegalArgumentException, IndexOutOfBoundsException, NullPointerException
Error exceptions:
The third kind of exception is the error. These are exceptional conditions that are external to the application, and that the application usually cannot anticipate or recover from. For example, suppose that an application successfully opens a file for input, but is unable to read the file because of a hardware or system malfunction. The unsuccessful read will throw java.io.IOError. An application might choose to catch this exception, in order to notify the user of the problem — but it also might make sense for the program to print a stack trace and exit.
Errors are not subject to the Catch or Specify Requirement. Errors are those exceptions indicated by Error and its subclasses.
Try & catch: Exceptions that are thrown by java runtime systems can be handled by Try and catch blocks.

The finally Block
The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.
The Throw clause can be used in any part of code where you feel a specific exception needs to be thrown to the calling method.
You can also create your own exception classes to represent problems that can occur within the classes you write. In fact, if you are a package developer, you might have to create your own set of exception classes to allow users to differentiate an error that can occur in your package from errors that occur in the Java platform or other packages.
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.
Chained Exceptions:
An application often responds to an exception by throwing another exception. In effect, the first exception causes the second exception. It can be very helpful to know when one exception causes another. Chained Exceptions help the programmer do this.

You are trying to read a number from the disk and using it to divide a number. Think the method throws an ArithmeticException because of an attempt to divide by zero (number we got). However, the problem was that an I/O error occurred, which caused the divisor to be set improperly (set to zero). 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. This is the place where chained exceptions come in to picture.
The following are the methods and constructors in Throwable that support chained exceptions.
Throwable getCause()
Throwable initCause(Throwable)
Throwable(String, Throwable)
The Throwable argument to initCause and the Throwable constructors is the exception that caused the current exception. getCause returns the exception that caused the current exception, and initCause sets the current exception's cause.
Customizing and Writing your Own

If you've derived your own classes from other libraries, making your own exceptions is fairly straightforward. We'll begin by creating the exception classes used in the previous example:
public class MyCheckedException extends Exception{
//It can also extend RuntimeException if you need it to be unchecked

Consequently, you also have access to features of the Exception class. Such as an error message. You can write your own constructor and pass whatever error message you want to it, creating your own personal error messages for effective error reporting and debugging. You can even pass the constructor another Throwable such as a RuntimeException or other Exception that caused this error in the first place. The implications of this will be covered in a future article, but this data can be abstracted by other functions to give a more accurate picture of what caused the problem.
public class MyCheckedException extends Exception{
public MyCheckedException(String msg){

public MyCheckedException(String msg, Throwable t){

Rethrowing an exception
Sometimes you’ll want to rethrow the exception that you just caught, particularly when you use Exception to catch any exception. Since you already have the reference to the current exception, you can simply rethrow that reference:
catch(Exception e) {
System.err.println("An exception was thrown");
throw e;
Rethrowing an exception causes it to go to the exception handlers in the next-higher context. Any further catch clauses for the same try block are still ignored. In addition, everything about the exception object is preserved, so the handler at the higher context that catches the specific exception type can extract all the information from that object.

Assertions: Assertions let you test your assumptions during development, but the assertion
code—in effect—evaporates when the program is deployed,
Assertions work quite simply. You always assert that something is true. If it is, no
problem. Code keeps running. But if your assertion turns out to be wrong (false),
then a stop-the-world AssertionError is thrown
Assertions come in two flavors: simple and really simple, as follows:
Really Simple
private void doStuff() {
assert (y > x);
// more code assuming y is greater than x
private void doStuff() {
assert (y > x): "y is " + y " " x is " + x;
// more code assuming y is greater than x
The difference between them is that the simple version adds a second expression,
separated from the first (boolean expression) by a colon, that adds a little more
information to the stack trace.

Assertion checking defaults to off at runtime. You should always turn them on.
String Class:
StringBuffer Class: The StringBuffer class should be used when you have to make a lot of modifications to strings of characters.String objects are
immutable, so if you choose to do a lot of manipulations with String objects, you will end up with a lot of abandoned String objects in the String pool. On the other hand, objects of type StringBuffer can be modified over and over again without leaving behind a great effluence of discarded String objects.
StringBuffer sb = new StringBuffer("abc");
StringBuffer objects are changeable
Important Methods in the StringBuffer Class:
public synchronized StringBuffer append(String s);
public synchronized StringBuffer insert(int offset, String s);
public synchronized StringBuffer reverse();
public String toString();

String is immutable whereas StringBuffer and StringBuilder can change their values.
The only difference between StringBuffer and StringBuilder is that StringBuilder is unsynchronized whereas StringBuffer is synchronized. So when the application needs to be run only in a single thread then it is better to use StringBuilder. StringBuilder is more efficient than StringBuffer.
java.lang.Math Class:

Wrapper Classes:
There is a wrapper class for every primitive in Java.
Wrapper objects are immutable. Once they have been given a value, that value cannot be
Creating Wrapper Objects:
Integer i1 = new Integer(42);
Integer i2 = new Integer("42");
The valueOf() Methods: Integer i2 = Integer.valueOf("101011", 2);
Wrapper to primitives:
Integer i2 = new Integer(42); // make a new wrapper object
byte b = i2.byteValue();
double d4 = Double.parseDouble("3.14");

Java Reflection API:
It allows the user to get the complete information about interfaces, classes, constructors, fields and various methods being used.
Class c = Class.forName(args[0]);
Method m[] = c.getDeclaredMethods();
2) Class cls = Class.forName("A");
boolean b1 = cls.isInstance(new Integer(37));
3) Method m = methlist[i];
System.out.println("name = " + m.getName());
// returns method name

4) SOP("decl class = " + m.getDeclaringClass());
// returns class name

5) Class pvec[] = m.getParameterTypes();
//return all parameters types

6) m.getReturnType()
//Returns return type of the method.

7) Constructor ctorlist[] = cls.getDeclaredConstructors();
//Return declared constructor list

8) ct.getDeclaringClass()
//return declaring class

9) Class cls = Class.forName("field1");

Field fieldlist[] = cls.getDeclaredFields();
10) Object retobj = ct.newInstance(arglist);
// Create object

Java Collection Classes

Collections: A collection — sometimes called a container — is simply an object that groups multiple elements into a single unit. Collections are used to store, retrieve, manipulate, and communicate aggregate data.
The Collections Framework is made up of a set of interfaces for working with groups of objects. The different interfaces describe the different types of groups.
Benefits of the Java Collections Framework
• Reduces programming effort
• Increases program speed and quality:
• Allows interoperability among unrelated APIs:
• Reduces effort to learn and to use new APIs:
• Reduces effort to design new APIs:
The following list describes the core collection interfaces:

Ordered When a collection is ordered, it means you can iterate through the collection in a specific (not-random) order.
Sorted:a sorted collection means a collection sorted by natural order.
Collection — the root of the collection hierarchy, Some types of collections allow duplicate elements, and others do not. Some are ordered and others are unordered. The Java platform doesn't provide any direct implementations of this interface but provides implementations of more specific subinterfaces, such as Set and List.
A Collection represents a group of objects known as its elements.
int size();
boolean isEmpty();
boolean contains(Object element);
boolean add(E element); //optional
boolean remove(Object element); //optional
Iterator iterator();

// Bulk operations
boolean containsAll(Collection c);
boolean addAll(Collection c); //optional
boolean removeAll(Collection c); //optional
boolean retainAll(Collection c); //optional
void clear(); //optional

List — A List cares about the index so list is an ordered collection (sometimes called a sequence). Lists can contain duplicate elements.
ArrayList: is ordered and unsynchronized collection.Think of this as a growable array. It gives you fast iteration and fast random access. an ArrayList is a variable-length array of object references. That is, an ArrayList can dynamically increase or decrease in size. Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk.
add(o) Appends the specified element to the end of this list.
public void add(int index, E element) : Inserts the specified element at the specified position in this
*list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
public E remove(int index): Removes the element at the specified position in this list.
* Shifts any subsequent elements to the left (subtracts one from their indices).

Vector: Synchronized order collection. Vector and ArrayList both uses Array internally as data structure.Null can be added to Vector

public synchronized E firstElement()
public synchronized void addElement(E obj)
public synchronized E lastElement()
public synchronized boolean removeElement(Object obj)

LinkedList: permits all elements (including null),and provided extra methods get,remove,insert an element at the beginning and end of the list. These operations allow linked lists to be used as a stack, queue, or double-ended queue (deque).

Sorted Set
A SortedSet is a Set that maintains its elements in ascending order. Several additional operations are provided to take advantage of the ordering. The SortedSet interface is used for things like word lists and membership rolls.
A SortedMap is a Map that maintains its mappings in ascending key order. It is the Map analogue of SortedSet. The SortedMap interface is used for apps like dictionaries and telephone directories.

Set — a collection that cannot contain duplicate elements.unOrdered collection.
HashSet: It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits the null element.
TreeSet: This class guarantees that the sorted set will be in ascending element order, sorted according to the natural order of the elements (see Comparable), or by the comparator provided at set creation time, depending on which constructor is used.
LinkedHashSet: which is the order in which elements were inserted into the set (insertion-order).
Map — an object that maps keys to values. A Map cannot contain duplicate keys;
HashTable: Any non-null object can be used as a key or as a value. To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method. HashTable is synchronized,
HashMap: permits null values and the null key. (The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.) This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

LinkedHashMap: with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).

TreeMap You can probably guess by now that a TreeMap is a sorted Map. And
you already know that this means “sorted by the natural order of the elements

Benefits and constraints of using different data structures.
• Data access is fast.
• Good for ordered data, which is not changed or searched frequently. • Inefficient if number of elements grow.
• Inefficient if an element to be inserted in middle of collection.
• Provides no special search mechanism.
Linked List
• Allows efficient inserts/delete at any location
• Allows arbitrary growth of collection.
• Applying order to the elements is easy.
• Slower while accessing elements by index.
• No special search mechanism is provided.
• Easy addition/deletion in middle.
• Allows arbitrary growth.
• A better and efficient search mechanism.
• Ordering is peculiar and some comparison mechanism is required.
• Searching is not efficient for unevenly distributed data.
• Efficient searching.
• Good access mechanism.
• Allows arbitrary growth of collection.
• Not good for small data set because of overheads.
• Overhead for storing keys for the values in collection.
• Overhead of hashing scheme.

Enumeration Interface: The Enumeration interface defines a way to traverse all the members of a collection of objects.
The hasMoreElements() method checks to see if there are more elements and returns a boolean.
If there are more elements, nextElement() will return the next element as an Object.
If there are no more elements when nextElement() is called, the runtime NoSuchElementException will be thrown.
Enumeration e = v.elements();
while (e.hasMoreElements()) {
Object o = e.nextElement();

Iterators - Similar to the familiar Enumeration interface, but more powerful, and with improved method names.
Iterator - In addition to the functionality of the Enumeration interface, allows the user to remove elements from the backing collection with well defined, useful semantics.
The Iterator interface has the following methods:
• hasNext.
• next.
• Remove
Iterator i = c.iterator();
while (i.hasNext()) {

ListIterator - Iterator for use with lists. In addition to the functionality of the Iterator interface, supports bi-directional iteration, element replacement, element insertion and index retrieval.
ListIterator iter = list.listIterator();
• hasNext()
• next()
• hasPrevious()
• previous()
• nextIndex()
• previousIndex()
• remove()
• set(E)
• add(E)

Comparable - Imparts a natural ordering to classes that implement it. The natural ordering may be used to sort a list or maintain order in a sorted set or map. Many classes have been retrofitted to implement this interface.
List of objects that implement this interface can be sorted automatically by sort method of the list interface. This interface has compareTo() method that is used by the sort() method of the list.
In this code Employee class is implementing Comparable interface and have method compareTO(). ComparableDemo.java is showing the use of this interface. This class first makes a list of objects of type Employee and call sort method of java.util.Collections, which internally uses compareTo() method of Employee class and sort the list accordingly.
This method compares this object with o1 object. Returned int value has the following meanings.
1. positive – this object is greater than o1
2. zero – this object equals to o1
3. negative – this object is less than o1
java.util.Collections.sort(List) and java.util.Arrays.sort(Object[]) methods can be used to sort using natural ordering of objects.

• Employee.java
public class Employee implements Comparable {

int EmpID;
String Ename;
double Sal;
static int i;

public Employee() {
EmpID = i++;
Ename = "dont know";
Sal = 0.0;

public Employee(String ename, double sal) {
EmpID = i++;
Ename = ename;
Sal = sal;

public String toString() {
return "EmpID " + EmpID + "\n" + "Ename " + Ename + "\n" + "Sal" + Sal;

public int compareTo(Object o1) {
if (this.Sal == ((Employee) o1).Sal)
return 0;
else if ((this.Sal) > ((Employee) o1).Sal)
return 1;
return -1;
• ComparableDemo.java
import java.util.*;

public class ComparableDemo{

public static void main(String[] args) {

List ts1 = new ArrayList();
ts1.add(new Employee ("Tom",40000.00));
ts1.add(new Employee ("Harry",20000.00));
ts1.add(new Employee ("Maggie",50000.00));
ts1.add(new Employee ("Chris",70000.00));
Iterator itr = ts1.iterator();

Object element = itr.next();
System.out.println(element + "\n");


Comparator - Represents an order relation, which may be used to sort a list or maintain order in a sorted set or map. Can override a type's natural ordering, or order objects of a type that does not implement the Comparable interface.
Sorting by other fields
If we need to sort using other fields of the employee, we’ll have to change the Employee class’s compareTo() method to use those fields. But then we’ll loose this empId based sorting mechanism. This is not a good alternative if we need to sort using different fields at different occasions. But no need to worry; Comparator is there to save us.

By writing a class that implements the java.lang.Comparator interface, you can sort Employees using any field as you wish even without touching the Employee class itself; Employee class does not need to implement java.lang.Comparable or java.lang.Comparator interface.
Sorting by name field
Following EmpSortByName class is used to sort Employee instances according to the name field. In this class, inside the compare() method sorting mechanism is implemented. In compare() method we get two Employee instances and we have to return which object is greater.
public class EmpSortByName implements Comparator{

public int compare(Employee o1, Employee o2) {
return o1.getName().compareTo(o2.getName());

Watch out: Here, String class’s compareTo() method is used in comparing the name fields (which are Strings).

Now to test this sorting mechanism, you must use the Collections.sort(List, Comparator) method instead of Collections.sort(List) method. Now change the TestEmployeeSort class as follows. See how the EmpSortByName comparator is used inside sort method.
import java.util.*;

public class TestEmployeeSort {

public static void main(String[] args) {

List coll = Util.getEmployees();
//use Comparator implementation
Collections.sort(coll, new EmpSortByName());

private static void printList(List list) {
for (Employee e: list) {
System.out.println(e.getEmpId() + "\t" + e.getName() + "\t" + e.getAge());

Now the result would be as follows. Check whether the employees are sorted correctly by the name String field. You’ll see that these are sorted alphabetically.
EmpId Name Age
6 Bill 34
4 Clerk 16
5 Frank 28
1 Jorge 19
8 Lee 40
2 Mark 30
3 Michel 10
7 Simp 8
Sorting by empId field
Even the ordering by empId (previously done using Comparable) can be implemented using Comparator; following class
does that.
public class EmpSortByEmpId implements Comparator{

public int compare(Employee o1, Employee o2) {
return o1.getEmpId().compareTo(o2.getEmpId());

Good website for collections:

Inner classes: define one class within another.
Inner Classes r 4 types:

• Inner Classes :
A “regular” inner class is declared inside the curly braces of another class, but outside any method or other code block. Can be declared abstract or final,p,p,p,strictfp

 gives the inner class access to all of the outer class’ members, including those marked private.
->From code within the enclosing class, you can instantiate the inner class using only the name of the inner class, as follows: MyInner mi = new MyInner();

From code outside the enclosing class’ instance methods, you can instantiate the inner class only by using both the inner and outer class names, and a reference to the outer class as follows:
MyOuter mo = new MyOuter();
MyOuter.MyInner inner = mo.new MyInner();
This usage
class MyInner {
public void seeOuter() {
System.out.println("Outer x is " + x);
System.out.println("Inner class ref is " + this);
System.out.println("Outer class ref is " + MyOuter.this);

Method-local Inner Classes

Anonymous Inner Classes
An anonymous class in Java is a class with no specified name declared and instantiated at the same time. Because it has no name it can only be used once at place of declaration. Anonymous classes are given a name by the compiler and they are treated as local inner classes. This means that anonymous classes can access members of the enclosing class regardless of their access modifiers and they can access final variables declared in the enclosing block of code.
Static Nested Classes:

Threads: There are two distinct types of multitasking: process-based and thread-based. process-based multitasking is the feature that allows your computer to run two or more programs concurrently.
a program can contain more than one thread, a single program can use multiple threads to perform two or more tasks at once.

To create a new thread, your program will either implement the Runnable interface or extend Thread. Both Runnable and Thread are packaged in java.lang. Thus, they are automatically available to all programs.
There are two ways to create a thread. The first is to declare a class that extends Thread. When the class is instantiated, the thread and object are created together and the object is automatically bound to the thread. By calling the object's start() method, the thread is started and immediately calls the object's run() method. Here is some code to demonstrate this method.
// This class extends Thread
class BasicThread1 extends Thread {
// This method is called when the thread runs
public void run() {
// Create and start the thread
Thread thread = new BasicThread1();
The second way is to create the thread and supply it an object with a run() method. This object will be permanently associated with the thread. The object's run() method will be invoked when the thread is started. This method of thread creation is useful if you want many threads sharing an object. Here is an example that creates a Runnable object and then creates a thread with the object.
class BasicThread2 implements Runnable {
// This method is called when the thread runs
public void run() {
// Create the object with the run() method
Runnable runnable = new BasicThread2();

// Create the thread supplying it with the runnable object
Thread thread = new Thread(runnable);

// Start the thread

The Thread Scheduler
The thread scheduler is the part of the JVM that decides which thread should run at any given moment, Any thread in the runnable state can be chosen by the scheduler to be the one and only running thread.

Yielding to other processes
A CPU intensive operation being executed may not allow other threads to be executed for a "large" period of time. To prevent this it can allow other threads to execute by invoking the yield() method. The thread on which yield() is invoked would move from running state to ready state.
For synchronizing a HashMap, you can use Collections.synchronizedMap() which will return a synchronized map for you, which is thread-safe.
Remember, synchronization will cause a performance problem. So, it needs to be used carefully, when really required.
wait(), notify(), and notifyAll() must be called from within a synchronized context! A thread can’t invoke a wait or notify method on an object unless it owns that object’s lock.

Deadlocking can occur when a locked object attempts to access another locked
object that is trying to access the first locked object. In other words, both
threads are waiting for each other’s locks to be released; therefore, the locks
will never be released! ThreadMXBean bean = ManagementFactory.getTreadMXBean();
bean.findMonitorDedlockedThreads(); To find all deadlocked threads.

Synchronized :
Java also offers three ways to define synchronized blocks.
Synchronized Class Method:
class class_name {
static synchronized type method_name() {
statement block
All the statements in the method become the synchronized block, and the class object is the lock.
Synchronized Instance Method:
class class_name {
synchronized type method_name() {
statement block
All the statements in the method become the synchronized block, and the instance object is the lock.
Synchronized Statement:
class class_name {
type method_name() {
synchronized (object) {
statement block
All the statements specified in the parentheses of the synchronized statement become the synchronized block, and the object specified in the statement is the lock.
Java applys the synchronization rule by assigning the ownership of the lock's monitor to the threads that are running the synchronized blocks. Here is how it works:
• When a synchronized clock is reached in an execution thread, it will try to gain the ownership of the monitor of the lock object. If another thread owns the lock's monitor, it will wait.
• Once the lock's monitor is free, the waiting thread will become the owner of the lock's monitor, and start to execute the synchronized block.
• Once the synchronized block is executed to the end, the lock's monitor will be freed again.
Note that one program can have many locks and each lock can be associated with many different synchronized blocks. But the synchronization rule only applies between the synchronized block and its associated lock.
For example, the following code defines two synchronized blocks. Both are associated with the same lock, the instance object.
class class_name {
type method_name() {
synchronized (this) {
statement block 1
synchronized type method_name() {
statement block 2
Block 1 will never be executed at the same time as block 2.
The following code defines two synchronized blocks. But they are associated with two different locks, one is the class object, and the other is the instance object. Those two synchronized blocks will never wait for each other.
class class_name {
type method_name() {
synchronized (this) {
statement block 1
static synchronized type method_name() {
statement block 2

Deadlocking can occur when a locked object attempts to access another locked object that is trying to access the first locked object. In other words, both threads are waiting for each other’s locks to be released; therefore, the locks will never be released!

Observer and Observable

How are Observer and Observable usedObjects that subclass the Observable class maintain a list of observers. When anObservable object is updated it invokes the update() method of each of its observers to notify the observers that it has changed state. The Observer interface is implemented by objects that observe Observable objects.

Annotations in Java 5.0
Annotations are generally a way to add meta-data information to a java element (an element can be a class, method, field, or anything) and these meta-data are processed by the tools (compilers, javadoc, etc.). Annotations are differentiated from other elements like class, interface etc., by preceding an '@' symbol before it.
Consider the following class definition,
Employee. java

final class Employee
private String name;
private String id;

// Getters and setters for Employee class.

The above is a definition of a class, Employee. It can be noted that the class declaration is preceded with the final keyword which tells that this class cannot be sub-classed. So, the introduction of the final keyword adds some additional information (or adds some constraints) over the class definition telling that no other class in the word can extend the Employee class. Therefore, the final keyword forms a part in providing meta-data information in the class definition. So, this is one variation of Annotation.
Annotations can be applied to a program's declarations of classes, fields, methods, and other program elements.
Annotations Used by the Compiler
There are three annotation types that are predefined by the language specification itself: @Deprecated, @Override, and @SuppressWarnings.
@Deprecated—the @Deprecated annotation indicates that the marked element is deprecated and should no longer be used. The compiler generates a warning whenever a program uses a method, class, or field with the @Deprecated annotation. When an element is deprecated, it should also be documented using the Javadoc @deprecated tag, as shown in the following example. The use of the "@" symbol in both Javadoc comments and in annotations is not coincidental—they are related conceptually. Also, note that the Javadoc tag starts with a lowercase "d" and the annotation starts with an uppercase "D".
// Javadoc comment follows
* @deprecated
* explanation of why it was deprecated
static void deprecatedMethod() { }
@Override—the @Override annotation informs the compiler that the element is meant to override an element declared in a superclass (overriding methods will be discussed in the the lesson titled "Interfaces and Inheritance").
// mark method as a superclass method
// that has been overridden
int overriddenMethod() { }
While it's not required to use this annotation when overriding a method, it helps to prevent errors. If a method marked with @Override fails to correctly override a method in one of its superclasses, the compiler generates an error.
@SuppressWarnings—the @SuppressWarnings annotation tells the compiler to suppress specific warnings that it would otherwise generate. In the example below, a deprecated method is used and the compiler would normally generate a warning. In this case, however, the annotation causes the warning to be suppressed.
// use a deprecated method and tell
// compiler not to generate a warning
void useDeprecatedMethod() {
objectOne.deprecatedMethod(); //deprecation warning - suppressed
Every compiler warning belongs to a category. The Java Language Specification lists two categories: "deprecation" and "unchecked." The "unchecked" warning can occur when interfacing with legacy code written before the advent of generics (discussed in the lesson titled "Generics"). To suppress more than one category of warnings, use the following syntax:
@SuppressWarnings({"unchecked", "deprecation"})
Serialization is the process of saving an object's state to a sequence of bytes; deserialization is the process of rebuilding those bytes into a live object.
The Java Serialization API provides a standard mechanism for developers to handle object serialization.
Why is serialization required?
In today's world, a typical enterprise application will have multiple components and will be distributed across various systems and networks. In Java, everything is represented as objects; if two Java components want to communicate with each other, there needs be a mechanism to exchange data. One way to achieve this is to define your own protocol and transfer an object. This means that the receiving end must know the protocol used by the sender to re-create the object, which would make it very difficult to talk to third-party components. Hence, there needs to be a generic and efficient protocol to transfer the object between components. Serialization is defined for this purpose, and Java components use this protocol to transfer objects.
How to serialize an object
In order to serialize an object, you need to ensure that the class of the object implements the java.io.Serializable interface, as shown in Listing 1.
Listing 1. Implementing Serializable
import java.io.Serializable;

class TestSerial implements Serializable {
public byte version = 100;
public byte count = 0;

In Listing 1, the only thing you had to do differently from creating a normal class is implement the java.io.Serializable interface. The Serializable interface is a marker interface; it declares no methods at all. It tells the serialization mechanism that the class can be serialized.
Now that you have made the class eligible for serialization, the next step is to actually serialize the object. That is done by calling the writeObject() method of the java.io.ObjectOutputStream class, as shown in Listing 2.
Listing 2. Calling writeObject()
public static void main(String args[]) throws IOException {
FileOutputStream fos = new FileOutputStream("temp.out");
ObjectOutputStream oos = new ObjectOutputStream(fos);
TestSerial ts = new TestSerial();
Three ways to customize serialization
mark sensitive fields as transient
implement Serializable and override
readObject() and writeObject()
implement Externalizable and override
writeExternal() and readExternal()
public class SalesEntry implements Serializable {
private void writeObject(ObjectOutputStream out) throws IOException {
// Write all fields that are not transient or static,
// including those in base classes.
// Write transient field baseSalary as an encrypted String.
private void readObject(ObjectInputStream in) throws IOException {
// Read all fields that are not transient or static
// including those in base classes.
// Read transient field baseSalary from an encrypted String.
baseSalary = MyEncrypter.decryptInt(in.readUTF());
writeExternal() &readExternal()
public class SalesEntry implements Externalizable {
// A public no-arg constructor is required by
// Externalizable.readExternal.
public SalesEntry() {

// Method in the Externalizable interface.
public void writeExternal(ObjectOutput out) throws IOException {
// Can't use ObjectOutputStream.defaultWriteObject().
// If base class fields require serialization
// then code to perform that must be supplied here.
out.writeObject(date); // use for objects and arrays

// Method in the Externalizable interface.
public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
// Can't use ObjectInputStream.defaultReadObject().
// If base class fields require deserialization
// then code to perform that must be supplied here.
name = in.readUTF();
date = (Date) in.readObject(); // use for objects and arrays
sales = in.readFloat();
baseSalary = MyEncrypter.decryptInt(in.readUTF());

JDBC Programming:
The JDBC ( Java Database Connectivity) API defines interfaces and classes for writing database applications in Java by making database connections. Using JDBC you can send SQL, PL/SQL statements to almost any relational database.
JDBC Steps :
1.Loading a database driver: In this step of the jdbc connection process, we load the driver class by calling Class.forName() with the Driver class name as an argument. Class.forName(”sun.jdbc.odbc.JdbcOdbcDriver”);

2. Creating a oracle jdbc Connection: The JDBC DriverManager class defines objects which can connect Java applications to a JDBC driver.

Its getConnection() method is used to establish a connection to a database. It uses a username, password, and a jdbc url to establish a connection to the database and returns a connection object.

Connection dbConnection=DriverManager.getConnection(url,”loginName”,”Password”)
3. Creating a jdbc Statement object: Once a connection is obtained we can interact with the database. Connection interface defines methods for interacting with the database via the established connection. To execute SQL statements, you need to instantiate a Statement object from your connection object by using the createStatement() method.
Statement statement = dbConnection.createStatement();
A statement object is used to send and execute SQL statements to a database.
Three kinds of Statements
Statement: Execute simple sql queries without parameters.
Statement createStatement()
Creates an SQL Statement object.
Prepared Statement: Execute precompiled sql queries with or without parameters.
PreparedStatement prepareStatement(String sql)
returns a new PreparedStatement object. PreparedStatement objects are precompiled
SQL statements.
An example of a PreparedStatement object is
PreparedStatement pstmt = con.prepareStatement(”update Orders set pname = ? where Prod_Id = ?”);

pstmt.setInt(2, 100);
pstmt.setString(1, “Bob”);

Callable Statement: Execute a call to a database stored procedure.
CallableStatement prepareCall(String sql)
returns a new CallableStatement object. CallableStatement objects are SQL stored procedure call statements.
4. Executing a SQL statement with the Statement object, and returning a jdbc resultSet.
Statement interface defines methods that are used to interact with database via the execution of SQL statements. The Statement class has three methods for executing statements:
executeQuery(), executeUpdate(), and execute(). For a SELECT statement, the method to use is executeQuery . For statements that create or modify tables, the method to use is executeUpdate. Note: Statements that create a table, alter a table, or drop a table are all examples of DDL
statements and are executed with the method executeUpdate. execute() executes an SQL
statement that is written as String object.
ResultSet provides access to a table of data generated by executing a Statement. The table rows are retrieved in sequence. A ResultSet maintains a cursor pointing to its current row of data. The next() method is used to successively step through the rows of the tabular results.
A cursor can be thought of as a pointer to the rows of the result set that has the ability to keep track of which row is currently being accessed. The JDBC API supports a cursor to move both forward and backward and also allowing it to move to a specified row or to a row whose position is relative to another row.
Types of ResultSets :
The sensitivity of the ResultSet object is determined by one of three different ResultSet types:
TYPE_FORWARD_ONLY — the result set is not scrollable i.e. the cursor moves only forward, from before the first row to after the last row.
TYPE_SCROLL_INSENSITIVE — the result set is scrollable; its cursor can move both forward and backward relative to the current position,
and it can move to an absolute position.
TYPE_SCROLL_SENSITIVE — the result set is scrollable; its cursor can move both forward and backward relative to the current position, and it can move to an absolute position.
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet srs = stmt.executeQuery(”…..”);
The first argument is one of three constants added to the ResultSet API to indicate the type of a ResultSet object: TYPE_FORWARD_ONLY, TYPE_SCROLL_INSENSITIVE, and TYPE_SCROLL_SENSITIVE. The second argument is one of two ResultSet constants for specifying whether a result set is read-only or updatable: CONCUR_READ_ONLY and CONCUR_UPDATABLE. If you do not specify any constants for the type and updatability of a ResultSet object, you will automatically get one that is TYPE_FORWARD_ONLY and CONCUR_READ_ONLY.
next() - moves the cursor forward one row. Returns true if the cursor is now positioned on a row and false if the cursor is positioned after the last row.
previous() - moves the cursor backwards one row. Returns true if the cursor is now positioned on a row and false if the cursor is positioned before the first row.
first() - moves the cursor to the first row in the ResultSet object. Returns true if the cursor is now positioned on the first row and false if the ResultSet object
does not contain any rows.
last() - moves the cursor to the last row in the ResultSet object. Returns true if the cursor is now positioned on the last row and false if the ResultSet object
does not contain any rows.
beforeFirst() - positions the cursor at the start of the ResultSet object, before the first row. If the ResultSet object does not contain any rows, this method has
no effect.
afterLast() - positions the cursor at the end of the ResultSet object, after the last row. If the ResultSet object does not contain any rows, this method has no effect.
relative(int rows) - moves the cursor relative to its current position.
absolute(int n) - positions the cursor on the n-th row of the ResultSet object.
ResultSetMetaData Interface holds information on the types and properties of the columns in a ResultSet. It is constructed from the Connection object.
Types Of Drivers : There are four types
A).JDBC-ODBC Bridge Driver(Bridge):

The Type 1 driver translates all JDBC calls into ODBC calls and sends them to the ODBC driver. ODBC is a generic API. The JDBC-ODBC Bridge driver is recommended only for experimental use or when no other alter native is available.
Advantage :
The JDBC-ODBC Bridge allows access to almost any database, since the database’s ODBC drivers are already available.
1. Since the Bridge driver is not written fully in Java, Type 1 drivers are not portable.
2. A performance issue is seen as a JDBC call goes through the bridge to the ODBC driver, then to the database, and this applies even in the reverse process. They are the slowest of all driver types.
3. The client system requires the ODBC Installation to use the driver.
4. Not good for the Web.

b. Native API Partly Java Driver(Native)
The distinctive characteristic of type 2 jdbc drivers are that Type 2 drivers convert JDBC calls into database-specific calls i.e. this driver is specific to a particular database. Some distinctive characteristic of type 2 jdbc drivers are shown below. Example: Oracle will have oracle native api.
The distinctive characteristic of type 2 jdbc drivers are that they are typically offer better performance than the JDBC-ODBC Bridge as the layers of communication (tiers) are less than that of Type
1 and also it uses Native api which is Database specific.

1. Native API must be installed in the Client System and hence type 2 drivers cannot be used for the Internet.
2. Like Type 1 drivers, it’s not written in Java Language which forms a portability issue.
3. If we change the Database we have to change the native api as it is specific to a database
4. Mostly obsolete now
5. Usually not thread safe.

c. Net protocol pure Java Driver(Middleware)
Type 3 database requests are passed through the network to the middle-tier server. The middle-tier then translates the request to the database. If the middle-tier server can in turn use Type1, Type 2 or Type 4 drivers.
1. This driver is server-based, so there is no need for any vendor database library to be present on client machines.
2. This driver is fully written in Java and hence Portable. It is suitable for the web.
3. There are many opportunities to optimize portability, performance, and scalability.
4. The net protocol can be designed to make the client JDBC driver very small and fast to load.
5. The type 3 driver typically provides support for features such as caching (connections, query results, and so on), load balancing, and advanced
system administration such as logging and auditing.
6. This driver is very flexible allows access to multiple databases using one driver.
7. They are the most efficient amongst all driver types.

It requires another server application to install and maintain. Traversing the recordset may take longer, since the data comes through the backend server.
d. Native protocol Pure Java Driver(pure)
The Type 4 uses java networking libraries to communicate directly with the database server.
1. The major benefit of using a type 4 jdbc drivers are that they are completely written in Java to achieve platform independence and eliminate deployment administration issues. It is most suitable for the web.
2. Number of translation layers is very less i.e. type 4 JDBC drivers don’t have to translate database requests to ODBC or a native connectivity interface or to pass the request on to another server, performance is typically quite good.
3. You don’t need to install special software on the client or server. Further, these drivers can be downloaded dynamically.

With type 4 drivers, the user needs a different driver for each database.

When we create an instace of a class using new operator, it does two things

1. Load the class in to memory, if it is not loaded -
which means creating in-memory representation of the class from the .class file so that an instance can be created out of it. This includes initializing static variables (resolving of that class)
2. create an instance of that class and store the reference to the variable.

Class.forName does only the first thing.
It loads the class in to memory and return that reference as an instance of Class. If we want to create an instance then, we can call newInstance method of that class. which will invoke the default constructor (no argument constructor).
Note that if the default constructor is not accessible, then newInstance method will throw an IllegalAccessException. and if the class is an abstract class or interface or it does not have a default constructor, then it will throw an InstantiationException. If any exception araises during resolving of that class, it will throw an ExceptionInInitializerError.

If the default constructor is not defined, then we have to invoke the defiend constructor using reflection API.

But the main advantage with Class.forName is, it can accept the class name as a String argument. So we can pass the class name dynamically. But if we create an instance of a class using new operator, the class name can't be changed dynamically.

Class.forName() in turn will call loadClass method of the caller ClassLoader (ClassLoder of the class from where Class.forName is invoked).

By default, the Class.forName() resolve that class. which means, initialize all static variables inside that class.
same can be changed using the overloaded method of Class.forName(String name,boolean initialize,ClassLoader loader)

The main reason for loading jdbc driver using Class.forName() is, the driver can change dynamically.
in the static block all Drivers will create an instance of itself and register that class with DriverManager using DriverManager.registerDriver() method. Since the Class.forName(String className) by default resolve the class, it will initialize the static initializer.
So when we call Class.forName("com.sun.jdbc.odbc.JdbcOdbcDriver"),
the Driver class will be loaded, instanciated and registers with DriverManager

So if you are using new Operator you have to do the following things.

Driver drv = new com.sun.jdbc.odbc.JdbcOdbcDriver();

Design patterns: a design pattern is a template for software development. The purpose of the template is to define a particular behavior or technique that can be used as a building block for the construction of software - to solve universal problems that commonly face developers.

Singleton Design Pattern
Restriction to a class to have one and only one instance. This way the developer can ensure as well as can control how an object is instantiated and can prevent others from creating or copying multiple instances of the class.

Purpose: The main purpose is that if we have to use a resource then we can create a singleton class which can only access the resource and by making this instance available to other threads or any part of the application we can make sure that unnecessary objects are not getting created for accessing the resource.
Another purpose is that we have some utility classes which are being used by other java classes to do some calculations etc. Now if we make the utility class a singleton class then all objects and all parts of the application can use this one instance of the utility class and no extra instaces are required. There are lots of other uses also.

public class SingletonObject
private SingletonObject()
// no code req'd

public static SingletonObject getSingletonObject()
if (ref == null)
// it's ok, we can call this constructor
ref = new SingletonObject();
return ref;

private static SingletonObject ref;

Preventing thread problems with your singleton
public static synchronized
SingletonObject getSingletonObject()

Here's where most articles on singletons fall down, because they forget about cloning. Examine the following code snippet, which clones a singleton object.
public class Clone
public static void main(String args[])
throws Exception
// Get a singleton
SingletonObject obj =

// Buahahaha. Let's clone the object
SingletonObject clone =
(SingletonObject) obj.clone();
Okay, we're cheating a little here. There isn't a clone() method defined in SingletonObject, but there is in the java.lang.Object class which it is inherited from. By default, the clone() method is marked as protected, but if your SingletonObject extends another class that does support cloning, it is possible to violate the design principles of the singleton. So, to be absolutely positively 100% certain that a singleton really is a singleton, we must add a clone() method of our own, and throw a CloneNotSupportedException if anyone dares try!
Here's the final source code for a SingletonObject, which you can use as a template for your own singletons.
public class SingletonObject
private SingletonObject()
// no code req'd

public static SingletonObject getSingletonObject()
if (ref == null)
// it's ok, we can call this constructor
ref = new SingletonObject();
return ref;

public Object clone()
throws CloneNotSupportedException
throw new CloneNotSupportedException();
// that'll teach 'em

private static SingletonObject ref;
Some other design patterens
Proxy Design Pattern

Composite view

Tuesday, August 18, 2009

Avoid Permgen space exception in Jboss and Eclipse

Hi All,
Add the following line of code in your eclipse under Jboss-arguments section.
1. Double click on the JBoss server which you already added to your eclipse.
2. Click on open launch configuration link.
3. After clicking this link one new window will open with edit launch
configuration properties.
4. In this window place the following code in your VM arguments Area section.
5.Click on save this(CTRL+S).

-Dprogram.name=run.bat -Djava.endorsed.dirs="C:/jboss-Location/jboss-eap-4.3/jboss-as/bin/../lib/endorsed" -Xms512m -Xmx512m -XX:MaxPermSize=128m

Jboss-Location means where is your jboss is placed.
My Jobss is there in C:/siva then replace this with Jboss_location.

There is another way to increase permgen space size.
1. open run.bat in either edit plus/wordpad/notepad
2. fist see the following code is commented or not ,find this is there or not
if it is commented uncommented, it is not there then paste it in run.bat file
set JAVA_OPTS=%JAVA_OPTS% -Xms128m -Xmx512m

I hope this will help you to avoid permgen space exception.

Saturday, March 14, 2009

Common Utility methods

1.Convert from Date object to String with given format
* Formats the date object in given format.
* @param aDate the date object
* @param aNewformat the new format
* @return string in new format.
public static String getFormattedDate(Date aDate, String aNewformat)
if (aDate != null)
SimpleDateFormat dateFormat = new SimpleDateFormat(aNewformat);
String formattedDate = dateFormat.format((aDate));
return formattedDate;
return "";
2.Convert Calendar To String with given format
* Formats the date object in given format.
* @param aCalendar the date object
* @param aNewformat the new format
* @return string in new format.
public static String getFormattedDate(Calendar aCalendar, String aNewformat) {
//For Ex: aNewformat =dd/mm/yyyy
if (aCalendar != null)
SimpleDateFormat dateFormat = new SimpleDateFormat(aNewformat);
String formattedDate = dateFormat.format((aCalendar.getTime()));
return formattedDate;
return "";
3.Convert Calendar to Date
* Checks calendar object for null and returns date object.
* @param aCalendar the calendar object to check.
* @return Date object or null
public static Date getTime(Calendar aCalendar)
if (aCalendar != null)
return aCalendar.getTime();
return null;
You Can Find More on SimpleDateFormat click here

Friday, January 30, 2009

JSP Interview Questions

What are the advantages of JSP over Servlet?
JSP is a serverside technology to make content generation a simple appear.The advantage of JSP is that they are document-centric. Servlets, on the other hand, look and act like programs. A Java Server Page can contain Java program fragments that instantiate and execute Java classes, but these occur inside an HTML template file and are primarily used to generate dynamic content. Some of the JSP functionality can be achieved on the client, using JavaScript. The power of JSP is that it is server-based and provides a framework for Web application development
2.What is the life-cycle of JSP?
When a request is mapped to a JSP page for the first time, it translates the JSP page into a servlet class and compiles the class. It is this servlet that services the client requests.
A JSP page has seven phases in its lifecycle, as listed below in the sequence of occurrence:
Loading the class
Instantiating the class
jspInit() invocation
_jspService() invocation
jspDestroy() invocation

3.What is the jspInit() method?

The jspInit() method of the javax.servlet.jsp.JspPage interface is similar to the init() method of servlets. This method is invoked by the container only once when a JSP page is initialized. It can be overridden by a page author to initialize resources such as database and network connections, and to allow a JSP page to read persistent configuration data
4.What is the _jspService() method?
SThe _jspService() method of the javax.servlet.jsp.HttpJspPage interface is invoked every time a new request comes to a JSP page. This method takes the HttpServletRequest and HttpServletResponse objects as its arguments. A page author cannot override this method, as its implementation is provided by the container.

5.What is the jspDestroy() method?
The jspDestroy() method of the javax.servlet.jsp.JspPage interface is invoked by the container when a JSP page is about to be destroyed. This method is similar to the destroy() method of servlets. It can be overridden by a page author to perform any cleanup operation such as closing a database connection.
6.What JSP lifecycle methods can I override?
You cannot override the _jspService() method within a JSP page. You can however, override the jspInit() and jspDestroy() methods within a JSP page. jspInit() can be useful for allocating resources like database connections, network connections, and so forth for the JSP page. It is good programming practice to free any allocated resources within jspDestroy().
7.How can I override the jspInit() and jspDestroy() methods within a JSP page?
The jspInit() and jspDestroy() methods are each executed just once during the lifecycle of a JSP page and are typically declared as JSP declarations:
<%! public void jspInit() { . . . } %>
<%! public void jspDestroy() { . . . } %>

8.What are implicit objects in JSP?
Implicit objects in JSP are the Java objects that the JSP Container makes available to developers in each page. These objects need not be declared or instantiated by the JSP author. They are automatically instantiated by the container and are accessed using standard variables; hence, they are called implicit objects.The implicit objects available in JSP are as follows:

The implicit objects are parsed by the container and inserted into the generated servlet code. They are available only within the jspService method and not in any declaration
9.What are the different types of JSP tags?

10.What are JSP directives?
JSP directives are messages for the JSP engine. i.e., JSP directives serve as a message from a JSP page to the JSP container and control the processing of the entire page
They are used to set global values such as a class declaration, method implementation, output content type, etc.
They do not produce any output to the client.
Directives are always enclosed within <%@ ….. %> tag.
Ex: page directive, include directive, etc.
11.What is page directive?
A page directive is to inform the JSP engine about the headers or facilities that page should get from the environment.
Typically, the page directive is found at the top of almost all of our JSP pages.
There can be any number of page directives within a JSP page (although the attribute – value pair must be unique).
The syntax of the include directive is: <%@ page attribute="value">
Example:<%@ include file="header.jsp" %>

12.What are the attributes of page directive?
There are thirteen attributes defined for a page directive of which the important attributes are as follows:
import: It specifies the packages that are to be imported.
session: It specifies whether a session data is available to the JSP page.
contentType: It allows a user to set the content-type for a page.
isELIgnored: It specifies whether the EL expressions are ignored when a JSP is translated to a servlet.

13.What is the include directive?
There are thirteen attributes defined for a page directive of which the important attributes are as follows:
The include directive is used to statically insert the contents of a resource into the current JSP.
This enables a user to reuse the code without duplicating it, and includes the contents of the specified file at the translation time.
The syntax of the include directive is as follows:
<%@ include file = "FileName" %>
This directive has only one attribute called file that specifies the name of the file to be included.

14.What are the JSP standard actions?
The JSP standard actions affect the overall runtime behavior of a JSP page and also the response sent back to the client.
They can be used to include a file at the request time, to find or instantiate a JavaBean, to forward a request to a new page, to generate a browser-specific code, etc.
Ex: include, forward, useBean,etc. object

15.What are the standard actions available in JSP?
The standard actions available in JSP are as follows:
jsp:include: It includes a response from a servlet or a JSP page into the current page. It differs from an include directive in that it includes a resource at request processing time, whereas the include directive includes a resource at translation time.
jsp:forward: It forwards a response from a servlet or a JSP page to another page.
jsp:useBean: It makes a JavaBean available to a page and instantiates the bean.
jsp:setProperty: It sets the properties for a JavaBean.
jsp:getProperty: It gets the value of a property from a JavaBean component and adds it to the response.
jsp:param: It is used in conjunction with jsp:forward;, jsp:, or plugin; to add a parameter to a request. These parameters are provided using the name-value pairs.
jsp:plugin: It is used to include a Java applet or a JavaBean in the current JSP page.

16.What is the jsp:useBean standard action?
The standard action is used to locate an existing JavaBean or to create a JavaBean if it does not exist. It has attributes to identify the object instance, to specify the lifetime of the bean, and to specify the fully qualified classpath and type.

17.What are the scopes available in ?
The scopes available in are as follows:

page scope:: It specifies that the object will be available for the entire JSP page but not outside the page.
request scope: It specifies that the object will be associated with a particular request and exist as long as the request exists.
application scope: It specifies that the object will be available throughout the entire Web application but not outside the application.
session scope: It specifies that the object will be available throughout the session with a particular client.
18.What is the standard action?
The standard action forwards a response from a servlet or a JSP page to another page.
The execution of the current page is stopped and control is transferred to the forwarded page.
The syntax of the standard action is :

Here, targetPage can be a JSP page, an HTML page, or a servlet within the same context.
If anything is written to the output stream that is not buffered before , an IllegalStateException will be thrown.
Note : Whenever we intend to use or in a page, buffering should be enabled. By default buffer is enabled.

19.What is the standard action?
The standard action enables the current JSP page to include a static or a dynamic resource at runtime. In contrast to the include directive, the include action is used for resources that change frequently. The resource to be included must be in the same context.The syntax of the standard action is as follows:

Here, targetPage is the page to be included in the current JSP.

20.What is the difference between include directive and include action?

Include directive Include action
The include directive, includes the content of the specified file during the translation phase–when the page is converted to a servlet. The include action, includes the response generated by executing the specified page (a JSP page or a servlet) during the request processing phase–when the page is requested by a user.
The include directive is used to statically insert the contents of a resource into the current JSP. The include standard action enables the current JSP page to include a static or a dynamic resource at runtime.
Use the include directive if the file changes rarely. It’s the fastest mechanism. Use the include action only for content that changes often, and if which page to include cannot be decided until the main page is requested.
Differentiate between pageContext.include and jsp:include?
The standard action and the pageContext.include() method are both used to include resources at runtime. However, the pageContext.include() method always flushes the output of the current page before including the other components, whereas flushes the output of the current page only if the value of flush is explicitly set to true as follows:

22.What is the jsp:setProperty action?

You use jsp:setProperty to give values to properties of beans that have been referenced earlier. You can do this in two contexts. First, you can use jsp:setProperty after, but outside of, a jsp:useBean element, as below:


Here, the jsp:setProperty is executed only if a new object was instantiated, not if an existing one was found.

23.What is the action?
The action is used to access the properties of a bean that was set using the action. The container converts the property to a String as follows:
If it is an object, it uses the toString() method to convert it to a String.
If it is a primitive, it converts it directly to a String using the valueOf() method of the corresponding Wrapper class.
The syntax of the method is:
Here, name is the id of the bean from which the property was set. The property attribute is the property to get. A user must create or locate a bean using the action before using the action.

24.What is the standard action?
The standard action is used with or to pass parameter names and values to the target resource. The syntax of the standard action is as follows:

25.What is the jsp:plugin action ?
This action lets you insert the browser-specific OBJECT or EMBED element needed to specify that the browser run an applet using the Java plugin.

26.What are scripting elements?
JSP scripting elements let you insert Java code into the servlet that will be generated from the current JSP page. There are three forms:
Expressions of the form <%= expression %> that are evaluated and inserted into the output,
Scriptlets of the form <% code %> that are inserted into the servlet's service method,
Declarations of the form <%! code %> that are inserted into the body of the servlet class, outside of any existing methods.

27.What is a scriptlet?

A scriptlet contains Java code that is executed every time a JSP is invoked. When a JSP is translated to a servlet, the scriptlet code goes into the service() method. Hence, methods and variables written in scriptlets are local to the service() method. A scriptlet is written between the <% and %> tags and is executed by the container at request processing time.

28.What are JSP declarations?

As the name implies, JSP declarations are used to declare class variables and methods in a JSP page. They are initialized when the class is initialized. Anything defined in a declaration is available for the whole JSP page. A declaration block is enclosed between the <%! and %> tags. A declaration is not included in the service() method when a JSP is translated to a servlet.

29.What is a JSP expression?

A JSP expression is used to write an output without using the out.print statement. It can be said as a shorthand representation for scriptlets. An expression is written between the <%= and %> tags. It is not required to end the expression with a semicolon, as it implicitly adds a semicolon to all the expressions within the expression tags.

30.How is scripting disabled?

Scripting is disabled by setting the scripting-invalid element of the deployment descriptor to true. It is a subelement of jsp-property-group. Its valid values are true and false. The syntax for disabling scripting is as follows:
url-pattern *.jsp url-pattern
scripting-invalid true scripting-invalid

Tuesday, January 27, 2009

Struts Interview Questions

What is MVC?
Model-View-Controller (MVC) is a design pattern put together to help control change. MVC decouples interface from business logic and data.
Model : The model contains the core of the application's functionality. The model encapsulates the state of the application. Sometimes the only functionality it contains is state. It knows nothing about the view or controller.
View: The view provides the presentation of the model. It is the look of the application. The view can access the model getters, but it has no knowledge of the setters. In addition, it knows nothing about the controller. The view should be notified when changes to the model occur.
Controller:The controller reacts to the user input. It creates and sets the model.
2.What is a framework?
A framework is made up of the set of classes which allow us to use a library in a best possible way for a specific requirement.
3.What is Struts framework?
Struts framework is an open-source framework for developing the web applications in Java EE, based on MVC-2 architecture. It uses and extends the Java Servlet API. Struts is robust architecture and can be used for the development of application of any size. Struts framework makes it much easier to design scalable, reliable Web applications with Java.
4.What are the components of Struts?
Struts components can be categorize into Model, View and Controller:
Model: Components like business logic /business processes and data are the part of model.
View: HTML, JSP are the view components.
Controller: Action Servlet of Struts is part of Controller components which works as front controller to handle all the requests.
5.What are the core classes of the Struts Framework?
Struts is a set of cooperating classes, servlets, and JSP tags that make up a reusable MVC 2 design.
JavaBeans components for managing application state and behavior.
Event-driven development (via listeners as in traditional GUI development).
Pages that represent MVC-style views; pages reference view roots via the JSF component tree.
6.What is ActionServlet?
ActionServlet is a simple servlet which is the backbone of all Struts applications. It is the main Controller component that handles client requests and determines which Action will process each received request. It serves as an Action factory – creating specific Action classes based on user’s request.

7.What is role of ActionServlet?

ActionServlet performs the role of Controller:
Process user requests
Determine what the user is trying to achieve according to the request
Pull data from the model (if necessary) to be given to the appropriate view,
Select the proper view to respond to the user
Delegates most of this grunt work to Action classes
Is responsible for initialization and clean-up of resources

8.What is the ActionForm?
ActionForm is javabean which represents the form inputs containing the request parameters from the View referencing the Action bean.

9.What are the important methods of ActionForm?
The important methods of ActionForm are : validate() & reset().

10.Describe validate() and reset() methods ?
validate() : Used to validate properties after they have been populated; Called before FormBean is handed to Action. Returns a collection of ActionError as ActionErrors. Following is the method signature for the validate() method.

public ActionErrors validate(ActionMapping mapping,HttpServletRequest request)

reset(): reset() method is called by Struts Framework with each request that uses the defined ActionForm. The purpose of this method is to reset all of the ActionForm's data members prior to the new request values being set.
public void reset() {}

11.What is ActionMapping?
Action mapping contains all the deployment information for a particular Action bean. This class is to determine where the results of the Action will be sent once its processing is complete.

12.How is the Action Mapping specified ?

We can specify the action mapping in the configuration file called struts-config.xml. Struts framework creates ActionMapping object from configuration element of struts-config.xml file

13.What is role of Action Class?
An Action Class performs a role of an adapter between the contents of an incoming HTTP request and the corresponding business logic that should be executed to process this request.

14.In which method of Action class the business logic is executed ?

In the execute() method of Action class the business logic is executed.

public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception ;

execute() method of Action class:
Perform the processing required to deal with this request
Update the server-side objects (Scope variables) that will be used to create the next page of the user interface
Return an appropriate ActionForward object

15.What design patterns are used in Struts?

Struts is based on model 2 MVC (Model-View-Controller) architecture. Struts controller uses the command design pattern and the action classes use the adapter design pattern. The process() method of the RequestProcessor uses the template method design pattern. Struts also implement the following J2EE design patterns.
Service to Worker
Dispatcher View
Composite View (Struts Tiles)
Front Controller
View Helper
Synchronizer Token

16.Can we have more than one struts-config.xml file for a single Struts application?
Yes, we can have more than one struts-config.xml for a single Struts application. They can be configured as follows:




17.What is the directory structure of Struts application?
The directory structure of Struts application :

18.What is the difference between session scope and request scope when saving formbean ?
when the scope is request,the values of formbean would be available for the current request.
when the scope is session,the values of formbean would be available throughout the session.
19.What are the important tags of struts-config.xml ?
The five important sections are:

20.What are the different kinds of actions in Struts?
The different kinds of actions in Struts are:
21.What is DispatchAction?
The DispatchAction class is used to group related actions into one class. Using this class, you can have a method for each logical action compared than a single execute method. The DispatchAction dispatches to one of the logical actions represented by the methods. It picks a method to invoke based on an incoming request parameter. The value of the incoming parameter is the name of the method that the DispatchAction will invoke.
22.How to use DispatchAction?
To use the DispatchAction, follow these steps :
Create a class that extends DispatchAction (instead of Action)
In a new class, add a method for every function you need to perform on the service – The method has the same signature as the execute() method of an Action class.
Do not override execute() method – Because DispatchAction class itself provides execute() method.
Add an entry to struts-config.xml

DispatchAction Example »
23.What is the use of ForwardAction?
The ForwardAction class is useful when you’re trying to integrate Struts into an existing application that uses Servlets to perform business logic functions. You can use this class to take advantage of the Struts controller and its functionality, without having to rewrite the existing Servlets. Use ForwardAction to forward a request to another resource in your application, such as a Servlet that already does business logic processing or even another JSP page. By using this predefined action, you don’t have to write your own Action class. You just have to set up the struts-config file properly to use ForwardAction.

24.What is IncludeAction?
The IncludeAction class is useful when you want to integrate Struts into an application that uses Servlets. Use the IncludeAction class to include another resource in the response to the request being processed.

25.What is the difference between ForwardAction and IncludeAction?
The difference is that you need to use the IncludeAction only if the action is going to be included by another action or jsp. Use ForwardAction to forward a request to another resource in your application, such as a Servlet that already does business logic processing or even another JSP page.

26.What is LookupDispatchAction?
The LookupDispatchAction is a subclass of DispatchAction. It does a reverse lookup on the resource bundle to get the key and then gets the method whose name is associated with the key into the Resource Bundle.

27.What is the use of LookupDispatchAction?

LookupDispatchAction is useful if the method name in the Action is not driven by its name in the front end, but by the Locale independent key into the resource bundle. Since the key is always the same, the LookupDispatchAction shields your application from the side effects of I18N.

28.What is difference between LookupDispatchAction and DispatchAction?
The difference between LookupDispatchAction and DispatchAction is that the actual method that gets called in LookupDispatchAction is based on a lookup of a key value instead of specifying the method name directly.

29.What is SwitchAction?
The SwitchAction class provides a means to switch from a resource in one module to another resource in a different module. SwitchAction is useful only if you have multiple modules in your Struts application. The SwitchAction class can be used as is, without extending.

30.What if element has declaration with same name as global forward?
In this case the global forward is not used. Instead the element’s takes precendence
31.What is DynaActionForm?

A specialized subclass of ActionForm that allows the creation of form beans with dynamic sets of properties (configured in configuration file), without requiring the developer to create a Java class for each type of form bean.

32.What are the steps need to use DynaActionForm?
Using a DynaActionForm instead of a custom subclass of ActionForm is relatively straightforward. You need to make changes in two places:
In struts-config.xml: change your form-bean to be an

form-bean name="loginForm"type="org.apache.struts.action.DynaActionForm"
form-property name="userName" type="java.lang.String"
form-property name="password" type="java.lang.String"

In your Action subclass that uses your form bean:
import org.apache.struts.action.DynaActionForm
downcast the ActionForm parameter in execute() to a DynaActionForm
access the form fields with get(field) rather than getField()

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import org.apache.struts.action.DynaActionForm;

public class DynaActionFormExample extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
DynaActionForm loginForm = (DynaActionForm) form;
ActionMessages errors = new ActionMessages();
if (((String) loginForm.get("userName")).equals("")) {
errors.add("userName", new ActionMessage(
if (((String) loginForm.get("password")).equals("")) {
errors.add("password", new ActionMessage(

33.How to display validation errors on jsp page?
tag displays all the errors. iterates over ActionErrors request attribute.

34.What are the various Struts tag libraries?
The various Struts tag libraries are:
Bean Tags
Logic Tags
Template Tags
Nested Tags
Tiles Tags

35.What is the use of logic:iterate?
repeats the nested body content of this tag over a specified collection.

table border=1
logic:iterate id="customer" name="customers"

bean:write name="customer" property="firstName"
bean:write name="customer" property="lastName"
bean:write name="customer" property="address"


36.What are differences between and
: is used to retrive keyed values from resource bundle. It also supports the ability to include parameters that can be substituted for defined placeholders in the retrieved string.

: is used to retrieve and print the value of the bean property. has no body.

37.How the exceptions are handled in struts?
Exceptions in Struts are handled in two ways:
Programmatic exception handling : Explicit try/catch blocks in any code that can throw exception. It works well when custom value (i.e., of variable) needed when error occurs.
Declarative exception handling :You can either define global-exceptions handling tags in your struts-config.xml or define the exception handling tags within tag. It works well when custom page needed when error occurs. This approach applies only to exceptions thrown by Actions.

exception key="some.key"

exception key="some.key"

38.What is difference between ActionForm and DynaActionForm?
An ActionForm represents an HTML form that the user interacts with over one or more pages. You will provide properties to hold the state of the form with getters and setters to access them. Whereas, using DynaActionForm there is no need of providing properties to hold the state. Instead these properties and their type are declared in the struts-config.xml
The DynaActionForm bloats up the Struts config file with the xml based definition. This gets annoying as the Struts Config file grow larger.
The DynaActionForm is not strongly typed as the ActionForm. This means there is no compile time checking for the form fields. Detecting them at runtime is painful and makes you go through redeployment.
ActionForm can be cleanly organized in packages as against the flat organization in the Struts Config file.
ActionForm were designed to act as a Firewall between HTTP and the Action classes, i.e. isolate and encapsulate the HTTP request parameters from direct use in Actions. With DynaActionForm, the property access is no different than using request.getParameter( .. ).
DynaActionForm construction at runtime requires a lot of Java Reflection (Introspection) machinery that can be avoided.

39.How can we make message resources definitions file available to the Struts framework environment?
We can make message resources definitions file (properties file) available to Struts framework environment by adding this file to struts-config.xml.

40.What is the life cycle of ActionForm?

The lifecycle of ActionForm invoked by the RequestProcessor is as follows:
Retrieve or Create Form Bean associated with Action
"Store" FormBean in appropriate scope (request or session)
Reset the properties of the FormBean
Populate the properties of the FormBean
Validate the properties of the FormBean
Pass FormBean to Action

Wednesday, October 8, 2008

Simple Hibernate Example with ANT script

The Following Example is shows how to save data into database for that you required the follwoing components
1.hibernate mapping file(hbm.xml)
2. simple PlainOldJavaObject (POJO) class with setters and Getters
3.required jar files for hibernate nad database driver
4.Ant script to run the program
5. simple Class with main() method

1.First i have Person table in database with the following fields
sql-query for creating table: create table person (personid int primary key,name varchar2(30),age int, weight int);
2. creating person.hbm.xml
<?xml version="1.0" encoding="UTF-8"?/>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<hibernate-mapping package="com.siva">
<class name="Person" table="person">
<id name="personId" type="java.lang.Long">
<generator class="increment"/>
<property name="name" type="string"/>
<property name="age" type="java.lang.Long"/>
<property name="weight" type="java.lang.Long"/>
In the above hbm file you have id generator class="increment" for id you no need to insert explicit value hibernate it self give id to you .
3. simple POJO class of Person

package com.siva;
public class Person
private Long personId;
private String name;
private Long age;
private Long weight;
public Long getPersonId() {
return personId;
public void setPersonId(Long personId) {
this.personId = personId;

public String getName() {
return name;
public void setName(String name) {
this.name = name;
public Long getAge() {
return age;
public void setAge(Long age) {
this.age = age;

public Long getWeight() {
return weight;
public void setWeight(Long weight) {
this.weight = weight;
Now for hibernate we have all are in place except configuration which database we need to point and what is driver for that so writing another xml file for this we mention all this in The following class itself
4. SimpleHibernateTest class as following
package com.siva;
import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.cfg.Environment;
public class SimpleHibernateTest
public static void main(String[] args)
Transaction tx = null;
Configuration cfg = new Configuration();
cfg.setProperty(Environment.AUTOCOMMIT ,"true" );
cfg.setProperty (Environment.DRIVER, "oracle.jdbc.driver.OracleDriver");
cfg.setProperty(Environment.DIALECT, "org.hibernate.dialect.Oracle9Dialect");
cfg.setProperty(Environment.USER, "database username");
cfg.setProperty(Environment.PASS,"database password");
cfg.setProperty(Environment.URL, "jdbc:oracle:thin:@localhost:1521:SID");
//here SID is database name it is xe for oracle 10G
System.out.println("connected to databse " + cfg.getProperty(Environment.URL));
Person p = new Person();
p.setAge(26L); p.setWeight(63L);
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();
tx = session.beginTransaction();
System.out.println("successfully save the record in database");
} //try end
catch(Exception ex)
}//catch end
}//main method end
}//class end
Now we run this program in two ways
1. we run this through command prompt like noramal program
javac com.siva.SimpleHibernateTest.java
but we need required jar files so all jar files we need to keep in classpath
or if we using eclipse no need to bother about class path you can all jar files to build path of
project properties.
2. writing ANT file through that we can run the program
if we want to run the program through ANT we need ANT bin directory to path.
the follwoing ANT XML file

<project name="test" default="compile">
<property name="sourcedir" value="${basedir}"/>
<property name="targetdir" value="${basedir}/bin"/>
<property file="build.properties"/>
<path id="libraries">
<fileset dir="${librarydir}">
<include name="*.jar">
<fileset dir="${OraLib}">
<include name="*.jar">
<target name="clean">
<delete dir="${targetdir}"/>
<mkdir dir="${targetdir}"/>
<target name="compile" depends="clean, copy-resources">
<javac srcdir="${sourcedir}"
<target name="copy-resources">
<copy todir="${targetdir}">
< fileset dir="${sourcedir}">
<exclude name="**/*.java"/>
<target name="run" depends="compile">
<java fork="true" classname="com.siva.SimpleHibernateTest" classpathref="libraries">
<classpath path="${targetdir}"/>

In the above build.xml file there is one propety file=build.properties
this is for to give the location of jar files
where your hibernate jar files and database jar fiels
like the following
librarydir = C:/Hibernate Training/hibernate-3.1.2/hibernate-3.1/lib
OraLib = C:/oracle/ora90/jdbc/lib
After completing the above steps
just locate where is your build.xml file
for eg: c:\test\build.xml
come to command prompt and type like the following
c:\test> ANT
automatically it will compile because in your nat file you written Default as compile
once successful compilation you need to rubn for that
in the command prompt type the following
c:\test>ANT run
it will executs and insert the data into database.
This is the simple Hibernate example to start to learn hibernate.

Wednesday, August 20, 2008

Java Access specifiers

in java there is one more interesting concept about access specifiers
the following four access speciers are there in java
default (no access specifier keep blank)
Now the explanation as below with example
the private access specifier for only with in the class accessible not other than class
public class Person
private String name;
class Employee extends Person
public String helloString()
return name; // here it will show compilation error Person.name is not visible because
private access specifier used in Person class for name

we used this specifier variables, methods any where in any class
in other packages also we can use this variables and methods
package com.test;
public class Person
public String name = "siva";
package com.test1;
import com.test.Person;
public class Person extends Person
public String helloString()
return name;
if we take this scenario it will work fine here one more concept you need to know
Person class is extending Person class only why it is not showing exception
Because one Person class in one package
another Person class in another package
so public access specifier you can use any package any class you need to extend that classes
default(if you are not specifing any access specier it is default)
this access is package level access
package com.test
public Class Person
String name = "siva";
public static void main(String args[])
Employee e = new Employee()
System.out.println("Name is "+e.getPersonDetails());
class Employee extends Person
public String getPersonDetails()
return name;
in this Example you need not identify one more concept.
in one package two classes are there but one class having public one is having nothing
in any package accepts only one public class

one of most important concept for interviews
suppose we have two packages
1 com.test;
2 com.test1;
package com.test; having one class with protected method
package com.test1;
two class one class not overriding protected method directly using that method.
now the second class in com.test1 package is extending the same package class
the protected method is available only for first class of in second package i.e com.test1
not for second class
if you are try to access that method it will through exception.
the good example is Object class Clone method it is protected

Wednesday, July 2, 2008

Java Interview Concepts

i am writing the java interview concepts i am not giving complete details i am giving the overall structure for glance when ever interview is there it is for quick reference i hope this might help
Object class Details
Object Class is Super class of all java classes
Object class having the following methods
public final Class getClass()
public int hashCode()
public boolean equals(Object obj)
protected Object clone() throws CloneNotSupportedException
public String toString()
public final void notify()
public final void notifyAll()
public final void wait(long timeout) throws InterruptedException
protected void finalize() throws Throwable
you will get all the implementation details and some where most of the people
ask about these details some questions are
Can we override finalize() method
Ans: yes we can override this method when ever we know object is going out of memory
that type object we call in finalize method. this work automatically done by java only
it has good alogitihm to remove the object from memory.
notify(), wait(),notifyAll() are related to threads but why there are overrided in
Object Class
Ans: this methods not only for Threads it is external resources also that's why they are
overrided inside Object class
Difference b/n equal (==) operator and equals() method why should we override
Ans: in equals() method also they are using == operator only but when ever we want to
compare content of the two objects and both objects content has same but when
we compare it it return false so we should have to override equals() in our class.
Now i am going to explain methods in Objects class with examples
1. toString()
Syntax: public String toString()
return " ";
> in String class toString() method is overrided. when ever you are working with String class you no need to override toString() method
example without overriding toString()
public class Person
private String mname;
public Person(String aname)
mname= aname;
public static void main(String args[])
Person p = new Person("siva");
System.out.println("Name is " + p);
or //toString() method is Object class method so it will come for
any class
System.out.println("Name is " + p.toString())

ANS: Name is ClassName i.e Person@some hexadecimal value
here hexa decimal value is where the object stored in memory area
so result is not correct we need correct result for that we have to override
toString() method
attach this code for Person class
public String toString()
return mname;
Now run the program you will get result as ----- Name is siva

Tuesday, June 24, 2008

Spring SimpleFormController Example

The above images showes how to write simple spring form submit.
please use required jar files and follow the structure what i given .
if images are not seen double click on that it will open as a big image.

Wednesday, June 11, 2008

Simple Hello world in core with java installation

Learn java in easy way
The following steps are how to install java , how to set path and "Hello world"
1. First download JDK latest version from http://java.sun.com/javase/downloads/index.jsp
2. save your hard disk and double click on that
3.while instalation it will ask location to install the JRE and JDK
otherwise by default it will install inside Programfiles of your system.
for JDK choose another location and for JRE keep by default location.
4. you completed instalation successfully.
5. Now you need to tell your operating System where you installed java as well as
to run java program also for this you have to set path in your system.
setting path in different ways i am giving simple way
right click on My Computer -properties-Advanced-Environment variables
if you click on Environment Variables one box will open and in that all the paths will be
located .
in that we find two windows
-- in System variable you find variable name as path edit this and come to end of that
line give semicolon(;)
After that copy your java (JDK) installation location eg:d:\java\jdk1.5 you need to copy upto bin(eg:d:\java\jdk1.5\bin) copy location and paste after semicolon ( ; ) and give semicolon after java path also.
now your set the path successfully.
if you want to see path is set fine or not just open command prompt and type java
if it gives some help text then your path is ok otherwise again set the path by follwing above steps carefully.
once set the path successfully. Then cretae one folder for your all java programs in your system
How To Write a Hello world Program
1. open notepad or edit plus
type the following code
class Hello
public static void main(String args[])
System.out.println("Hello world");

2.save this program in d:\javaexamples folder.
3.open the command prompt and open d:\javaexamples
4. now type like this javac Hello.java
5. if you have no errors in your program and while setting the path agin it will show
6. after that you type java Hello
7. after run the program you will see "Hello world" in commond prompt


Contact Form


Email *

Message *