What is diffrencence between final,finally,finalize?
final - final is modifier applicable for classes,methods and variables. If a class declared as final then we can't extend the class. i.e., we can't create child class for that classes.
If method declared as final then we can't override that method in the child class.
If a variable declared as final then it will become constant and we can't perform re-assignment for that variable.
finally - finally is a block always associated with try-catch to maintain cleanup code.
try{ // Riskycode }
catch{ // Handling code }
finally{ // Cleanup code }
finalize( ) - finalize is a method which is always invoked by garbage collector just before destroying an object to perform cleanup activities.
Note:
finally meant for cleanup activities related to try block. Where as finalize() meant for cleanup activities related to object.
String and String Buffer
String (Immutable) - Once we creates a string object we can't perform any changes in the existing object. If we are trying to perform any changes with those changes a new object will be created. This non changeble nature is nothing but immutability of the string object.
StringBuffer(Mutable) - Once we creates a StringBuffer object we can perform any type of changes in the existing object. This changeble is nothing but mutability of the string Buffer object.
= = Operator and equals( ) method?
= = Operator meant for reference and address comparison, where as .equals() method for content comparison.
Note:
* equals() method present in Object class also meant for reference comparison only based on our requirement we can override for content comparision.
* In String class, all wrapper class and all collection classes .equals() method is overridden for content comparison.
Q) Diff between Throw and Throws ?
throw:
Java throw keyword is used to explicitly throw an exception.
Checked exception cannot be propagated using throw only.
Throw is followed by an instance.
Throw is used within the method.
You cannot throw multiple exceptions.
throws:
Java throws keyword is used to declare an exception.
Checked exception can be propagated with throws.
Throws is followed by class.
Throws is used with the method signature.
You can declare multiple exceptions e.g.
public void method()throws IOException,SQLException.
Diffrence between Functional Interface and Interface?
Diffrence between Shallow Copy and Deep Copy?
A deep copy occurs when an object is copied along with the objects to which it refers.
Shallow copy is a bit-wise copy of an object. A new object is created that has an exact copy of the values in the original object
Thread Race conditions:
Diffrence between Stream API and Collection API?
1. Version: Collection API is in use since Java 1.2.
Stream API is recent addition to Java in version 8.
2. Usage: Collection API is used for storing data in different kinds of data structures.
Stream API is used for computation of data on a large set of Objects.
3. Finite: With Collection API we can store a finite number of elements in a data structure.
With Stream API, we can handle streams of data that can contain infinite number of elements.
4. Eager vs. Lazy: Collection API constructs objects in an eager manner.
Stream API creates objects in a lazy manner.
5. Multiple consumption: Most of the Collection APIs support iteration and consumption of elements multiple times.
With Stream API we can consume or iterate elements only once.
public static void main(String[] args) ?
public - To call by JVM from anywhere
static - without existing object also JVM has to call this method and main method no way related to any object.
void - main() method won't return anything to JVM.
main - This is name which is configured inside JVM
String[] args - command line arguments
Types of Inheritance:
Single-level inheritance - When a class extends another class, such type of inheritance is known as single level inheritance.
Multi-level inheritance - When a class extends another class, which is itself extended by another class,
Multiple inheritance - Java does not allow a class to extend multiple classes at the same time, but it allows a class to implement multiple interfaces at the same time.
What Is the Default Size of the Load Factor in Hashing a Based Collection?
The default size is 0.75, and the default capacity is computed as:
Initial capacity * Load factor
What Is a Package?
- A package is a collection of related classes and interfaces.
- A package is a group of similar type of classes, interfaces, and sub-packages.
- It provides access protection and removes naming collision. The packages in Java can be categorized into two forms, inbuilt package, and user-defined package.
- There are many built-in packages such as Java, lang, awt, javax, swing, net, io, util, sql, etc.
What's the Base Class of all Exception Classes?
Java.lang.Throwable is the superclass of all exception classes, and all exception classes are derived from this base class.
Differance between HashMap and Concurrent Hash Map?
HashMap | ConcurrentHashMap | |
---|---|---|
Synchronized | HashMap is not synchronized. | ConcurrentHashMap is synchronized. |
Thread Safe | HashMap is not thread safe. | ConcurrentHashMap is thread safe. |
Iterator type | HashMap iterator is fail-fast and ArrayList throws ConcurrentModificationException if concurrent modification happens during iteration. | ConcurrentHashMap is fail-safe and it will never throw ConcurrentModificationException during iteration. |
Null values | HashMap allows key and value to be null. | ConcurrentHashMap does not allow null key/value. It will throw NullPointerException. |
Performance | HashMap is faster. | ConcurrentHashMap is slower than HashMap. |
Since Java Version | 1.2 | 1.5 |