Saturday, March 24, 2012

Core - Java Notes

1. Normal printing of the hashcode will print the Class Name and @hexadecimal value which is default. Inorder to get a custom String on printing the object then override the toString() Method.

2, String and Wrapper class overrides the equals method so that comparison is the same.

3. if we dont override the equals method then the objects wont work properly in sets and cant be used as keys in hashtable.

4.it is possible to use an object as a key in a hashtable but the objects equals property should be overridden else we will never be able to create that key as every object creation will create a seperate hashcode.

5.HashCodes are typically used to increase the perfomance of large large collection of data.

6. in real life hashing also it is not required that one bucket contains only 1 value one bucket can have 2 values with identical hashcode so retrieving froma hashing set is a 2 step process.

1. Find the right bucket and use equals operator.

7. If 2 objects are equal there hashcode must also be equal.(this is like a standard).u can use any type of values to be used as hashcode.The hashcode function can go on generating identical values for the same.This is also right only thing is retrivall process is painful as will have to rely on equals to find our object.

8. Tranisient variables are variables that cant be persisted or serialized.During the serialization the variable will be skipped.So we should avoid using transient variable in hashcode and equals cases because once the state is persisted and retrived the values will come as 0 for transient variables and the hascode or equals condition will fail because of which retrival from hashmap fails.

9.Collections is a class in Java.util which contains utility methods while collections is an abstract interface that is extended that contains common methods for most collections.

10.A list can have duplicate values but a set cant have.

11. along with this there are sorted,unsorted ordered and unordered,However a sorted cacn never be unordered,

12. ArrayList is an ordered list but not a sorted list.It can be used when we need fast iteration over the data and less insertion and deletion.Only vector and Arraylist implement random Access interface.

Linked List is the one in which each element is doubly linked and so it is easier to add and remove in the begining and end which makes it best choice for implementing queue and stack.The linked list maintains the order in which elements are inserted.

13. Vector is same as arraylist however vector is thread safe and so has a perfomance hit.

14. Treeset and TreeMap are the 2 sorted ordered collections.The elements will be in the ascending order.

15. HashMap allows null key and multiple null values.

16. Hastable is synchronized version of hashmap which means the methods of hastable are synchronized.

17. the collections doesnt allow primitive types it allows only objects but with java 1.5 the autoboxing happens.

18. There are 2 ways to sort a collection Comporator and Comparable.

 The objects should implement comparable and CompareTo method can be used to compare objects.

The Comparator is implemented by the third party class and takes 2 objects in Compare() method.

19. The Enum class overrides the equals and hashcode method.so it can be safely used as a key in the map.

20.While adding into a sortable collection and retrieving there are 2 things to note

1. All the items added to the Sortable collection should be of comparable or of same type else it throws a type cast exception

2. While retriving it checks 2 things one is the

   hashcode() -- should be same
equals() -- the  equals method should also be same.


22. Java doesnt support multiple inheritence because

      a. Diamond Problem - Say 1 class is inheriting from 2 parent class both having the function f00 then a call to f00 function on child object will be difficult to implement. it causes an abugity.
           A foo()
           / \
          /   \
   foo() B     C foo()
          \   /
           \ /
            D
           foo()

     b. So as to make the design more simple java designers avoid using it so that constructor chaining and multiple inheritance and all can be handled easily.In Interfaces only method signatures so wont cause ambiguity.

23. Why are string immutable?

     a. Consider the case of a String Pool .
          
                 StringA= a;
                 StringB=a;
            now if some one calls StringB.toUpperCase() the value of stringA will also change.
   
     b.Since String is immutable it is thread safe. Else using same string in multiple threads need to        make sure synchronization is done in all cases.

  c. It avoids lot of security threats as we cant manipulate the value to somethting else. Especially DB connectivity and all.

  d. It helps to cache the hashcode of the string. The string value doesnt change so that the hascode can be cached and improves the performance.

This is the reason why it is final as well. To be Immutable it should be final.

24. Thoughts on How Substring Works in java.

        a. Substring is an overloaded method in java. it has 2 variations
                  - begin index - the start index and rest will be printed
                  - begin index - End Index - The start Index to end index will be printed.
                     if the value is made to zero or equal to length the empty string returns. if <0 or greater     than length then the string out of bound exception is thrown.

How Substring causes a memory leak is because when the substring is called it copies the value into an array. So if it is a big string then the whole string is copied into the array and as long as it is reffered in memory it holds it so leads to memory leak.

it can be avoided in 2 ways first one assigning the subString to the String as a constructor because that internally trims the values or use intern on string. The intern first checks in the pool if they are same using the equals method .if so it will fetch it else it will add it to pool and retrive it there by optimizing memory.
                    
25. Singleton Pattern

public class Singleton{
     
private volatile Singleton INSTANCE;
 
     
private Singleton(){}
 
     
public Singleton getInstance(){
         
if(INSTANCE == null){
           
synchronized(Singleton.class){
               
//double checking Singleton instance
               
if(INSTANCE == null){
                    INSTANCE
= new Singleton();
               
}
           
}
         
}
         
return INSTANCE;
     
}
}

           

No comments:

Post a Comment