Friday, March 4, 2016

Difference between Static binding and Dynamic binding in java ?

Difference between Static binding and Dynamic binding in java ?

Static binding : Occurs during compile time . Uses type(Class) information for binding.
      Actors :  Overloaded methods
Dynamic binding :   Occurs during run-time. Uses instance of class(Object) to resolve calling of method at run-time.
      Actors : Overridden methods

Call By Value and Call By Reference

How Java pass parameters to method ? Call by Reference or Call by Value ?


One of the tricky parts of java to understand, whether it support call by value or call by reference while using methods. Java supports both of them based on the type of the passed parameter. Let's dive into that.

Call By Value and Call By Reference :

There are two ways to pass an argument to a method
  1. call-by-value : In this approach copy of an argument value is pass to a method. Changes made to the argument value inside the method will have no effect on the arguments.
  2. call-by-reference : In this reference of an argument is pass to a method. Any changes made inside the method will affect the agrument value.
Here is the point to remember :  In Java, when you pass a primitive type to a method it is passed by value whereas when you pass an object of any type to a method it is passed as reference.
That's it Happy Learning!!!!!!

Friday, February 5, 2016

How hashCode and equals method works in HashMap or HashTable


How hashCode and equals method works in HashMap or HashTable?

Ans)  HashMap or HashTable or ConcurrentHashMap are used based on requirements when there are key value storage needed.  We should remember HashMap is backed by array in Java we will call it as bucket. 
whenever you use a get(key) method of hashcode the following are happened. 
1. Key.hashCode() method is used to find the bucket location in backing array. 
2.    In bucket, key and values are stored in form of a nested class called Entry.  If there is only one Entry at bucket location then, value from that entry is returned. 
Till now it's easy.. right? Now if the two keys has same hashcode then, we need a bit tricky to get the value for proposed key.  In this situation,  during put() operation collision had occurred, which means multiple Entry object stored in a bucket location. Each Entry keep track of another Entry, forming a  linked list data structure there.
Now the trace goes as follows,
1.   Call hashCode() method of key to find bucket location.
2.   Traverse through linked list, comparing keys in each entries using keys.equals() until it return true.
So, we use equals() method of key object to find correct entry and then return value from that. Remember key.equals() method. Many programmers mention value.equals(), which may be due to interview nervousness, but that’s incorrect. Since you don't have value object passed to get() method, there is no question of calling equals and hashCode() method on value object.  

That's it... Happy Learning!!!!