What is difference between Comparable and Comparator interface 1) Comparable interface is in java.lang package Comparator interface is in java.util package ================================ 2) int compareTo(Object o1) method takes input as only object int compare(Object o1, Objecto2) method takes input as 2 object ================================ 3) java.lang.Comparable: int compareTo(Object inuptObject1) This method compares this object with inputObject1. Returned int value has the following meanings. 1. positive (this) object is greater than inuptObject1 2. zero (this) object equals to inuptObject1 3. negative (this) object is less than inuptObject1 java.util.Comparator: int compare(Object object1, Object object2) This method compares object1 and object2 objects. Returned int value has the following meanings. 1. positive object1 is greater than object2 2. zero ...
My notes for an interview which I gues will help Fact about SingleTon 1) if your singleTOn class implements the java.io.Serializable interface , the class's instances can be serialized and deserialized. However, if you serialize a singleton object and subsequently deserialize that object more than once, you will have multiple singleton instances. 2)SingleTon Class is not thread safe if(instance == null) { instance = new Singleton(); } 3) if you clone singleTon class , it will create a clone and there will be two different object will be created. You can print hashCode of each instance, it will be different 4) if you create synchronized method, it will always hit the performance whenever you you create object. public synchronized static Singleton getInstance() { if(singleton == null) { synchronized(Singleton.class) { singleton = new Singleton(); } System.out.println("Object created"); } return singleton; } 5) if you creat synchroni...
Memory management is the process of recognizing when allocated objects are no longer needed, deallocating (freeing) the memory used by such objects, and making it available for subsequent allocations. Garbage collection solves many, but not all, memory allocation problems. You could, for example, create objects indefinitely and continue referencing them until there is no more memory available. Garbage collection is also a complex task taking time and resources of its own. =========================== When does GarbageCollector will run What will happen if garbage collector is not able to free memory =========================== Consider the possibility that the garbage collector may never even run during an application's lifetime. There is no guarantee as to when or if the JVM will invoke the garbage collector -- even if a program explicitly calls System.gc() . Typically, the garbage collector won't be automatically run until a program needs more memory than is currently ava...
Comments
Post a Comment