Thursday, July 14, 2011

puzzles

3 litres and 5 litres containers puzzle

Puzzle: You have two container - one can contain exactly 3 litres of water and the other can contain exactly 5 litres of water. How many minimum steps will you take to get exactly 4 litres of water without using any other container? You can assume the availability of sufficient quantity of water.


Solution: Find below the steps:-

  • Step #1: fill the 5 litres container (3L - 0, 5L - 5)
  • Step #2: fill the 3 litres container with the filled 5 litres container (3L - 3, 5L - 2)
  • Step #3: empty the just filled 3 litres container (3L -0, 5L - 2)
  • Step #4: transfer the 2 litres of water left in 5 litres container into 3 litres container (3L - 2, 5L - 0)
  • Step #5: fill the 5 litres container (3L - 2, 5L - 5)
  • Step #6: fill the 3 litres container (having 2L currently) using just filled 5 litres container (3L - 3, 5L - 4)
So in a minimum of 6 steps you get exactly 4L of water in the 5 litres container.

Choosing the Most Specific Method - Tricky Overloading



Choosing the Most Specific Method - Tricky Overloading

REF:http://geekexplains.blogspot.com/2009/06/choosing-most-specific-method-tricky.html

Choosing the Most Specific Method - Tricky Method Overloading

Let's start with looking at a code-segment and try to think of the output/error, it would produce when compiled/executed and subsequently we'll discuss the behavior of code.


public class NullTest {

   public static void method(Object obj){
     System.out.println("method with param type - Object");
   }
 
   public static void method(String obj){
     System.out.println("method with param type - String");
   }
 
   public static void main(String [] args){
     method(null);
   }
}

So, what do you expect as the output here? Before thinking about the output, do you really expect the code to compile successfully? Well... yeah, the code will compile and run fine as opposed to anyone who might have sensed an ambiguity here - we'll see the reason soon.

Since the methods are overloaded, the resolution will be done at compile-time only. Which method do you see being bind here - the one with parameter type 'Object' or the one with parameter type 'String' and why? Of course, the compiler can't bind two methods with one call, so on what basis would it pick the most suitable? Which method would be picked, is evident from the output given below:-


method with param type - String

Any guesses for why a special treatment is being given to 'String' here? Well... it's not actually for 'String' class specifically, but any sub-class would get a preference over the super class in such a situation. But, why? Because JLS (Section: 15.12.2.5) allows this. It clearly says:

"If more than one member method is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen."

As you easily deduce that the compiler should be able to pick 'the most specific', failing which it will throw a compile-time error. Let's understand it with the below code-segment which doesn't compile because the compiler can't pick 'the most specific' here.


public class NullTest {

   public static void method(Object obj){
     System.out.println("method with param type - Object");
   }
 
   public static void method(String str){
     System.out.println("method with param type - String");
   }
 
   public static void method(StringBuffer strBuf){
     System.out.println("method with param type - StringBuffer");
   }
 
   public static void main(String [] args){
     method(null); //... compile-time error!
   }
}

Why is the compiler not able to pick 'the most specific' here - because both String and StringBuffer are are sub-classes of the Object class, but without being in the same inheritance hierarchy. For finding 'the most specific' method, the compiler needs to find a method having the parameter type, which is a sub-class of the parameter types of all other overloaded methods.

This holds true for overloaded methods having more than one parameters as well. The compiler would pick 'the most specific' by looking which method is having at least one of its parameter types as a clear sub-class of the corresponding parameter type and other parameter types being either the same or clear sub-classes, in all other overloaded methods. If it can find one, good, otherwise it will throw a compile-time error. For example:


public class NullTest {

 public static void method(Object obj, Object obj1){
   System.out.println("method with param types - Object, Object");
 }

 public static void method(String str, Object obj){
   System.out.println("method with param types - String, Object");
 }

 public static void main(String [] args){
   method(null, null);
 }
}

Output

method with param types - String, Object

In this case the compiler can easily pick 'the most specific' as the method having parameter types (String, Object) as the other overloaded method is having its parameter types as (Object, Object) - clearly 'String' is a subclass of 'Object' and the other parameter is of same type, so the method with parameter types (String, Object) can be picked with ease. But, the below code would throw a compile-time error as none of the methods satisfy the condition for being picked as 'the most specific' method.


public class NullTest {

 public static void method(Object obj, String obj1){
   System.out.println("method with param types - Object, String");
 }

 public static void method(String str, Object str1){
   System.out.println("method with param types - String, Object");
 }

 public static void main(String [] args){
   method(null, null); //... compile-time error!
 }
}

What is the difference between abstraction and encapsulation?


  • Abstraction focuses on the outside view of an object (i.e. the interface) Encapsulation (information hiding) prevents clients from seeing it’s inside view, where the behavior of the abstraction is implemented.
  • Abstraction solves the problem in the design side while Encapsulation is the Implementation.
  • Encapsulation is the deliverables of Abstraction. Encapsulation barely talks about grouping up your abstraction to suit the developer needs.

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