Category Archives: Java

Method Overloading with Autoboxing and Widening in Java

Prerequisite: Method Overloading, Autoboxing and Unboxing In Java there are two type of variables: Primitive type and Reference type. Conversion of primitive type to its corresponding wrapper Object is called Autoboxing and Wrapper Object to its corresponding primitive type is known as Unboxing. Method Overloading with Autoboxing In method overloading, you may come across a… Read More »

Java.util.PriorityQueue class in Java

It is a priority queue based on priority heap. Elements in this class are in natural order or depends on the Constructor we used at this the time of construction. It doesn’t permit null pointers. It doesn’t allow inserting a non-comparable object, if it relies on natural ordering. Constructors: PriorityQueue(): Creates a PriorityQueue with the… Read More »

Automatic Resource Management in Java

Java provides a feature to make the code more robust and to cut down the lines of code. This feature is known as Automatic Resource Management(ARM) using try-with-resources from Java 7 onwards. The try-with-resources statement is a try statement that declares one or more resources. This statement ensures that each resource is closed at the… Read More »

Joining Threads in Java

java.lang.Thread class provides the join() method which allows one thread to wait until another thread completes its execution. If t is a Thread object whose thread is currently executing, then t.join(); it causes the current thread to pause its execution until thread it join completes its execution. If there are multiple threads calling the join()… Read More »

Why to Override equals(Object) and hashCode() method ?

Prerequisite – Equals and Hashcode method HashMap and HashSet use the hashcode value of an object to find out how the object would be stored in the collection, and subsequently hashcode is used to help locate the object in the collection. Hashing retrieval involves: First, find out the right bucket using hashCode(). Secondly, search the… Read More »

Singleton Class in Java

In object-oriented programming, a singleton class is a class that can have only one object (an instance of the class) at a time. After first time, if we try to instantiate the Singleton class, the new variable also points to the first instance created. So whatever modifications we do to any variable inside the class… Read More »

Equals() and hashCode() methods in Java

Java.lang.object has two very important methods defined: public boolean equals(Object obj) and public int hashCode(). equals() method In java equals() method is used to compare equality of two Objects. The equality can be compared in two ways: Shallow comparison: The default implementation of equals method is defined in Java.lang.Object class which simply checks if two… Read More »

Serial Sort v/s Parallel Sort in Java

We often need to sort array while programming. For this, we use inbuilt method provided by Java in Arrays class i.e sort(). sort() method uses merge sort or Tim Sort to sort the array elements. In both the cases sort() method sequentially sort the elements of an array. In Java 8, there is a new… Read More »

Java.lang.Class class in Java | Set 2

Java.lang.Class class in Java | Set 1 More methods: int getModifiers() : This method returns the Java language modifiers for this class or interface, encoded in an integer. The modifiers consist of the Java Virtual Machine’s constants for public, protected, private, final, static, abstract and interface. These modifiers are already decoded in Modifier class in… Read More »