Found 7442 Articles for Java

Difference between inheritance and composition in Java

Kiran Kumar Panigrahi
Updated on 28-Jul-2022 11:37:32

4K+ Views

In computer programming, the concept of reusable code refers to the utilisation of previously developed software in the construction of new software. Reusability of code is recognised as an essential component of productive functionality. Establishing associations between classes is one method that object-oriented programming uses to encourage this.In object-oriented programming, there are two primary ways to construct these relationships: inheritance and composition.In object-oriented programming (OOP), inheritance refers to the process through which an object can take on the properties of one or more other objects. In OOP, it is one of the most powerful concepts for establishing code reusability. When ... Read More

Difference between Stack and Heap memory in Java

Teja Kolloju
Updated on 15-Apr-2025 19:12:52

7K+ Views

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

Difference Between ReentrantLock and Synchronized in Java

Teja Kolloju
Updated on 15-Apr-2025 19:12:33

3K+ Views

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

Difference between Thread and Runnable in Java

Teja Kolloju
Updated on 15-Apr-2025 19:12:14

36K+ Views

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

Difference between Exception and Error in Java

Teja Kolloju
Updated on 15-Apr-2025 19:11:02

24K+ Views

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

Difference between Serialization and Externalization in Java

Teja Kolloju
Updated on 15-Apr-2025 19:11:58

3K+ Views

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

Difference between Comparable and Comparator in Java

Teja Kolloju
Updated on 15-Apr-2025 19:09:18

5K+ Views

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

Difference between Concurrent hash map and Synchronized hashmap in Java

Teja Kolloju
Updated on 15-Apr-2025 19:10:46

8K+ Views

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

Difference between Tree Set and Hash Set in Java

Alshifa Hasnain
Updated on 14-Apr-2025 12:15:27

19K+ Views

In Java,  HashSet and TreeSet both belong to the collection framework. HashSet is the implementation of the Set interface, whereas TreeSet implements a sorted set. TreeSet is backed by TreeMap while HashSet is backed by a HashMap. Difference Table The following are the key differences between HashSet and TreeSet : Sr. No. Key ... Read More

Difference between Iterator and Enumeration in Java

Teja Kolloju
Updated on 17-Apr-2025 19:00:12

7K+ Views

Iterator and Enumeration are both cursors to traverse and access elements from the collection. They both belong to the collection framework. Enumeration was added in JDK1.0 and Iterator in JDK 1.2 version in the collection framework.  Java Enumeration Enumeration: An enumeration is a special "class" that indicates a collection of constants. Enumeration can’t make structural changes in the collection because it has read-only access to its elements. It has the following methods − hasMoreElements(): The hasMoreElements() method checks to see if more elements exist in the underlying collection class nextElement(): The ... Read More

Advertisements