While running multi-threads, there will be a significant problem when those threads try to use same resource.

For example, multiple people try to deposit money into the same account. Let’s say initial balance is $0. If 5 people are accessing at the same time, they all will see $0 as an account balance. Then, after all people’s deposit of $500 each, it will have $500 for total deposit. This is because they accessed at the same time, and they all updated balance as $500. However, the balance should be $2,500.

So, lock is needed to avoid other thread trying to use the same resource at the same time.

Primitive type

Basic types for characters, different kinds of integers, and different kinds of floating point numbers (numbers with a decimal point), and also types for the values true and false – char, int, float, and bool. All of these basic types are known as primitive types.

The value of the variable is stored in the memory location assigned to the variable. For example, if an integer variable is declared as “int a = 5″, then when we look at the memory location of “a”, there will be a “5” stored there just as expected.

Reference type

You might be confused by all the different terminology used. However, class types, object types, and reference types all mean the exact same thing.

A variable of a reference type only stores the memory address of where the object is located. For example, “SomeClass anObject”, then when we look at “anObject” in memory, it just stores an address of another place in memory where all the details of “anObject” reside. This memory address is called a reference to the object.

public: A class, method, constructor, interface etc declared public can be accessed from any other class.

protected: Variables, methods and constructors which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members’ class.

no modifier: A variable or method declared without any access control modifier is available to any other class in the same package.

private: Methods, Variables and Constructors that are declared private can only be accessed within the declared class itself.

Comparison

With both, you can enumerate(iterate) the elements in a collection of objects, but Enumeration is older, and Iterator is improved one.

Main difference between Enumeration and iterator is Iterator has a remove() method while Enumeration doesn’t.

Enumeration is used when ever we want to make Collection objects as Read-only.

Iterator is more secure and safe as compared to Enumeration because it does not allow other thread to modify the collection object while some thread is iterating over it and throws ConcurrentModificationException.