Friday, July 30, 2010

About java finalize() method ?

Its a method defined in Object(Which is a base class of all the classes) class. Java dont have destructors instead it uses finalize() method. finalize() method not guarantee to run. For any given object finalize() method will be called only once by the garbage collector. Calling finalize() method can actually result in saving an object from deletion. If finalize() method contains the code of assigning a reference so that the object is not eligible for Garbage Collector. The second time the garbage collector will delete it. Because finalize() method run only one time for a given object.

Thursday, July 29, 2010

The list of interfaces we use in javax.servlet.http package ?

The javax.servlet.http package contains a number of classes and interfaces that describe and define the contracts between Servlet class running under a HTTP Potocol and the runtime environment provided for an instance of such a class by a conforming Servlet container.

The List of interfaces and classes in javax.servlet.http package are as follows.

HttpServletRequest
HttpServletResponse
HttpSession
HttpSessionListener
HttpServlet
Cookie

The list of interfaces we use in javax.servlet package ?

The javax.servlet package contains a number of classes and interfaces that describe and define the contracts between Servlet class and the runtime environment provided for an instance of such a class by a conforming Servlet container.

The List of interfaces and classes in javax.servlet package are as follows.

Servlet
ServletConfig
ServletRequest
ServletResponse
RequestDispatcher
Filter
FilterConfig
FilterChain
GenericFilter
ServletOutputStream
ServletInputStream
ServletException

Wednesday, July 28, 2010

What is a Java Thread and How to create them ?

Threads are lightweight processes that appear to run in parallel with your main program. Unlike a process a thread shares memory and data with the rest of the program. The word thread is a contraction of "thread of execution".An example of where threads can be useful is in printing. When you click on a print button you probably don't want the main program to stop responding until printing has finished. What would be nice is that the printing process started running "in the background" and allowed you to continue using the main portion of the program.

There are 2 ways to create a java thread.
1. By extending the class with the Thread class.
2. By implementing the Runnable interface.

In both the cases we have to implement run() method.

The best way to create the thread is to implement Runnable interface. Because, As you know java will not support multiple inheritance so, if we extend the class with the Thread class we cant extend with any other class. If we implement the class with Runnable interface, we can extend any other class

Tuesday, July 27, 2010

Calling Java Garbage Collector ?

We can request (not demand) the java garbage collector to run by using 2 ways.

1. Runtime rt = Runtime.getRuntime();
rt.gc();

2. System.gc();

Using Runtime object rt you can call more methods to know JVM memory. Examples,

rt.totalMemory(), Will return the total memory of JVM.
rt.freeMemory(), will return the free memory of JVM.

Which Objects eligible for Garbage Collection in Java ?

The Objects which are eligible for garbage collection are

1. StringBuffer sb=new stringBuffer("Hello");
System.out.println(sb); // "Hello" is NOT ELIGIBLE here for Garbage Collection.
sb=null; //"Hello" is ELIGIBLE here for Garbage Collection.


2. StringBuffer sb1=new stringBuffer("Hello");
StringBuffer sb2=new stringBuffer("goodbye");
s1=s2; //"Hello" is ELIGIBLE here for Garbage Collection.

3. Public static Date getDate(){
Date d2=new Date();
StringBuffer now=new StringBuffer(d2.toString());
System.out.println(now);
return d2;// Here d2 is NOT ELIGIBLE for Garbage Collection. But now is
ELIGIBLE for Garbage Collection
}

Wednesday, July 14, 2010

Advantages of struts2 over struts1.0

• Struts 1 supports separate Request Processors (lifecycles) for each module, but all the Actions in the module must share the same lifecycle, Struts 2 supports creating different lifecycles on a per Action basis via Interceptor Stacks. Custom stacks can be created and used with different Actions, as needed.Struts 1 integrates with JSTL, so it uses the JSTL EL, Struts 2 can use JSTL, but the framework also supports a more powerful and flexible expression language called "Object Graph Notation Language" (OGNL).(Object graph navigation language)( It is aimed to be used in J2EE applications with taglibs as expression language.)
• Struts 1 Actions have dependencies on the servlet API since the HttpServletRequest and HttpServletResponse is passed to the execute method when an Action is invoked. Struts 2 Actions are not coupled to a container. Most often the servlet contexts are represented as simple Maps.
• Struts 1 Actions are singletons and must be thread-safe since there will only be one instance of a class to handle all requests for that Action. Struts 2 Action objects are instantiated for each request, so there are no thread-safety issues.
• Struts 1 requires Action classes to extend an abstract base class. A common problem in Struts 1 is programming to abstract classes instead of interfaces. Struts 2 , we can use interfaces.
3. Interceptors
• These are request pre-processors or post processors
• Examples create-session, params, prepare, validation, exception and logger.
o Create-session, which creates HTTPSession.
o Prepare, to run prepare() method.
o Fileupload, to validate a file object
o Exception, handling exceptions.
o Roles, It checks the allowed roles and disallowed roles using intercept() method.
o Servlet-config sets the action properties, based on the different interface implemented.
o Validation, checks the action against all validation rules.

Monday, July 12, 2010

What is JDK, JRE, JVM ?

JDK (Java Development Kit)

Java Development Kit contains tools needed to develop the Java programs, and JRE to run the programs. The tools include compiler (javac.exe), Java application launcher (java.exe), Appletviewer, etc…

Compiler converts java code into byte code. Java application launcher opens a JRE, loads the class, and invokes its main method.

You need JDK, if at all you want to write your own programs, and to compile the m. For running java programs, JRE is sufficient.

JRE is targeted for execution of Java files

i.e. JRE = JVM + Java Packages Classes(like util, math, lang, awt,swing etc)+runtime libraries.

JDK is mainly targeted for java development. I.e. You can create a Java file (with the help of Java packages), compile a Java file and run a java file
JRE (Java Runtime Environment)

Java Runtime Environment contains JVM, class libraries, and other supporting files. It does not contain any development tools such as compiler, debugger, etc. Actually JVM runs the program, and it uses the class libraries, and other supporting files provided in JRE. If you want to run any java program, you need to have JRE installed in the system

The Java Virtual Machine provides a platform-independent way of executing code; programmers can concentrate on writing software, without having to be concerned with how or where it will run.

If u just want to run applets (ex: Online Yahoo games or puzzles), JRE needs to be installed on the machine.
JVM (Java Virtual Machine)

As we all aware when we compile a Java file, output is not an 'exe' but it's a '.class' file. '.class' file consists of Java byte codes which are understandable by JVM. Java Virtual Machine interprets the byte code into the machine code depending upon the underlying operating system and hardware combination. It is responsible for all the things like garbage collection, array bounds checking, etc… JVM is platform dependent.

The JVM is called "virtual" because it provides a machine interface that does not depend on the underlying operating system and machine hardware architecture. This independence from hardware and operating system is a cornerstone of the write-once run-anywhere value of Java programs.

There are different JVM implementations are there. These may differ in things like performance, reliability, speed, etc. These implementations will differ in those areas where Java specification doesn’t mention how to implement the features, like how the garbage collection process works is JVM dependent, Java spec doesn’t define any specific way to do this.