Make delicious recipes!


Below is a glossary of some of the most common terms used in the Java world.
Every Java programmer is expected to know most of these terminology.


A member declared transient is not serialized and thus does not contribute to the persistent state of an object.


A variable marked volatile is not optimized during compilation. This usually happens for variables used in multithreading environment where the value of the variable may be changed by more than one threads. Hence, it's not ok to optimize them by just considering one thread’s code.


A hashmap whose keys are allowed to be garbage collected if they have no reference to them other than the hash-map. This property of the weak hashmap makes is vulnerable to random changes. Keys may disappear while the map is in operation. Size() method may return different values even if invoked twice immediately and so on. Clearly, WeakHashMaps are used to prevent memory leaks. For exmaple, if a hash-map is expected to be in use for a long time and a normal hash-map is used instead of a WeakHashMap, then due the presence of the strong hash-map, all the keys will remain in memory forever. They will never be garbage collected and to prevent memory leak, the programmer has to remember to remove them from the hash-map. WeakHashMap automates this for the programmer. Also, if the value referred by a key has no other references, then in ordinary maps, even that value will remain in memory forever, but not so in weak-hash-maps.

Weak, Soft and Phantom Reference:

WeakReference is a reference to an object such that it can be cleared anytime by the garbage collector if that object has no other strong reference remaining.

SoftReference is like weak reference, but the object it refers to is garbage collected only the JVM is not having enough memory. So, an object held by SoftReference sticks a little while longer than those held by WeakReference. Accordingly, WeakReference objects can be used to hold extremely large objects in cache so that they can be cleared as soon as their use is over. While SoftReference can be used to hold medium size objects in cache.

PhantomReference is a reference to an object which has been garbage collected. Since its been already collected, the get method of a PhantomReference always returns null.


Same as iterator, just has bigger function names and used by some older APIs


Modern version of Enumeration, has extra function remove()


An iterator meant only for Lists. It can move forward as well as backward.It can also add an element in the list and return the next or previous index to current position.


Implemented by ArrayList, LinkedList and Vector

ArrayList and Vector use a growing array in the background while LinkedList uses a linked list.

Vector is synchronized while ArrayList is not.

Uses of Primitive Wrapper Classes

  1. Wrapper class (like Integer, Double etc) objects can store an additional value of NULL

  2. Collection classes work with Objects and not primitives.

  3. Helps to write functions like equals, hashCode etc and encapsulate any other behavior.

  4. Several functions have fixed signatures where they can accept only objects. So if we pass primitives there, first of all they will not accept. Even if they accept somehow, it would be impossible to distinguish between different primitives because they will not have methods/fields like <primitive>.class.

  5. Also, since objects are passed by reference, passing an object allows us to pass different sized primitives like Double and Integer by the same object argument interface. Primitive arguments cannot handle this.

  6. Using primitives allows us to add methods to primitives types. Plain primitives cannot have any behavior defined by methods.

Useful methods in class Object

protected void finalize()

Finalize() method is called when the GC (Garbage Collector) decides that an object is not referenced from anywhere in the code and is available for clean-up.
It is called at most once per object.
Objective of this method is to allow the object to do a clean-up (like close file-handles, free system resources etc.) before being garbage collected.
The finalize() method of base class Object does nothing.
It is intended for sub-classes to override this method to perform clean-up.

Note 1:   It is legal for an object to become reachable again in its finalize method (by assigning "this" to some global variable for example). In this case, the GC will not garbage collect that object unless it becomes unreachable again. However, in the second case, its finalize() would not be called again
Note 2:   Any exception thrown by finalize() is ignored.

public final void wait()

wait() method provides 3 flavors:

public final void wait() throws InterruptedException
public final void wait(long timeout) throws InterruptedException
public final void wait(long timeout, int nanos) throws InterruptedException

wait() methods are used by a thread owning lock on an object (monitor) to give up the lock and wait to become live again when some other thread calls notify() or notifyAll().
timeout in the second method is meant to specify the maximum time a thread will wait before becoming live again.
The last method provides a fine control by allowing user to specify timeout in nanoseconds alongwith milliseconds.

Note: The waiting thread gives up the monitor before beginning to wait and so must acquire the monitor before it can start running again.

public String toString()

This method is called whenever a string representation of the object is desired, example in System.out.println()
Object.toString() by default returns:
     getClass().getName() + '@' + Integer.toHexString(hashCode())

Difference between Hashmap and Hashtable

1) Hashtable is synchronized while hashmap is not. So hashmap performs better where synchronization is not required.
2) Hashmap allows null keys and null values while hashtable does not.
3) Most code which deals with Hashmap interacts through the Map interface.
Map interface is implemented by LinkedHashMap, TreeMap etc. classes too which help to maintain a particular order of elements. So its easy to substitute TreeMap or LinkedHashMap in place of HashMap. There is no such substitute available for Hashtable. 4) Synchronization in a HashMap is easy to achieve by using Collections.synchronizedMap(myMap)
5) So in practice, Hashmap is better than Hashtable for all the use cases and should be preferred.

Like us on Facebook to remain in touch
with the latest in technology and tutorials!

Got a thought to share or found a
bug in the code?
We'd love to hear from you:

Email: (Your email is not shared with anybody)

Facebook comments:

Site Owner: Sachin Goyal