Tuesday, July 12, 2011

What is the difference between JDK and JRE?


What is the difference between JDK and JRE?
The JRE is the Java RunTime Environment that is a plug-in needed for running java programs. The JRE is an implementation of the Java Virtual Machine which actually executes Java programs.
The JDK is the Java Development Kit for Java application developers. The JDK is bundle of software which contains one (or more) JRE's along with the various development tools like the Java source compilers, bundling and deployment tools, debuggers, development libraries, etc.

Spring Tutorial


Spring interview question: http://www.developersbook.com/

http://static.springsource.org/docs/Spring-MVC-step-by-step/overview.html#overview-whats-covered

http://java9s.com/

http://www.theserverside.com/news/1364527/Introduction-to-the-Spring-Framework




String pool in java

public class DemoStringCreation {

  public static void main (String args[]) {
    String str1 = "Hello"; 
    String str2 = "Hello";
    System.out.println("str1 and str2 are created by using string literal.");   
    System.out.println("    str1 == str2 is " + (str1 == str2)); 
    System.out.println("    str1.equals(str2) is " + str1.equals(str2)); 

   
    String str3 = new String("Hello"); 
    String str4 = new String("Hello");
    System.out.println("str3 and str4 are created by using new operator.");   
    System.out.println("    str3 == str4 is " + (str3 == str4)); 
    System.out.println("    str3.equals(str4) is " + str3.equals(str4)); 
   
    String str5 = "Hel"+ "lo"; 
    String str6 = "He" + "llo";
    System.out.println("str5 and str6 are created by using string
constant expression.");   
    System.out.println("    str5 == str6 is " + (str5 == str6)); 
    System.out.println("    str5.equals(str6) is " + str5.equals(str6));

    String s = "lo";
    String str7 = "Hel"+ s; 
    String str8 = "He" + "llo";
    System.out.println("str7 is computed at runtime.");          
    System.out.println("str8 is created by using string constant
expression.");   
    System.out.println("    str7 == str8 is " + (str7 == str8)); 
    System.out.println("    str7.equals(str8) is " + str7.equals(str8));
   
  }
}
The output result is:
str1 and str2 are created by using string literal.
    str1 == str2 is true
    str1.equals(str2) is true
str3 and str4 are created by using new operator.
    str3 == str4 is false
    str3.equals(str4) is true
str5 and str6 are created by using string constant expression.
    str5 == str6 is true
    str5.equals(str6) is true
str7 is computed at runtime.
str8 is created by using string constant expression.
    str7 == str8 is false
    str7.equals(str8) is true
The creation of two strings with the same sequence of letters without the use of the new keyword will create pointers to the same String in the Java String literal pool. The String literal pool is a way Java conserves resources.

REF:

Friday, July 1, 2011

Thursday, June 16, 2011

Calculate factorial and power(x,n) function‎ Using Recursion


power(x,n) function:
public int raisePower( int base, int power )
{
if (power == 1)
{
return base;
}
else
{
base *= raisePower( base,power - 1 );
}

return base;
}

Calculate factorial :

public int factorial(int n)
    {
        if (n == 1) {
            return n;
        }
        else {
            return n * factorial(n - 1);
        }
    }

Wednesday, June 15, 2011

What is deadlock in Java ? How to fix it ?

What is deadlock in Java ? How to fix it ?

This is one of the question which is flavor of the season for multithreading , asked more at a senior level and with lots of follow up questions , though question looks very basic but most of developer get stuck once you start going deep.

questions starts with "What is deadlock ?"
answer is simple , when two or more threads waiting for each other to release lock and get stuck for infinite time , situation is called deadlock . it will only happen in case of multithreading.

How do you detect deadlock ?
though this could have many answers , my version is first I would look the code if I see nested synchronized block or calling one synchronized method from other or trying to get lock on different object then there is good chance of deadlock if developer is not very careful.

other way is to find it when you actually get locked while running the application , try to take thread dump , in linux you can do this by command "kill -3" , this will print status of all the thread in application log file and you can see which thread is locked on which object.

other way is to use jconsole , jconsole will show you exactly which threads are get locked and on which object.

once you answer this , they may ask you to write code which will result in deadlock ?
here is one of my version

public void method1(){
synchronized(String.class){
System.out.println("Aquired lock on String.class object");

synchronized (Integer.class) {
System.out.println("Aquired lock on Integer.class object");
}
}
}

public void method2(){
synchronized(Integer.class){
System.out.println("Aquired lock on Integer.class object");

synchronized (String.class) {
System.out.println("Aquired lock on String.class object");
}
}
}


If method1() and method2() both will be called by two or many threads , there is a good chance of deadlock becuase if thead 1 aquires lock on Sting object while executing method1() and thread 2 aquires lock on Integer object while executing method2() both will be waiting for each other to release lock on Integer and String to proceed further which will never happen.

now interviewer comes to final part , one of the most important in my view , How to fix deadlock ?

if you have looked above code carefully you may have figured out that real reason for deadlock is not multiple threads but the way they access lock , if you provide an ordered access then problem will be resolved , here is
the fixed version.

public void method1(){
synchronized(Integer.class){
System.out.println("Aquired lock on Integer.class object");

synchronized (String.class) {
System.out.println("Aquired lock on String.class object");
}
}
}

public void method2(){
synchronized(Integer.class){
System.out.println("Aquired lock on Integer.class object");

synchronized (String.class) {
System.out.println("Aquired lock on String.class object");
}
}
}

Now there would not be any deadlock because both method is accessing lock on Integer and String object in same order . so if thead A aquires lock on Integer object , thread B will not proceed until thread A releases Integer lock , same way thread A will not be blocked even if thread B holds String lock because now thread B will not expect thread A to release Integer lock to proceed further.

hope this would be useful.

Thursday, May 12, 2011

What are the differences between SAX and DOM parser.


SAX DOM
Both SAX and DOM are used to parse the XML document. Both has advantages and disadvantages and can be used in our programming depending on the situation.
Parses node by node Stores the entire XML document into memory before processing
Doesn’t store the XML in memory Occupies more memory
We cant insert or delete a node We can insert or delete nodes
Top to bottom traversing Traverse in any direction.
SAX is an event based parser DOM is a tree model parser
SAX is a Simple API for XML Document Object Model (DOM) API
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
 
doesn’t preserve comments preserves comments
SAX generally runs a little faster than DOM SAX generally runs a little faster than DOM
If we need to find a node and doesn’t need to insert or delete we can go with SAX itself otherwise DOM provided we have more memory.