Found 2620 Articles for Java

Difference between List and Set in Java

Mahesh Parahar
Updated on 05-Dec-2023 09:48:36

2K+ Views

List and Set both interface belongs to the Collection framework. Both interfaces extend the Collection interface. They both are used to store a collection of objects as a single unit. Before jdk1.2, we used to use Arrays, Vectors, and Hashtable for grouping objects as a single unit. Sr. No. Key List Set 1 Positional Access The list provides positional access of the elements in the collection. Set doesn't provide positional access to the elements in the collection. 2 Implementation Implementation of List are ArrayList, LinkedList, Vector ,Stack. Implementation of a set interface is HashSet and LinkedHashSet. 3 Duplicate We can store the duplicate elements in the list. We can’t store duplicate elements in Set. 4 Ordering List maintains ... Read More

Difference between String buffer and String builder in Java

Mahesh Parahar
Updated on 05-Dec-2023 10:02:00

11K+ Views

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 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. Sr. No. Key String Buffer String Builder 1 Basic StringBuffer was introduced with the initial release of Java It was introduced in Java 5 2 Synchronized It is synchronized It is not synchronized 3 Performance It is thread-safe. So, multiple threads can’t ... Read More

Difference between inheritance and composition in Java

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

3K+ 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

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

21K+ 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

2K+ 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

6K+ 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

Advertisements