Java Differences
Java Differences
ArrayList LinkedList
ArrayList internally uses a dynamic array LinkedList internally uses a doubly linked list to
(growable/resizable) to store the elements. store the elements.
Data is stored in the form of array. Data is stored in the form of node.
Initial Capacity and Incremental Capacity is Initial Capacity and Incremental Capacity is not
applicable. applicable.
Involves shift operations. That is manipulation Does not involves any shift operations. That is
with ArrayList is slow because it internally uses manipulation with LinkedList is faster than
an array and If any element is removed from the ArrayList because it uses a doubly linked list, so
array, all the bits are shifted in memory. no bit shifting is required in memory.
Has three overloaded constructors. Has two overloaded constructors.
Memory is continuous. Memory may not be continuous.
An ArrayList class can act as a list only because it LinkedList class can act as a list and queue both
implements List only. because it implements List and Deque interfaces.
ArrayList is better for storing and accessing data. LinkedList is better for manipulating data.
(Insertion or removal of data in between)
Consumes less memory. Consumes more memory.
Difference between Array List and Vector
ArrayList Vector
ArrayList is not synchronized. That is ArrayList is Vector is synchronized. That is Vector is single
multi-threaded. threaded.
If the total number of elements exceeds than its Vector increments 100% which means it doubles
capacity, the incremental capacity of the the array size if the total number of elements
ArrayList is exceeds than its capacity. That is Current
((Current Capacity * 3)/2) + 1 Capacity * 2
ArrayList is not a legacy class. It is introduced in Vector is a legacy class. It is present since JDK 1.0.
JDK 1.2.
ArrayList is fast because it is non-synchronized. Vector is slow because it is synchronized, i.e., in a
multithreading environment, it holds the other
threads in runnable or non-runnable state until
current thread releases the lock of the object.
ArrayList uses the Iterator interface to traverse A Vector can use the Iterator interface or
the elements. Enumeration interface to traverse the elements.
Has three overloaded constructors. Has four overloaded constructors.
Cannot control the growth. That is the Can control the growth. That is the incremental
incremental capacity is fixed. capacity can be explicitly mentioned by the
programmer. It is done using one of the
overloaded constructors.
public vector (int initialCapacity, int
incrementalCapacity)
Comparable Comparator
Comparable affects the original class, i.e., the Comparator doesn't affect the original class, i.e.,
actual class is modified. the actual class is not modified.
Comparable provides compareTo() method to Comparator provides compare() method to sort
sort elements. elements.
Comparable is present in java.lang package. A Comparator is present in the java.util package.
We can sort the list elements of Comparable We can sort the list elements of Comparator type
type by Collections.sort(List) method. by Collections.sort(List, Comparator) method.
HashMap Hashtable
HashMap is non-synchronized. It is not-thread safe Hashtable is synchronized. It is thread-safe and
and can't be shared between many threads can be shared with many threads.
without proper synchronization code.
HashMap allows one null key and multiple null Hashtable doesn't allow any null key or value.
values.
HashMap is a new class introduced in JDK 1.2. Hashtable is a legacy class.
HashMap is fast. Hashtable is slow.
We can make the HashMap as synchronized by Hashtable is internally synchronized and can't be
calling this code unsynchronized.
Map m =
Collections.synchronizedMap(hashMap);
HashMap is traversed by Iterator. Hashtable is traversed by Enumerator and
Iterator.
Iterator in HashMap is fail-fast. Enumerator in Hashtable is not fail-fast.
HashMap inherits AbstractMap class. Hashtable inherits Dictionary class.
StringBuffer StringBuilder
StringBuffer is synchronized i.e. thread safe. It StringBuilder is non-synchronized i.e. not thread
means two threads can't call the methods of safe. It means two threads can call the methods
StringBuffer simultaneously. of StringBuilder simultaneously.
StringBuffer is less efficient than StringBuilder. StringBuilder is more efficient than StringBuffer.
Difference between Object and Class
Object Class
Object is an instance of a class. Class is a blueprint or template from which
objects are created.
Object is any real-world entity that is physically Class is a group of similar objects.
present or that can be experienced.
Object is a physical entity. Class is a logical entity.
Object is created through new keyword mainly Class is declared using class keyword e.g.
e.g. class Student{}
Student s1=new Student();
Object is created many times as per requirement. Class is declared once.
Object allocates memory when it is created. Class doesn't allocated memory when it is
created.
There are many ways to create object in java such There is only one way to define class in java using
as new keyword, newInstance() method, clone() class keyword.
method, factory method and deserialization.
String StringBuffer
String class is immutable. StringBuffer class is mutable.
String is slow and consumes more memory when StringBuffer is fast and consumes less memory
you concatenate too many strings because every when you concatenate strings.
time it creates new instance.
String class overrides the equals() method of StringBuffer class doesn't override the equals()
Object class. So you can compare the contents of method of Object class.
two strings by equals() method.
Throw throws
Java throw keyword is used to explicitly throw an Java throws keyword is used to declare an
exception. exception.
Checked exception cannot be propagated using Checked exception can be propagated with
throw only. throws.
Throw is followed by an instance. Throws is followed by class.
Throw is used within the method. Throws is used with the method signature.
You cannot throw multiple exceptions. You can declare multiple exceptions e.g.
public void method()throws IOException,
SQLException.
Difference between == and equals()
== equals()
It is an operator. It is a method.
In case of non-primitive, == operator compares the In case of non-primitive, the .equals() method is
reference (address). That is == checks if both used for content comparison. That is, .equals()
objects point to the same memory location. evaluates to the comparison of values in the
objects.
No such things with related to == If a class does not override the equals method,
then by default it uses equals(Object o) method
of the closest parent class that has overridden
this method.
== is used mostly to compare the primitive values. Equals() method is used to compare the contents
of the object.