Wednesday, September 20, 2006

Tell me about JSP best practices?

Separate HTML code from the Java code: Combining HTML and Java code in the same source code can make the code less readable. Mixing HTML and scriplet will make the code extremely difficult to read and maintain. The display or behaviour logic can be implemented as a custom tags by the Java developers and Web designers can use these Tags as the ordinary XHTML tags.

Place data access logic in JavaBeans: The code within the JavaBean is readily accessible to other JSPs and Servlets.

Factor shared behaviour out of Custom Tags into common JavaBeans classes: The custom tags are not used outside JSPs. To avoid duplication of behaviour or business logic, move the logic into JavaBeans and get the custom tags to utilize the beans.

Choose the right “include” mechanism: What are the differences between static and a dynamic include? Using includes will improve code reuse and maintenance through modular design.

Use style sheets (e.g. css), template mechanism (e.g. struts tiles etc) and appropriate comments (both hidden and output comments).

Thursday, September 14, 2006

What is the difference between final, finally and finalize() in Java?

final - keyword is constant declaration.

finally - handles exception. The finally block is optional and provides a mechanism to clean up regardless of what happens within the try block (except System.exit(0) call). Use the finally block to close files or to release other system resources like database connections, statements etc.

finalize() - method helps in garbage collection. A method that is invoked before an object is discarded by the garbage collector, allowing it to clean up its state. Should not be used to release non-memory resources like file handles, sockets, database connections etc because Java has only a finite number of these resources and you do not know when the garbage collection is going to kick in to release these non-memory resources through the finalize() method.

Monday, September 11, 2006

What is the difference between an instance variable and a static variable? Give an example where you might use a static variable?

Class variables are called static variables. There is only one
occurrence of a class variable per JVM per class loader.
When a class is loaded the class variables (aka static
variables) are initialised.

Instance variables are non-static and there is one
occurrence of an instance variable in each class instance
(i.e. each object).

A static variable is used in the singleton pattern.A static variable is used with a final
modifier to define constants.

Friday, September 08, 2006

When is a method said to be overloaded and when is a method said to be overridden?

Method Overloading :
1. Overloading deals with multiple methods in the same class with the same name but different method signatures.
2. Overloading lets us define the same operation in different ways for different data.

Method Overriding :
1. Overriding deals with two methods, one in the parent class and the other one in the child class and has the same name and signatures.

2. Overriding lets you define the same operation in different ways for different object types.

Saturday, September 02, 2006

What is the main difference between a String and a StringBuffer class?

String is immutable: you can’t modify a string
object but can replace it by creating a new
instance. Creating a new instance is rather
expensive.

StringBuffer is mutable: use StringBuffer or StringBuilder when you want to
modify the contents. StringBuilder was added in Java 5 and it is identical in
all respects to StringBuffer except that it is not synchronised, which makes