Java technical interview questions answers(Difference between)

1. Difference between Comparable and Comparator?

2. What are the differences between Interface and Abstract class?

3. What are the differences between a HashMap and a HashTable?

4. what are the differences between String, StringBuilder and StringBuffer? 


Difference between Comparable and Comparator


Comparable Comparator
Comparable present in the java.lang package  Comparator present in Java.util package
The Comparable interface provides only one method  compareTo() Comparator interface provides two methods compare() and equals(). 
Comparable interface used for the default sorting order Comparator interface used for the customized sorting order. 
All wrapper classes and String classes implemented Comparable interface The only implemented classes of Comparator are Collator and RuleBasedCollector

What are the differences between Interface and Abstract class?

Interface Abstract class
If we don't know about the implementation in advance and just have requirement specifications then we should use the interface   We should use the Abstract class if we have partial knowledge about the implementation.
Inside the interface, every method is always public and abstract whether we declare it or not. The method inside the Abstract class need not be public and Abstract. we can take concrete methods also in Abstract class. 
We can't declare methods with private and protected modifiers(till java 1.7v) from java 1.8 we can declare methods with public, private, static and default modifiers as well. There are no restrictions on abstract class method modifiers. 
Every variable inside the interface is always public, static and final whether we declare it or not. Every variable present inside the Abstract class need not be public. 
Inside the interface, we can't declare constructors. Inside the Abstract class, we can declare a constructor. 
Interface is slower than Abstract class. Abstract is faster than Interface. 
Interface keyword used to declare an interface. Also, the interface can be implemented by using the keyword “implements”. Abstract keyword used to declare the abstract class. Also, the abstract class can be extended using the keyword “extends”. 


What are the differences between a HashMap and a HashTable?

HashMap HashTable
It is not synchronized.   It is synchronized.
It is not thread-safe because multiple threads can operate HashMap objects simultaneously. It is thread-safe because only one thread is allowed to operate the HashTable object at a time. 
A HashMap allows only one null key and any number of null values. A HashTable does not allow null keys and null values. 
It is fast compared to HashTable. It could be faster compared to HashMap. 
It is non-legacy, introduced in the 1.2 version. It is a legacy, introduced in the 1.0 version. 


What are the differences between String, StringBuilder and StringBuffer?




String StringBuilder StringBuffer
A string is Immutable.   StringBuilder is Mutable. StringBuffer is Mutable.
A string is thread-safe. StringBuilder is not thread-safe.  StringBuffer is thread-safe. 
Performance is high. StringBuilder performance is faster when compared to StringBuffer.   StringBuffer performance is slower when compared to StringBuilder. 
When you need an immutable sequence of characters (e.g., storing constants, literals, or text that won’t change) use String. When constructing a string piece by piece and thread safety is not a concern (e.g., building dynamic strings) use StringBuilder.  When you need thread safety (e.g., in multi-threaded applications) use StringBuffer. 
Post a Comment (0)
Previous Post Next Post