Core Java
Core Java
Only instances of classes that implement the Cloneable interface can be cloned. Trying to clone an object that does not implement the Cloneable interface throws a CloneNotSupportedException. Both shallow copy and deep copy object nedd to implement Cloneable Interface. Shallow copy : The java.lang.Object root superclass defines a clone() method. default behavior of clone() is to return a shallow copy of the object. This means that the values of all of the original object?s fields are copied to the fields of the new object. If a shallow copy is performed on obj1, then it is copied but its contained objects are not. For Example: Public class Emp { Private Address address; } Emp emp1 = new Emp(); Address add = new Address(); emp1. address= add. If we clone Emp emp2 = emp1.clone(); Then emp2.addess reference to the same Address object which emp1 refer.
Deep Copy :
A deep copy occurs when an object is copied along with the objects to which it refers. A deep copy makes a distinct copy of each of the object?s fields, recursing through the entire graph of other objects referenced by the object being copied. The Java API provides no deep-copy equivalent to Object.clone(). One solution is to simply implement your own custom method (e.g., deepCopy()) that returns a deep copy of an instance of one of your classes. Your Object Class should implements Serializable and Cloneable interface. . public static Object copy(Object orig) { Object obj = null; try { // Write the object out to a byte array ByteArrayOutputStream bo = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bo); out.writeObject(orig); out.flush(); out.close(); // Make an input stream from the byte array and read // a copy of the object back in. ObjectInputStream in = new ObjectInputStream( new ByteArrayInputStream(bos.toByteArray())); obj = in.readObject();
Why to override equals() and hashCode()? and How i can implement both equals() and hashCode() for Set ?
f you are implementing HashSet to store unique object then you need to implement equals() and hashcode() method. if two objects are equal according to the equals() method, they must have the same hashCode() value (although the reverse is not generally true). Two scenarios Case 1) : If you don't implement equals() and hashcode() method : When you are adding objects to HashSet , HashSet checks for uniqueness using equals() and hashcode() method the class ( ex. Emp class). If there is no equals() and hashcode() method the Emp class then HashSet checks default Object classes equals() and hashcode() method. In the Object class , equals method is public boolean equals(Object obj) { return (this == obj); } Under this default implementation, two references are equal only if they refer to the exact same object. Similarly, the default implementation of hashCode() provided by Object is derived by mapping the memory address of the object to an integer value. This will fail to check if two Emp object with same employee name . For Example : Emp emp1 = new Emp(); emp1.setName("sat"); Emp emp2 = new Emp(); emp2.setName("sat"); Both the objects are same but based on the default above method both objects are dirrefent because references and hashcode are different . So in the HashSet you can find the duplicate emp objects.
To overcome the issue equals() and hashcode() method need to override. Case 2) : If you override equals() and hashcode() method Example : implement equals and hashcode public class Emp { private long empId; String name = ""; public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof Emp)) return false; Emp emp = (Emp)o; return emp. empId == empId && emp. name == name; } public int hashCode(){ int result = 10; result = result * new Integer(String.valueOf(empId)).intValue(); return result; } } In the equals() , it check for name is same or not. This way you can find out the objects are equals or not. In the hashCode() also it return some unique value for each object. In this way if two Emp object has same empId then it will say both are same object. Now HashSet store only unique objects. If you do Emp emp1 = new Emp(); emp1.setName("sat"); Emp emp2 = new Emp(); emp2.setName("sat"); if(emp1.equals(emp2)){ System.out.println("equal"); } This will print : equal
instance = new Singleton(); //3 } } return instance; } Thread 1 enters the getInstance() method.
Thread 2 attempts to acquire the lock at //1 because instance is still null. However, because thread 1 holds the lock, thread 2 blocks at //1.
Thread 1 executes and because instance is still null at //2, creates a Singleton object and assigns its reference to instance.
Thread 1 exits the synchronized block and returns instance from the getInstance() method.
Thread 2 acquires the lock at //1 and checks to see if instance is null.
Because instance is non-null, a second Singleton object is not created and the one created by thread 1 is returned.
What is daemon thread and which method is used to create the daemon thread?
Daemon thread is a low priority thread which runs intermittently in the back ground doing the garbage collection operation for the java runtime system. setDaemon method is used to create a daemon thread.
If I write System.exit (0); at the end of the try block, will the finally block still execute?
No ,in this case the finally block will not execute because when you say System.exit (0); the control immediately goes out of the program, and thus finally never executes.
Java only supports pass by value. With objects, the object reference itself is passed by value and so both the original reference and parameter copy both refer to the same object.
What is final?
A final class can't be extended ie., final class may not be subclassed. A final method can't be overridden when its class is inherited. You can't change value of a final variable (is a constant).
When the sub-types behaviour is totally different then you use an interface, when the sub-types behaviour is partially common and different with respect to the supertype an abstract class is used. In an abstract class the partially common behaviour is given a concrete implementation. Since there is no common behaviour between an interface and a sub-type an interface does not have an implementation for any of its behaviour. For Example - Abstract Class abstract public class Animal { public eatChicken(){ System.out.println("Eat Chicken"); } } public Lion extends Animal{ } public Tiger extends Animal{ } Both Lion and Tiger class has common behaviour eatChicken() and all the implementaion put into eatChicken() method of super calss. so we go for abstract class. Example- Interface Interface Animal{ public eat(); } public Lion implements Animal{ public eat(){ System.out.println("Lion eat Non Veg"); } } public Cow implements Animal{ public eat(){ System.out.println("Cow eat Veg"); } } Both Lion and Cow class has different behaviour for eat() and they have different implementations. so we go for Interface.
but "implements" multiple interfaces. * If given a choice, use interface instead of abstract class.