Found 2616 Articles for Java

Difference between Stack and Heap memory in Java

Mahesh Parahar
Updated on 18-Nov-2019 07:10:08

6K+ 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.Stack always stored blocks in LIFO order whereas heap memory used 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 In contrast, the memory allocated to stack lives until the function returns. Below are the differences.Sr. No.KeyStackHeap Memory1BasicStack memory is used to store items which have a very short life like local variables, a reference variable of ... Read More

Difference Between ReentrantLock and Synchronized in Java

Mahesh Parahar
Updated on 18-Nov-2019 07:06:01

2K+ Views

There are two ways to get a lock on the shared resource by multiple threads. One is Reentrant Lock (Or ReadWriteLock ) and the other is by using the Synchronized method.ReentrantLock class has been provided in Java concurrency package from Java 5. It is the implementation of Lock interface and According to java docs, implementation of Lock interface provides more extensive operation than can be obtained using synchronized method.Sr. No.KeyReentrantLockSynchronized1Acquire Lock Reentrant lock class provides lock() methods to get a lock  on the shared resource by thread You need to just write synchronized keyword to acquire a lock  2Release Lock To release lock , ... Read More

Difference between Thread and Runnable in Java

Mahesh Parahar
Updated on 18-Nov-2019 06:59:00

24K+ 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 thread may ... Read More

Difference between Exception and Error in Java

Mahesh Parahar
Updated on 14-Sep-2023 16:08:28

22K+ Views

Exceptions and errors both are subclasses of 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. Some of the examples of errors are system crash error and out of memory error. Errors mostly occur at runtime that's they belong to an unchecked type. Exceptions are the problems which can occur at runtime and compile time. It mainly occurs in the code written by the developers.  Exceptions are divided into two categories such as checked exceptions and unchecked exceptions. Sr. No.KeyErrorException1Type Classified as an unchecked type Classified ... Read More

Difference between Serialization and Externalization in Java

Mahesh Parahar
Updated on 18-Nov-2019 06:52:04

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 which implements java.io.Serializable interface can be serialized. On the other hand, externalization used for custom serialization based on the requirement in the application. Externalization extends java.io.Serializable. Sr. No.KeySerializationExternalization1InterfaceSerialization is a marker interface Externalization contains two methods readExternal and writeExternal. 2 Implementation logic The class which is implementing this interface gives the responsibility to JVM for serializing or persist java object.  JVM use readObject and writeObject for serialization Externalization provides implementation logic control to the application by overriding readExternal and writeExternal methods.3 Way to ... Read More

Difference between Comparable and Comparator in Java

Mahesh Parahar
Updated on 08-Jul-2020 07:25:33

3K+ 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. Sr. No.KeyComparableComparator1MethodsThe comparable interface has a method compareTo(Object a ) The comparator has a method compare(Object o1, Object O2) 2Sorting uses Collection.sort(List) method can be used to sort the collection of Comparable type objects.Collection.sort(List, Comparator) method can be used to sort the collection of Comparator type objects. 3Sorting sequence Comparable provides single sorting ... Read More

Difference between Concurrent hash map and Synchronized hashmap in Java

Mahesh Parahar
Updated on 18-Nov-2019 06:43:10

7K+ Views

Concurrent Hashmap is a class that was introduced in jdk1.5.  Concurrent hash map applies locks only at bucket level called fragment while adding or updating the map. So, a concurrent hash map allows concurrent read and write operation to the map. Synchronized hashmap(Collection.syncronizedHashMap()) is a method of Collection framework. This method applies a lock on the entire collection. So, if one thread is accessing the map then no other thread can access the same map. Sr. No.KeyConcurrent hash mapSynchronized hashmap1ImplementationIt is a class that implements a Concurrent hash map and serializable interface. It is a method in Collection class.  2Lock mechanismLocks the portionLocks ... Read More

Difference between Tree Set and Hash Set in Java

Mahesh Parahar
Updated on 24-Apr-2021 18:44:19

14K+ Views

Hash set and tree set both belong to the collection framework. HashSet is the implementation of the Set interface whereas Tree set implements sorted set. Tree set is backed by TreeMap while HashSet is backed by a hashmap.Sr. No.KeyHash SetTree Set1Implementation Hash set is implemented using HashTable The tree set is implemented using a tree structure. 2Null Object HashSet allows a null object The tree set does not allow the null object. It throws the null pointer exception. 3Methods Hash set use equals method to compare two objects Tree set use compare method for comparing two objects. 4Heterogeneous object Hash set doesn't now allow a heterogeneous object Tree set allows a ... Read More

Difference between Iterator and Enumeration in Java

Mahesh Parahar
Updated on 18-Nov-2019 06:30:54

6K+ Views

Iterator and Enumeration both are the cursors to traverse and access an element from the collection. They both belong to the collection framework. Enumeration was added in JDK1.0 and Iterator in the JDK.1.2 version in the collection framework. Enumeration can’t make structural changes in the collection because it has read-only access to the element in the collection. It has the following methods :*hasMoreElements()*nextElement()On the other hand, an iterator can read and remove the element in the collection. It has the following methods −*hasNext()*next()*remove()Sr. No.KeyIteratorEnumeration1BasicIn Iterator,  we can read and remove element while traversing element in the collections. Using Enumeration, we can only ... Read More

Difference between Save and SaveAndFlush in Spring Java

Mahesh Parahar
Updated on 18-Nov-2019 06:14:24

4K+ Views

Save and saveAndFlush both can be used for saving entities. They both are both belong to the Spring data library. save may or may not write your changes to the DB straight away. When we call saveAndFlush system are enforcing the synchronization of your model state with the DB.Sr. No.KeySaveSaveAndFlush1RepositoryIt belongs to CrudRepositoryIt belongs to JPARepository2Data flush StrategyIt doesn't flush data directly to a database until and unless we explicitly call flush and commit method.It's flush directly flush data to a database.3Bulk SaveCrudRepository provides bulk save methodsaveAndFlush method doesn't support the bulk operation 4Data Visibility after savingIt doesn't flush data directly ... Read More

Advertisements