Java Interview Points

Java Interview Points
  1. Uses of finalizer :- 
    • It acts as a safety net. It may be better to free the resources than never e.g. FileInputStream, Connection etc. 
    • Object with native peers, the garbage collector doesn't know about it when it reclaimed the java peer. If native peer hold some critical resources, we can use this method to free the resources. 
  2. Clone will not work if the array fields are final because clone would be prohibited from assigning a new value to the field. 
  3. A method in an interface can’t be native, synchronized, strictfp but in the implementation class it can be since these keywords describe implementation properties rather than interface properties. Same is true for final. 
  4. Only data associated with specific instances of a class is serialized, because static data is associated with a class as opposite to an instance, so it’s not serialized automatically. To serialize static data, one must provide class specific serialization. 
  5. Array implementation in Java: - Java treats an array as an object. When we declare an array as 
          long[] temp = new long[10] ;
          the jvm creates a long[] class with temp as its object. When we print the name as              
            SOP(temp.getClass().getName()), it prints 
              [J 

           Definition of this class is: 

           public abstract final class [J extends java.lang.Object implements Cloneable,Serilizable {
           }

     6 . Abstract class constructors are frequently used to enforce class constraints as invariants such as:
           minimum fileds requiredto set up the class.

      7. The foreach loop can be applied to any Object that implements Iterable<E> which in turn refers to  
           Iterator<E>. Methods Iterstor, hasNext, next which are used by during the transalation of the loop.

      8. The < ? extends Animal> means that you can take any subtype of animal, the subtype can be either a                
          sublass of a class (Abstract or concrete) or a type that implemetsthe interface after the word extends.    
                        Also, it can be assigned a collection that is subtype of Animal or anything that extends    
          Animal and I promise that I will not add into the collection.

     9. List<? > means that it can support any type but does not support Add.

    10. You cannot put anything into a type declared with extends wildcard except for values as NULL, 
           which belongs to every ref type.
   
    11. Extends wildcard does not ensure immutability since we can still add null to it. Also you can remove,  
          remove all, retain swap, shuffle etc.

    12. Get and Put Principle tells us that if a structure contains a wildcard, we should get the values of it(if it  
          extends wildcard) or put values into it (if it is super wildcard). For a strucutr to be usefull, we must do  
          both.



e.g List <Number> nums = new ArrayList<Number>();

List<? Super Number> sink = number // put

List<? Extends Number> source = nums; //get





Comments

Popular posts from this blog

When to use getClass() and instanceOf in Java

Why Time gets truncated with ResutSet getDate() function.

How class.forName loads the Database Driver in JDBC