In this article, we will learn about the differences between the paint() method and the repaint() method in Java. In the AWT and Swing frameworks, rendering graphical components is done in two different roles by the two methods paint() and repaint(). The paint() Method This method holds instructions to paint this component. In Java Swing, we can change the paintComponent() method instead of paint() method as paint calls paintBorder(), paintComponent() and paintChildren() methods. We cannot call this method directly instead we can call repaint(). Syntax The following is the syntax: public void paint(Graphics g) { // paint() method ... Read More
In this article, we will learn to minimize/maximize a JFrame programmatically in Java. In Swing, programmers often need to resize the window as needed. For example, they can shrink the window when carrying out background tasks or expand the window size for a better full-screen experience. What is a JFrame? A JFrame class is a subclass of Frame class and the components added to a frame are referred to as its contents, these are managed by the contentPane. A JFrame contains a window with title, border, (optional) menu bar and user-specific components. By default, we can minimize a JFrame by ... Read More
String buffer and StringBuilder both are mutable classes which can be used to do operation on string objects such as reverse of string, concating string and etc. We can modify a string without creating a new object of the string. A string buffer is thread-safe whereas string builder is not thread-safe. Therefore, it is faster than a string buffer. Also, a string concat + operator internally uses StringBuffer or StringBuilder class. Below are the differences. What is StringBuilder? The StringBuilder class in java is used to create and change a sequence of characters. In a regural string once the characters ... Read More
JVM has divided memory space between two parts: one is Stack and another one is Heap space. Stack space is mainly used for storing order of method execution and local variables. Stacks always store blocks in LIFO order whereas heap memory uses dynamic allocation for allocating and deallocating memory blocks. Memory allocated to the heap lives until one of the following events occurs : Program terminated Memory free What is a Heap memory? Heap memory is allocated for storing objects, arrays, and JRE (Java Runtime Environment) classes. Memory may be ... Read More
There are two ways to get a lock on the shared resource by multiple threads. One is a Reentrant Lock (or read/write lock), and the other is by using the Synchronized method. The reentrant lock class has been provided in the Java concurrency package from Java 5. It is the implementation of the Lock interface, and according to Java docs, the implementation of the Lock interface provides more extensive operation than can be obtained using synchronized method. What is the Reentrant lock? ReetrantLock is a class that implements the Lock Interface. It provides the synchronization feature with great flexibility, ... Read More
There are two ways to create a new thread of execution. One is to declare a class to be a subclass of the Thread class. This subclass should override the run method of the Thread class. An instance of the subclass can then be allocated and started. The other way to create a thread is to declare a class that implements the Runnable interface. That class then implements the run method. An instance of the class can then be allocated, passed as an argument when creating Thread, and started. Every thread has a name for identification purposes. More than one ... Read More
Serialization and externalization both are the processes of converting an object to stream byte and storing byte stream in database or memory. The class that implements java.io.Serializable interface can be serialized. What is Serialization? Java provides a mechanism called object serialization where an object can be converted into a byte stream that includes the object's data and details about the object's type. Example The following is an example of Serialization in Java: import java.io.Serializable; class SerializableExample implements Serializable { private static final long serialVersionUID = 5081877L; String name; } public ... Read More
In this article, we will compare the Java Error class and the Java Exception class. Both exceptions and errors are subclasses of the Throwable class. The error indicates a problem that mainly occurs due to the lack of system resources and our application should not catch these types of problems. It's important to note that the term error in general programming refers to mistakes in code, logic, or unexpected behavior. The Java Error class is not the same as general errors. What is an Error? An Error is a subclass of the Throwable class that represents serious problems that ... Read More
A Map is an object that stores key-value pairs, where each key is unique but values can repeat. The HashMap is a type of Map that uses a hashtable in order to store these pairs. Now we will discuss the differences between ConcurrentHashMap and Synchronized Hashmap. What is a ConcurrentHashMap? ConcurrentHashMap is a class that was introduced in jdk1.5. ConcurrentHashMap applies locks only at bucket level called fragment while adding or updating the map. So, aConcurrentHashMap allows concurrent read and write operation to the map. Example of ConcurrentHashMap The following is an example of ConcurrentHashMap in java − import java.util.Map; ... Read More
Comparable and Comparator both are an interface that can be used to sort the elements of the collection. Comparator interface belongs to java.util package while comparable belongs to java.lang package. Comparator interface sort collection using two objects provided to it, whereas comparable interface compares" this" refers to the one objects provided to it. What is a Comparable interface? The Comparable interface is an interface which is used by Java Collections to sort and compare custom objects. It belongs to java.lang package and has a single method called compareTo(). Example of Comparable The following is an example of Comparable in ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP