Category Archives: Java

Java.util.Locale Class in Java | Set 2

Java.util.Locale Class in Java | Set 1 More methods: getDisplayVariant() : java.util.Locale.getDisplayVariant() displays variant of the Locale Syntax : public final String getDisplayVariant() Parameters : —- Return : ———– getDisplayVariant(Locale in) : java.util.Locale.Locale in(Locale in) returns the variant of “in” locale. Syntax : public final String getDisplayVariant(Locale in) Parameters : in : the instance local… Read More »

Java.util.Locale Class in Java | Set 1

As the name suggests util.Locale Class is used to perform locale task and provides locale information for the user. Declaration : public final class Locale extends Object implements Cloneable, Serializable Constructors : Locale(String L) : Creates Locale form the given language code. Locale(String L, String C) : Creates Locale form the given language, country code.… Read More »

Multiples of 3 and 5 without using % operator

Write a short program that prints each number from 1 to n on a new line. For each multiple of 3, print “Multiple of 3” instead of the number. For each multiple of 5, print “Multiple of 5” instead of the number. For numbers which are multiples of both 3 and 5, print “Multiple of… Read More »

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 »