1. What is the difference between equals() and == ?
  2. What is difference between HashMap and HashTable ?
  3. What is difference between abstract class and interface ?
  4. What is the difference between final, finally and finalize() in Java?
Asked in/Preparing for : tcs


1 Respond



1. == operator is used to compare the references of the objects.
public boolean equals(Object o) is the method provided by the Object class. The default implementation uses == operator to compare two objects. But since the method can be overriden like for String class. equals() method can be used to compare the values of two objects.
String str1 = "MyName";
String str2 = "MyName";
String str3 = new String(str2);
if (str1 == str2) {
     System.out.println("Objects are equal")
}
else{
     System.out.println("Objects are not equal")
}
if(str1.equals(str2)) {
     System.out.println("Objects are equal")
}
else {
     System.out.println("Objects are not equal")
}

2. Both collections implements Map. Both collections store value as key-value pairs. The key differences between the two are
  • Hashmap is not synchronized in nature but hashtable is.
  • Another difference is that iterator in the HashMap is fail-safe while the enumerator for the Hashtable isn't.
  • Fail-safe -if the Hashtable is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will throw a ConcurrentModificationException?
  • HashMap permits null values and only one null key, while Hashtable doesn't allow key or value as null.


3.
  • A class is called abstract when it contains at least one abstract method. It can also contain n numbers of concrete method.Interface can contain only abstract( non implemented) methods.
  • The abstract class can have public,private,protect or default variables and also constants. In interface the variable is by default public final. In nutshell the interface doesnt have any variables it only has constants.
  • A class can extend only one abstract class but a class can implement multiple interfaces.
  • If an interface is implemented its compulsory to implement all of its methods but if an abstract class is extended its not compulsory to implement all methods.
  • The issue with an interface is, if you want to add a new feature (method) in its contract, then you MUST implement those method in all of the classes which implement that interface. However, in the case of an abstract class, the method can be simply implemented in the abstract class and the same can be called by its subclass.


4. final - A final variable acts as a constant, a final class is immutable and a final method cannot be ovrriden while doing inheritance.
finally - handles exception. The finally block is optional and provides a mechanism to clean up regardless of what happens within the try block (except System.exit(0) call). Use the finally block to close files or to release other system resources like database connections, statements etc.
finalize() - method belongs to Object class. The method that is invoked while doing the garbage collection of the object. It could be used for allowing it to clean up its state. Good use cases will be to free connection pools , deallocate resources etc.
 
 
 
sumit patwal
Reputation : 336



Written
Exam Questions
BLOG Posts
For study materials
Job Walk-In
Updates across India
Interview round
Questions
Interview or procedure
Experiences
Files
For study materials
Please LIKE our page to Get regular JOB WALK-IN UPDATES across India and STUDY MATERIALS on facebook news feed.