Вы находитесь на странице: 1из 279

Core Java

JAVA Collection Framework :

Collection Framework provides an architecture to store and manipulate the group of


objects.

All the operations that you perform on a data such as searching, sorting, insertion,
deletion etc. can be performed by Java Collection Framework.

Collection simply means a single unit of objects.

Collection framework provides many interfaces (Set, List, Queue,


Deque etc.) and classes (ArrayList, Vector, LinkedList,
PriorityQueue, HashSet, LinkedHashSet, TreeSet etc).

What is Collection ?

Collection represents a single unit of objects i.e. a group.

What is framework ?

provides ready-made architecture.


represents set of classes and interface.
is optional.

Collection framework
Collection framework represents a unified architecture for storing and
manipulating group of object. It has:

Interfaces and its implementations i.e. classes


Algorithm

Hierarchy of Collection Framework :

Let us see the hierarchy of collection framework. The java.util


package contains all the classes and interfaces for Collection
framework.

Interfaces are mentioned as Green color box


Classes are mentioned as purple color box

Core Java

Iterator interface :

Iterator interface provides the facility of iterating the elements in


forward direction only.

Methods of Iterator interface :

public boolean hasNext() it returns true if iterator has more


elements.
public object next() it returns the element and moves the cursor
pointer to the next element.
public void remove() it removes the last elements returned by the
iterator. It is rarely used.

Advantages Of Collection Framework :

Reduces programming effort by providing data structures and algorithms


so you don't have to write them yourself.

Increases performance by providing high-performance implementations of


data structures and algorithms. Because the various implementations
of each interface are interchangeable, programs can be tuned by
switching implementations.

Collection is resizable and can grow

Core Java
List Of Interfaces and Classes :

Interface
Set
List
Deque
Map

Hash
Balanced
Resizable Array
Table
Tree
HashSet
TreeSet
ArrayList
ArrayDeque
HashMap
TreeMap

Linked List

Hash Table + Linked List


LinkedHashSet

LinkedList
LinkedList
LinkedHashMap

Simple Collection Hierarchy :

Disadvantages of Collection Framework :

It must cast to correct type.

It can't be done compile-time type checking.

ArrayList :

Uses a dynamic array for storing elements. It extends AbstractList


and implements List

can contain duplicate elements

not synchronized

maintains insertion order

random access because array works at the index basis

manipulation slow because a lot of shifting needs to be occurred

ArrayList supports both Iterator and ListIterator for iteration

It allows null values.

It supports Generic which makes Java ArrayList even more powerful


because of enhanced type-safety

Core Java

ArrayList supports dynamic arrays that can grow as needed.

Standard Java arrays are of a fixed length. After arrays are


created, they cannot grow or shrink, which means that you must know
in advance how many elements an array will hold.

Array lists are created with an initial size. When this size is
exceeded, the collection is automatically enlarged. When objects
are removed, the array may be shrunk.

The ArrayList class supports three constructors. The first constructor


builds an empty array list.
1. ArrayList( )
The following constructor builds an array list that is initialized with
the elements of the collection c.
2. ArrayList(Collection c)
The following constructor builds an array list that has the specified
initial capacity. The capacity is the size of the underlying array that
is used to store the elements.
The capacity grows automatically as elements are added to an array list.
3. ArrayList(int capacity)

ArrayList is not a synchronized collection hence it is not suitable to be used between


multiple threads concurrently. If you want to use ArrayList then you need
to either use new CopyonWriteArrayList or use Collections.synchronizedList()
to create a synchronized List.

Core Java
Example,
List list = Collections.synchronizedList(new ArrayList(...));

CopyOnWriteArrayList<String> threadSafeList = new


CopyOnWriteArrayList<String>();
threadSafeList.add("Java");
threadSafeList.add("J2EE");
threadSafeList.add("Collection");

CopyonWriteArrayList is recommended for concurrent multi-threading


environment as it is optimized for multiple concurrent read and
creates copy for write operation.

Example Of ArrayList :
1) Creating ArrayList
ArrayList<String> stringList = new ArrayList<String>(); //Generic
ArrayList to Store only String objects

2) Checking size of ArrayList


Size of an ArrayList in Java is total number of elements currently stored
in ArrayList.
Int size = stringList.size();

3) Checking Index of an Item in Java Arraylist

You can use indexOf() method of ArrayList in Java to find out index of a
particular object.
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not
contain the element.
Int index = stringList.indexOf("Item");//location of Item object in List
Returns the index of the last occurrence of the specified element in this list, or -1 if this list does
not contain the element.
Int index = stringList.lastIndexOf("Item");//location of Item object in
List
4) Checking ArrayList for an Item
Sometimes we need to check whether an element exists in ArrayList in Java
or not for this purpose we can use contains() method of Java. contains()
method takes type of object defined in ArrayList creation and returns
true if this list contains the specified element.

Core Java
contains(Object o)
Returns true if this list contains the specified element
5) Removing an Item from ArrayList

There are two ways to remove any elements from ArrayList in Java. You can
either remove an element based on its index or by providing object
itself.

Remove remove (int index) and remove (Object o) method is used to remove
any element from ArrayList in Java.

Since ArrayList allows duplicate its worth noting that remove(Object o)


removes the first occurrence of the specified element from this
list, if it is present.

In below code first call will remove first element from ArrayList
while second call will remove first occurrence of item from
ArrayList in Java.

stringList.remove(0); Removes the element at the specified position in this list.


stringList.remove(item); Removes the first occurrence of the specified element from this list, if it is
present.
6) Copying data from one ArrayList to another ArrayList in Java

Many a times you need to create a copy of ArrayList for this purpose you
can use addAll(Collection c) method of ArrayList in Java to copy all
elements from on ArrayList to another ArrayList in Java.

Below code will add all elements of stringList to newly created


copyOfStringList.

ArrayList<String>copyOfStringList =newArrayList<String>();
copyOfStringList.addAll(stringList);

7) Replacing an element at a particular index

You can use set (int index, E element) method of java ArrayList to
replace any element from a particular index.

stringList.set(0,"Item2");

8) Converting from ArrayList to Array in Java

Java ArrayList provides you facility to get the array back from your
ArrayList.

Core Java

You can use toArray(T[] a) method returns an array containing all of


the elements in this list in proper sequence (from first to last
element).

String[]itemArray =newString[stringList.size()];
String[]returnedArray = stringList.toArray(itemArray);
9) Converting from Array to ArrayList in Java
This is the simplest way of converting from array to arraylist.
String[] asset = {"equity", "stocks", "gold", "foreign exchange","fixed income", "futures",
"options"};
List assetList =Arrays.asList(asset);
Its worth to note following point while using Arrays.asList() method for
array to arraylist conversion
1) This method returns a List view of underlying array.
2) List returned by this method would be fixed size.
3) Most important point to note is when you change an element into this List corresponding
element in original array will also be changed.
4) Another important point is since List is fixed size, you can not add element into it. If you
try you will get exception.
5) This is the most efficient way of converting array to arraylist as per my
knowledge because it doesn't copy the content of underlying array to create list.
6) From Java 5 or JDK 1.5 onwards this method supports generic so you can
generate type safe ArrayList from array.
10) Array to ArrayList by using Collections.addAll method
This example of converting an array to arraylist is not that efficient as
the earlier method but its more flexible.
List assetList = new ArrayList();
String[] asset = {"equity", "stocks", "gold", "foriegn exchange", "fixed income", "futures",
"options"};
Collections.addAll(assetList, asset);
Important point about this method of array to ArrayList conversion is :
1) Its not as fast as Arrays.asList() but more flexible.
2) This method actually copies the content of the underlying array into
ArrayList provided.
3) Since you are creating copy of original array, you can add, modify and remove any element
without affecting original one.
4) If you want to provide individual element you can do so by specifying

Core Java
them individually as comma separated. Which is a very convenient way of
inserting some more elements into existing list for example,
Collections.addAll(assetList,"EquityDerivatives","Eqity Index
Arbitrage","Mutual Fund");
11) This is another great way of converting an array to arraylist.
We are essentially using Collection interface's addAll() method for
copying content from one list to another.
Since List returned by Arrays.asList is fixed-size its not much of use,
this way we can convert that into proper arraylist.
Arraylist newAssetList =newArraylist();
newAssetList.addAll(Arrays.asList(asset));

12) Creating Synchronized ArrayList


Some times you need to synchronize your ArrayList in java to make it shareable
between multiple threads you can use Collections utility class for this
purpose as shown below.
List list =Collections.synchronizedList(newArrayList(...)); (or)
CopyOnWriteArrayList<String> threadSafeList = new
CopyOnWriteArrayList<String>();
threadSafeList.add("Java");
threadSafeList.add("J2EE");
threadSafeList.add("Collection");
13) ArrayList to HashSet conversion

Most of Collection class provides a constructor which accepts a


Collection object as argument. Which can be used to copy all
elements of one Collection into another.

HashSet also provide such constructors which can be used to copy


all object from ArrayList to HashSet. But be careful since HashSet
doesn't allow duplicates some of the objects will not be included
which result in less number of objects.

Also List allows duplicates but Set doesn't allow any duplicates, which means if you have duplicates
in your ArrayList they will be lost when you convert ArrayList to HashSet and that's the reason why
sometime size of ArrayList doesn't match with size of HashSet after conversion

Example,
HashSet companySet = new HashSet(Collection<? extends E> c);

Core Java
14) If we set ArrayList reference null in Java all the elements inside
ArrayList becomes eligible to garbage collection in java , provided there are no
more reference exists for those objects.
Difference between Arrays and ArrayList :

Arrays

ArrayList

ArrayList are not strongly


Arrays are strongly typed. Hence only
same type of elements can stored.
typed(Loosely typed).
(Ex. if A is an array, here we
can store only integer or string or
bool etc..)
Since strongly typed, boxing and
Since Loosely typed, boxing and
unboxing never happens. This result unboxing happens which results in
in better performance
performance problem
These are fixed in size and will
not grow dynamically runtime

They can grow dynamically while


runtime

To store and to retrieve we should


use integral index
Array dont support Add() and
Remove() functions

ArrayList provide sufficient methods


to work with like Add()

ADVANTAGES OF USING ARRAYLIST

An array is type specific i.e. one array can only store data of
only one data type where as arraylist can store multiple data in
the form of objects. So multiple objects of different data types
can be stored in arraylist.

In array you cannot increase or decrease the size dynamically where


as in arraylist the size can be increased or decreased dynamically.

You can make arraylist readonly.

You can insert element at any location in arraylist.

Arraylist supports BinarySearch.

ArrayLists implement iterable , which means you can easily iterate


them.

If an arraylist is modified by another thread while it is being


iterated by one thread , then it fails fast , which means you don't
end up with unreliable data in the list.

Core Java
DISADVANTAGES OF USING ARRAYLIST

Not efficient for large quantity of data because when the arraylist has to
increase its size, its content is copied to a larger arraylist
which can be a slow process.

It can only holds Objects i.e. it cant hold normal data types values
such as int .

Make ArrayList as ReadOnly :


Example,
Syntax :
public static <T> List<T> unmodifiableList(List<? extends T> list)
Which is present in Collections Class in java.util package
ArrayList al = new ArrayList();
al.add("string");
List alNew = Collections.unmodifiableList(al);
Note : The returned list will be serializable if the specified list is
serializable
ArrayList is not Synchronized by Default :
Array list is not synchronized means that object is mutable that means once
creating the Array list object that object is calling two threads at a
time but one thread is changing the value of object that can be effected
by another object so Array list is not thread safe so Array list is not Synchronized by
default

Vector vs ArrayList :
Common property of Vector and ArrayList in Java
1) Both Vector and ArrayList are derived from AbstractList and implements
List interface, which means both of them are ordered collection and allows
duplicates.
2) Another similarity between Vector vs ArrayList is that both are index
based Collection and you can use get(index) method to retrieve objects from
Vector and ArrayList.
Vector vs ArrayList in Java
1) First and most common difference between Vector vs ArrayList is that
Vector is synchronized and thread-safe while ArrayList is neither Synchronized nor

Core Java
thread-safe. Now, What does that mean? It means if multiple thread try to
access Vector same time they can do that without compromising Vector's
internal state. Same is not true in case of ArrayList as methods like
add(), remove() or get() is not synchronized.
2) Second major difference on Vector vs ArrayList is Speed, which is directly
related to previous difference. Since Vector is synchronized, its slow
and ArrayList is not synchronized its faster than Vector.
3) Third difference on Vector vs ArrayList is that Vector is a legacy class
and initially it was not part of Java Collection Framework.
These were some comparison on Vector vs ArrayList. In Summary use
ArrayList if you are using ArrayList in Single threaded environment and
use Vector if you need a thread-safe collection. ArrayList is anytime faster
than Vector in case thread-safety is not a concern.
What do you mean by Legacy class in JAVA?

Legacy classes are those that were built using Java 1.1 or Java 1.2
API. In general we cannot use this term for java classes. Say for
example when Java 1.0 was available Vector was used instead of
dynamic array and since there was no ArrayList class people were
forced to use Vector for this purpose.

When Java 1.2 was released ArrayList was much more advanced to
Vector but has all the features of Vector too. So people started
using ArrayList instead of Vector (this again depends on the
project requirement, both Vector and ArrayList have their own
advantages). And in this case Vector became legacy class.

But this is not constant since in future Sun may introduce a new
class that is much more advanced to ArrayList and Vector. And then
we call both ArrayList and Vector as legacy classes.

But in general a "legacy" is a term used to define a software


created using older version of software.

Example,
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("Text 1");
list.add("Text 2");
list.add("Text 3");
System.out.println("#1 normal for loop");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}

Core Java
System.out.println("#2 advance for loop");
for (String temp : list) {
System.out.println(temp);
}
System.out.println("#3 while loop");
int j = 0;
while (list.size() > j) {
System.out.println(list.get(j));
j++;
}

System.out.println("#4 iterator");
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}

What is Mutable and Immutable Objects in Java ?

An immutable object is an object whose state cannot be modified after


it is created.

This is in contrast to a mutable object, which can be modified after


it is created

Example ,
int i = 42; //int is of primitive type
i = 43; // OK

Mutable

final int j = 42;


Immutable
j = 43; // does not compile. j is final so can't be reassigned
In Java, objects are referred by references. If an object is known to be
immutable, the object reference can be shared. For example, Boolean, Byte,
Character, Double, Float, Integer, Long, Short, and String are immutable classes
in Java, but the class StringBuffer is a mutable object in addition to the
immutable String.
class Program {
public static void main(String[] args) {
String str = "HELLO";
System.out.println(str);
str.toLowerCase();
System.out.println(str);
}
}
The output result is
HELLO
HELLO
From the above example, the toLowerCase() method does not impact on the

Core Java
original content in str. What happens is that a new String object
"hello" is constructed. The toLowerCase() method returns the new
constructed String object's reference. Let's modify the above code:
class Program {
public static void main(String[] args) {
String str = "HELLO";
System.out.println(str);
String str1 = str.toLowerCase();
System.out.println(str1);
}
}
The output result is
HELLO
hello
The str1 references a new String object that contains "hello". The
String object's method never affects the data the String object
contains, excluding the constructor.

Difference between String and


StringBuffer/StringBuilder in Java
Well, the most important difference between String and StringBuffer/StringBuilder
in java is that String object is immutable whereas StringBuffer /
StringBuilder objects are mutable.
By immutable, we mean that the value stored in the String object cannot be
changed. Then the next question that comes to our mind is If String is immutable
then how am I able to change the contents of the object whenever I wish to? . Well,
to be precise its not the same String object that reflects the changes you do.
Internally a new String object is created to do the changes.
So suppose you declare a String object:
String myString = Hello;
Next, you want to append Guest to the same String. What do you do?
myString = myString + Guest;
When you print the contents of myString the output will be Hello Guest. Although
we made use of the same object(myString), internally a new object was
created in the process. So, if you were to do some string operation involving an

Core Java
append or trim or some other method call to modify your string object, you would
really be creating those many new objects of class String.
Now isnt that a performance issue?
Yes, it definitely is.
Then how do you make your string operations efficient?
By using StringBuffer or StringBuilder.
How would that help?
Well, since StringBuffer/StringBuilder objects are mutable, we can make
changes to the value stored in the object. What this effectively means is that string
operations such as append would be more efficient if performed using
StringBuffer/StringBuilder objects than String objects.
Finally, whats the difference between StringBuffer and StringBuilder?
StringBuffer and StringBuilder have the same methods with one difference and
thats of synchronization. StringBuffer is synchronized( which means it is
thread safe and hence you can use it when you implement threads for your
methods) whereas StringBuilder is not synchronized( which implies it isnt
thread safe).
So, if you arent going to use threading then use the StringBuilder class
as itll be more efficient than StringBuffer due to the absence of
synchronization.

Heres how you use StringBuilder


Now get out of that bad habit of using String objects to perform operations on
strings. With StringBuilder(introduced in J2Se 5.0), you can perform those very
append and other such operations more efficiently.
Scene 1: typical coder. using the same old String object to perform append
operations.

Core Java
String s = Hello;
s = s + World;
system.out.println(s);
I agree itll give you Hello World, but fella. lets be different from the rest and
think about making the code more efficient.
Enter Scene 2.
StringBuilder sb = new StringBuilder(Hello);
sb.append( World);
system.out.println(sb);
well thats it!! Youll get your Hello World but in a more efficient way

Simple example to demonstrate that String object is


immutable
In my post Difference between String and StringBuffer/StringBuilder I told you
stuff like String object is immutable( meaning the value stored in the object cannot
be changed) and that when you perform operations such as concat or replace,
internally a new object is created to hold the result.
Below is a simple example that will make you believe that what I said about String
object is indeed true!
String s = Lets test;
s.concat( if the String object is IMMUTABLE);

Core Java
System.out.println(s);
s = s.concat( if the String object is IMMUTABLE);
System.out.println(s);
The output of the above code will be:
Lets test
Lets test if the String object is IMMUTABLE
Thats all people! The above piece of code proves that String is immutable and
hence the results of operations like concat etc. should be stored into a new object.

LinkedList :

uses doubly linked list to store the elements. It extends the


AbstractList class and implements List and Deque interfaces.
Contain duplicate elements.
maintains insertion order.
Multiple Null values allowed here
not synchronized.
No random access.
manipulation fast because no shifting needs to be occur.
can be used as list, stack or queue.

Example,
public static void main(String[] args) {
System.out.println("Linked List Example!");
LinkedList <Integer>list = new LinkedList<Integer>();
int num1 = 11, num2 = 22, num3 = 33, num4 = 44;
int size;
Iterator iterator;
//Adding data in the list
list.add(num1);
list.add(num2);
list.add(num3);

Core Java
list.add(num4);
size = list.size();
System.out.print( "Linked list data: ");
//Create a iterator
iterator = list.iterator();
while (iterator.hasNext()){
System.out.print(iterator.next()+" ");
}
System.out.println();
//Check list empty or not
if (list.isEmpty()){
System.out.println("Linked list is empty");
}
else{
System.out.println( "Linked list size: " + size);
}
System.out.println("Adding data at 1st location: 55");
//Adding first
list.addFirst(55);
System.out.print("Now the list contain: ");
iterator = list.iterator();
while (iterator.hasNext()){
System.out.print(iterator.next()+" ");
}
System.out.println();
System.out.println("Now the size of list: " + list.size());
System.out.println("Adding data at last location: 66");
//Adding last or append
list.addLast(66);
System.out.print("Now the list contain: ");
iterator = list.iterator();
while (iterator.hasNext()){
System.out.print(iterator.next()+" ");
}
System.out.println();
System.out.println("Now the size of list: " + list.size());
System.out.println("Adding data at 3rd location: 55");
//Adding data at 3rd position
list.add(2,99);
System.out.print("Now the list contain: ");

Core Java
iterator = list.iterator();
while (iterator.hasNext()){
System.out.print(iterator.next()+" ");
}
System.out.println();
System.out.println("Now the size of list: " + list.size());
//Retrieve first data
System.out.println("First data: " + list.getFirst());
//Retrieve lst data
System.out.println("Last data: " + list.getLast());
//Retrieve specific data
System.out.println("Data at 4th position: " + list.get(3));
//Remove first
int first = list.removeFirst();
System.out.println("Data removed from 1st location: " + first);
System.out.print("Now the list contain: ");
iterator = list.iterator();
//After removing data
while (iterator.hasNext()){
System.out.print(iterator.next()+" ");
}
System.out.println();
System.out.println("Now the size of list: " + list.size());
//Remove last
int last = list.removeLast();
System.out.println("Data removed from last location: " + last);
System.out.print("Now the list contain: ");
iterator = list.iterator();
//After removing data
while (iterator.hasNext()){
System.out.print(iterator.next()+" ");
}
System.out.println();
System.out.println("Now the size of list: " + list.size());
//Remove 2nd data
int second = list.remove(1);
System.out.println("Data removed from 2nd location: " + second);
System.out.print("Now the list contain: ");
iterator = list.iterator();
//After removing data

Core Java
while (iterator.hasNext()){
System.out.print(iterator.next()+" ");
}
System.out.println();
System.out.println("Now the size of list: " + list.size());
//Remove all
list.clear();
if (list.isEmpty()){
System.out.println("Linked list is empty");
}
else{
System.out.println( "Linked list size: " + size);
}
}
public class LinkedListTest {
public static void main(String[] args) {
List<String> ll = new LinkedList<String>();
ll.add("3");
ll.add("2");
ll.add(null);
ll.add("4");
ll.add("4");
ll.add(null);
ll.add(null);
ll.add(null);

}
}
Output :
3
2
null
4
4
null
null
null

Iterator<String> iter2 = ll.iterator();


while(iter2.hasNext()){
System.out.println(iter2.next());
}

Core Java
Doubly Linked List :

A doubly-linked list is a linked data structure that consists of a set


of sequentially linked records called nodes.

Each node contains two fields, called links, that are references to
the previous and to the next node in the sequence of nodes.

The beginning and ending nodes' previous and next links,


respectively, point to some kind of terminator, typically a
sentinel node or null, to facilitate traversal of the list.

If there is only one sentinel node, then the list is circularly


linked via the sentinel node. It can be conceptualized as two
singly linked lists formed from the same data items, but in
opposite sequential orders

ArrayList vs LinkedList vs Vector :

List, as its name indicates, is an ordered sequence of elements.

When we talk about List, it is a good idea to compare it with Set


which is a set of elements which is unordered and every element is unique.
The following is the class hierarchy diagram of Collection.

Core Java
ArrayList vs. LinkedList vs. Vector

From the hierarchy diagram, they all implement List interface.

They are very similar to use. Their main difference is their


implementation which causes different performance for different operations.

ArrayList is implemented as a resizable array. As more elements are


added to ArrayList, its size is increased dynamically. It's elements can
be accessed directly by using the get and set methods, since ArrayList is
essentially an array.

LinkedList is implemented as a double linked list. Its performance on add


and remove is better than Arraylist, but worse on get and set methods.

Vector is similar with ArrayList, but it is synchronized.

ArrayList is a better choice if your program is thread-safe.

Vector and ArrayList require space as more elements are added.


Vector each time doubles its array size, while ArrayList grow 50% of its
size each time.

LinkedList, however, also implements DeQue interface which adds more


methods than ArrayList and Vector, such as offer(), peek(), poll(),
etc.

Note: The default initial capacity of an ArrayList is pretty small. It is


a good habit to construct the ArrayList with a higher initial capacity.
This can avoid the resizing cost.

Time Duration :
ArrayList add: 13265642
LinkedList add: 9550057
ArrayList get: 1543352
LinkedList get: 85085551
ArrayList remove: 199961301
LinkedList remove: 85768810
The difference of their performance is obvious. LinkedList is faster in add and
remove, but slower in get. LinkedList should be preferred if:

There are no large number of random access of element GET


There are a large number of add/remove operations Add/Remove

Core Java
List Interface :
List Interface is the subinterface of Collection. It contains methods to
insert and delete elements in index basis. It is a factory of
ListIterator interface.

public void add(int index,Object element);

public boolean addAll(int index,Collection c);

public object get(int Index position);

public object set(int index,Object element);

public object remove(int index);

public ListIterator listIterator();

public ListIterator listIterator(int i);

ListIterator :
ListIterator Interface is used to traverse the element in backward and
forward direction

public boolean hasNext();

public Object next();

public boolean hasPrevious();

public Object previous();

Difference between List & ListIterator :


There are two Differences :
1) We can use Iterator to traverse Set and List and also Map type of
Objects. But List Iterator can be used to traverse for List type Objects, but
not for Set type of Objects.
That is, we can get a Iterator object by using Set and List, see here :
By using Iterator we can retrieve the elements from Collection Object in
forward direction only.
Methods in Iterator :
1.hashNext()
2.next()
3.remove()
Iterator ite = Set.iterator();
Iterator ite = List.iterator();

Core Java
2) But we get List Iterator object only from List interface, see here :
where as List Iterator, which allows you to traverse in either directions.
That is List Iterators traverse two directions. So it has another methods
hasPrevious() & previous() other than Iterator.
Methods in ListIterator
1.hasNext()
2.next()
3.previous()
4.hasPrevious()
5.remove()
6.nextIndex()
7.previousIndex()
ListIterator listite = List.listIterator();
i.e., we can't get List Iterator object from Set interface.
Sno

Iterator

List Iterator

Using iterator you can visit


the elements of collection in
the forward direction only

In list iterator you can visit the


elements of collection in the
forward and backward direction.

Using iterator you cannot add


elements to collection

Using list Iterator you can add


elements to collection

Using iterator you can not


replace existing object with
new object

Using iterator you can replace


existing object with new object

Using iterator you can not get


index of elements

Using list Iterator you can get


index of elements list

Methods in Iterator :

Methods in ListIterator

hashNext()
next()
remove()

hasNext()
next()
previous()
hasPrevious()
remove()
nextIndex()
previousIndex()

Iterator to traverse Set and List


and also Map type of Objects

List Iterator can be used to traverse


for List type Objects

Here cursor position always


points to a specific index

Here cursor lies between the element


that will be returned by call to
previous() and element that will be
returned by call to next()

Core Java
Example,
public class IteratorAndListIterator {
public static void main(String[] args) {
List<String> namesList = new ArrayList<String>();
namesList.add("A");
namesList.add("B");
namesList.add("C");
namesList.add("D");
namesList.add("E");
Iterator<String> iterator = namesList.iterator();
System.out.println("Using Iterator");
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
System.out.println();
System.out.println("Using List Iterator");
ListIterator<String> listIterator = namesList.listIterator();
while (listIterator.hasNext()) {
System.out.print(listIterator.nextIndex());
System.out.println(" " + listIterator.next());
}

element");

System.out.println();
System.out.println("Using ListIterator with Insertion of an
listIterator = namesList.listIterator();
while (listIterator.hasNext()) {
System.out.print(listIterator.nextIndex());
if (listIterator.nextIndex() == 2) {
listIterator.add("Q");
}
System.out.println(" " + listIterator.next());
}

System.out.println();
System.out.println("Using ListIterator with iteration in
reverse order");
listIterator = namesList.listIterator(6);
while (listIterator.hasPrevious()) {
System.out.print(listIterator.previousIndex());
System.out.println(" " + listIterator.previous());
}
}
}

Core Java
Output
Using Iterator
A
B
C
D
E
Using List Iterator
0A
1B
2C
3D
4E
Using ListIterator with Insertion of an element
0A
1B
2C
4D
5E
Using ListIterator with iteration in reverse order
5E
4D
3C
2Q
1B
0A

Difference between List and Set :


List

Set

Is an Ordered grouping of elements

Is an Unordered grouping of elements

List is used to collection of


elements with duplicates

Set is used to collection of elements


without duplicates

Can be accessed by index

Can not be accessed by index

LinkedList

HashSet (unordered)

ArrayList

LinkedHashSet (ordered)
TreeSet (sorted by natural order or
by provided comparator)

SET :

At most Allows one null element

Does not allow duplicate elements

Unordered grouping of elements

Core Java

Can not be accessed by index basis

Extends AbstractSet class implements Set interface

HashSet :

Uses hashtable to store the elements. It extends AbstractSet class


and implements Set interface.

Contains unique elements only.

A HashSet is an unsorted, unordered Set

HashSet is not synchronized. If more than one thread wants to


access it at the same time then it must be synchronized externally.

Example,
class Simple{
public static void main(String args[]){
HashSet al=new HashSet();
al.add("Ravi");
al.add("Vijay");
al.add("Ravi");
al.add("Ajay");
Iterator itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
Output : Ajay
Vijay
Ravi
The HashSet class supports four constructors. The first form constructs a
default hash set:

1) HashSet( )

Core Java
The following constructor form initializes the hash set by using the
elements of c.
2) HashSet(Collection c)
The following constructor form initializes the capacity of the hash set
to capacity.
The capacity grows automatically as elements are added to the Hash.
3) HashSet(int capacity)
The fourth form initializes both the capacity and the fill ratio (also
called load capacity) of the hash set from its arguments:
4) HashSet(int capacity, float fillRatio)
Make it Synchronized Set :
Set s = Collections.synchronizedSet(new HashSet());
...
synchronized (s) {
Iterator i = s.iterator(); // Must be in the synchronized block
while (i.hasNext())
foo(i.next());
}
Make it Read-Only Colelction :
Syntax :
public static <T> Set<T> unmodifiableSet(Set<? extends T> s)
Collections.unmodifiableSet(new HashSet());
Example,
public static void main(String[] args) {
// TODO Auto-generated method stub
Set<String> set = new HashSet<String>();
set.add("O");
set.add("C");
set.add("A");
set.add("B");
/*Set<String> linkedSet = new LinkedHashSet<String>();
linkedSet.add("O");
linkedSet.add("C");
linkedSet.add("A");
linkedSet.add("B");

Core Java
for (String str : linkedSet) {
System.out.println(str);
}*/
for (String str : set) {
System.out.println(str);
}
}
Output : (default ascending order)
A
B
C
O
Using LinkedHashset it displays insertion order like
O
C
A
B
public static void main(String [] args) {
System.out.println( "Collection Example!\n" );
int size;
// Create a collection
HashSet <String>set = new HashSet <String>();
String str1 = "Yellow", str2 = "White", str3 = "Green", str4 = "Blue";
Iterator iterator;
//Adding data in the collection
set.add(str1);
set.add(str2);
set.add(str3);
set.add(str4);
System.out.print(" set data: ");
//Create a iterator
iterator = set.iterator();
while (iterator.hasNext()){
System.out.print(iterator.next() + " ");
}
System.out.println();
// Get size of a collection
size = set.size();
if (set.isEmpty()){
System.out.println(" set is empty");
}
else{
System.out.println( " set size: " + size);

Core Java
}
System.out.println();
// Remove specific data
set.remove(str2);
System.out.println("After removing [" + str2 + "]\n");
System.out.print("Now collection data: ");
iterator = set.iterator();
while (iterator.hasNext()){
System.out.print(iterator.next() + " ");
}
System.out.println();
size = set.size();
System.out.println(" set size: " + size + "\n");
//Collection empty
set.clear();
size = set.size();
if (set.isEmpty()){
System.out.println(" set is empty");
}
else{
System.out.println( " set size: " + size);
}
}
Readonly collections
public class ReadOnlyCollections {
public static void main(String args[]) {
// creating a list
List godList = Arrays
.asList(new String[] { "Donald", "Dennis", "Ken" });

// making a read-only list


List list = new ArrayList(godList);
list = Collections.unmodifiableList(list);

// checking the reference in a read-only set


Set set = new HashSet(godList);
Collections.unmodifiableSet(set);

Core Java
// the following statement allows to modify the above set as the
// reference is pointing to the original collection therefore it is not
// read-only
set.add("Alan");

// making a read-only map and try to modify it


Map godMap = new HashMap();
godMap.put("TAOCP", "Donald");
godMap.put("C", "Dennis");
godMap = Collections.unmodifiableMap(godMap);
try {
// modifying the read-only map to check what happens
godMap.put("Unix", "Ken");
} catch (UnsupportedOperationException e) {
System.out.println("You cannot modify a read only collection!");
}
}
}

LinkedHashSet :

Contains unique elements only like HashSet.

Core Java

It extends HashSet class and implements Set interface.

Maintains insertion order.

A LinkedHashSet is an ordered version of HashSet that maintains a


doubly-linked List across all elements.

Use this class instead of HashSet when you care about the iteration
order.

When you iterate through a HashSet the order is unpredictable,


while a LinkedHashSet lets you iterate through the elements in the
order in which they were inserted.

HashSet vs LinkedHashSet :

The only difference is that the LinkedHashSet maintains the order of the
items added to the Set.

It does this by maintaining a doubly linked list containing the


hash and the original order of the items.

According to Sun, the LinkedHashSet should run nearly as fast as


the HashSet.

TreeSet :

Contains unique elements only like HashSet.

The TreeSet class implements NavigableSet interface that extends the


SortedSet interface

Maintains ascending order.

It extends AbstractSet class

A NavigableSet implementation based on a


TreeMap. The elements are ordered using their
natural ordering, or by a Comparator provided at set
creation time, depending on which
constructor is used.

Its not synchronized. To make it as


synchronized set do the following,

SortedSet s = Collections.synchronizedSortedSet(new
TreeSet(...));

Core Java
Example,
public static void main(String[] args) {
System.out.println("Tree Set Example!\n");
TreeSet tree = new TreeSet();
tree.add(12);
tree.add(63);
// Output 12, 34, 45, 63
tree.add(34);
tree.add(45);

// here it test it's sorted, 63 is the last element. see output below
Iterator iterator = tree.iterator();
System.out.print("Tree set data: ");
//Displaying the Tree set data
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
System.out.println();
//Check empty or not
if (tree.isEmpty()) {
System.out.print("Tree Set is empty.");
} else {
System.out.println("Tree Set size: " + tree.size());
}
//Retrieve first data from tree set
System.out.println("First data: " + tree.first());
//Retrieve last data from tree set
System.out.println("Last data: " + tree.last());
if (tree.remove(45)) { // remove element by value
System.out.println("Data is removed from tree set");
} else {
System.out.println("Data doesn't exist!");
}
System.out.print("Now the tree set contain: ");
iterator = tree.iterator();

//Displaying the Tree set data


while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
System.out.println();
System.out.println("Now the size of tree set: " + tree.size());
//Remove all
tree.clear();
if (tree.isEmpty()) {
System.out.print("Tree Set is empty.");
} else {
System.out.println("Tree Set size: " + tree.size());
}
}

Core Java
HashSet vs TreeSet vs LinkedHashSet :

In a set, there are no duplicate elements. That is one of the major


reasons to use a set.

There are 3 commonly used implementations of Set in Java:


1) HashSet,
2) TreeSet and
3) LinkedHashSet.
When and which to use is an important question.

In brief, if we want a fast set, we should use HashSet;

if we need a sorted set, then TreeSet should be used;

if we want a set that can be read by following its insertion order,


LinkedHashSet should be used.

HashSet is Implemented using a hash table. Elements are not ordered.


The add, remove, and contains methods has constant time complexity
O(1).

TreeSet is implemented using a tree structure(red-black tree in


algorithm book). The elements in a set are sorted, but the add,
remove, and contains methods has time complexity of O(log (n)). It
offers several methods to deal with the ordered set like first(),
last(), headSet(), tailSet(), etc.

LinkedHashSet is between HashSet and TreeSet. It is implemented as a


hash table with a linked list running through it, so it provides the
order of insertion. The time complexity of basic methods is O(1).

What is common in HashSet and TreeSet in Java .?


1)Both HashSet and TreeSet implements java.util.Set interface which means
they follow contract of Set interface and doesn't allow any duplicates.
2)Both HashSet and TreeSet are not thread-safe and not synchronized. Though you
can make them synchronized by using Collections.synchronizedSet() method.
3) Third similarity between TreeSet and HashSet is that, Iterator of both
classes are fail-fast in nature. They will throw
ConcurrentModificationException if Iterator is modified once Iterator is
created. this is not guaranteed and application code should not rely on
this code but Java makes best effort to fail as soon as it detects
structural change in underlying Set.

Core Java
HashSet vs TreeSet in Java
Now let's see couple of differences between HashSet vs TreeSet in Java.
This is enough to decide whether you should use HashSet or TreeSet in a
given scenario.
1) First major difference between HashSet and TreeSet is performance.
HashSet is faster than TreeSet and should be preferred choice if sorting
of element is not required.
2) Second difference between HashSet and TreeSet is that HashSet allows null
object but TreeSet doesn't allow null Object and throw NullPointerException, Why,
because TreeSet uses compareTo() method to compare keys and compareTo() will
throw java.lang.NullPointerException as shown in below example :
HashSet <String> hashSet =new HashSet<String>();
hashSet.add("Java");
hashSet.add(null);
TreeSet <String> treeSet =new TreeSet<String>();
treeSet.add("C++");
treeSet.add(null);//Java.lang.NullPointerException
Output:
Exceptionin thread"main"java.lang.NullPointerException
at java.util.TreeMap.put(TreeMap.java:541)
at java.util.TreeSet.add(TreeSet.java:238)
at test.CollectionTest.main(CollectionTest.java:27)
JavaResult:1

3) Another significant difference between HashSet and TreeSet is that ,


HashSet is backed by HashMap while TreeSet is backed by TreeMap in Java.
4) One more difference between HashSet and TreeSet which is worth
remembering is that HashSet uses equals() method to compare two object in Set
and for detecting duplicates while TreeSet uses compareTo() method for
same purpose. If equals() and compareTo() are not consistent, i.e. for two equal
object equals should return true while compareTo() should return zero,
than it will break contract of Set interface and will allow duplicates in
Set implementations like TreeSet
5) Now most important difference between HashSet and TreeSet is ordering.
HashSet doesn't guaranteed any order while TreeSet maintains objects in
Sorted order defined by either Comparable or Comparator method in Java.

Core Java
Comparable Interface : (for sorting purpose)

Comparable interface is used to order the objects of user-defined


class.

This interface is found in java.lang package and contains only one


method named compareTo(Object).

It provide only single sorting sequence i.e. you can sort the elements
based on single data member only.

public int compareTo(Object obj) is used to compare the current object with the
specified object.

Collections class provides static methods for sorting the elements


of collection.

If collection elements are of Set type, we can use TreeSet. But We


cannot sort the elements of List.

Collections class provides methods for sorting the elements of List


type elements.

public void sort(List list) is used to sort the elements of List. List
elements must be of Comparable type.

Note: String class and Wrapper classes implements the Comparable


interface. So if you store the objects of string or wrapper
classes, it will be Comparable.

If any class implements comparable interface then collection of


that object can be sorted automatically using Collection.sort() or
Arrays.sort(). Object will be sort on the basis of compareTo method in
that class.

Example ,
class Student implements Comparable{
int rollno;
String name;
int age;
Student(int rollno,String name,int age){
this.rollno=rollno;
this.name=name;
this.age=age;
}

Core Java
public int compareTo(Object obj){
Student st=(Student)obj;
if(age==st.age)
return 0;
else if(age > st.age)
return 1;
else
return -1;
}}

Comparator interface :

Comparator interface is used to order the objects of user-defined class.

This interface is found in java.util package and contains only one


method named compare(Object obj1,Object obj2).

It provides multiple sorting sequence i.e. you can sort the elements
based on any data member.

public int compare(Object obj1,Object obj2) compares the first object with
second object.

Example,
1) class AgeComparator implements Comparator{
public int Compare(Object o1,Object o2){
Student s1=(Student)o1;
Student s2=(Student)o2;

if(s1.age==s2.age)
return 0;
else if(s1.age>s2.age)
return 1;
else
return -1;
}
}

Core Java
2) class NameComparator implements Comparator{
public int Compare(Object o1,Object o2){
Student s1=(Student)o1;
Student s2=(Student)o2;

return s1.name.compareTo(s2.name);
}
}
3) class Simple{
public static void main(String args[]){
ArrayList al=new ArrayList();
al.add(new Student(101,"Vijay",23));
al.add(new Student(106,"Ajay",27));
al.add(new Student(105,"Jai",21));
System.out.println("Sorting by Name...");
Collections.sort(al,new NameComparator());
Iterator itr=al.iterator();
while(itr.hasNext()){
Student st=(Student)itr.next();
System.out.println(st.rollno+" "+st.name+" "+st.age);
}
System.out.println("sorting by age...");
Collections.sort(al,new AgeComparator());
Iterator itr2=al.iterator();
while(itr2.hasNext()){
Student st=(Student)itr2.next();
System.out.println(st.rollno+" "+st.name+" "+st.age);
}}}

Core Java
Comparator vs Comparable :
Parameter

Comparator
Sorting logic is in separate
Sorting logic must be in
class. Hence we can write
same class whose objects
different sorting based on
are being sorted. Hence
Sorting logic
different attributes of objects
this is called natural
to be sorted. E.g. Sorting using
ordering of objects
id, name etc.
Class whose objects to be sorted
Class whose objects to be do not need to implement this
sorted must implement thisinterface. Some other class can
interface. e.g Country
implement this interface.
Implementation class needs to implement E.g.-CountrySortByIdComparator
comparable to collection class can implement Comparator
of country object by id
interface to sort collection of
country object by id
int compareTo(Object o1)
This method compares this
object with o1 object and int compare(Object o1,Object o2)
returns a integer. Its
This method compares o1 and o2
value has following
objects. and returns a integer.
meaning
Its value has following meaning.
1.
positive

this
object
1. positive o1 is greater than
Sorting method
is greater than o1
o2
2. zero this object
2. zero o1 equals to o2
equals to o1
3. negative o1 is less than o1
3. negative this object
is less than o1
Collections.sort(List)
Collections.sort(List, Comparator)
Here objects will be
Here objects will be sorted on
Calling method
sorted on the basis of
the basis of Compare method in
CompareTo method
Comparator
Package
Sorting
Sequence

Comparable

Java.lang.Comparable
Accepts Single Sorting
Sequence

Java.util.Comparator
Accepts Multiple Sorting Sequence

Example,
package org.arpit.javapostsforlearning;
//If this.cuntryId < country.countryId:then compare method will return -1
//If this.countryId > country.countryId:then compare method will return 1
//If this.countryId==country.countryId:then compare method will return 0
public class Country implements Comparable{
int countryId;
String countryName;
public Country(int countryId, String countryName) {
super();
this.countryId = countryId;
this.countryName = countryName;
}
@Override

Core Java
public int compareTo(Object arg0) {
Country country=(Country) arg0;
return (this.countryId < country.countryId ) ? -1:
(this.countryId > country.countryId ) ? 1:0 ;
}
public int getCountryId() {
return countryId;
}
public void setCountryId(int countryId) {
this.countryId = countryId;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
}
ComparableMain :
package org.arpit.javapostsforlearning;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ComparableMain {
/**
* @author Arpit Mandliya
*/
public static void main(String[] args) {
Country indiaCountry=new Country(1, 'India');
Country chinaCountry=new Country(4, 'China');
Country nepalCountry=new Country(3, 'Nepal');
Country bhutanCountry=new Country(2, 'Bhutan');
List<Country> listOfCountries = new ArrayList<Country>();
listOfCountries.add(indiaCountry);
listOfCountries.add(chinaCountry);
listOfCountries.add(nepalCountry);
listOfCountries.add(bhutanCountry);
System.out.println('Before Sort : ');
for (int i = 0; i < listOfCountries.size(); i++) {
Country country=(Country)
listOfCountries.get(i);
System.out.println('Country Id:
'+country.getCountryId()+'||'+'Country name: '+country.getCountryName());
}
Collections.sort(listOfCountries);

Core Java
System.out.println('After Sort : ');
for (int i = 0; i < listOfCountries.size(); i++) {
Country country=(Country)
listOfCountries.get(i);
System.out.println('Country Id:
'+country.getCountryId()+'|| '+'Country name:
'+country.getCountryName());
}
}
}
Before Sort
Country Id:
Country Id:
Country Id:
Country Id:
After Sort
Country Id:
Country Id:
Country Id:
Country Id:

:
1||Country name: India
4||Country name: China
3||Country name: Nepal
2||Country name: Bhutan
:
1|| Country name: India
2|| Country name: Bhutan
3|| Country name: Nepal
4|| Country name: China

Comparator Example,
package org.arpit.javapostsforlearning;
public class Country{
int countryId;
String countryName;
public Country(int countryId, String countryName) {
super();
this.countryId = countryId;
this.countryName = countryName;
}
public int getCountryId() {
return countryId;
}
public void setCountryId(int countryId) {
this.countryId = countryId;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
}

Core Java
package org.arpit.javapostsforlearning;
import java.util.Comparator;
//If country1.getCountryId()<country2.getCountryId():then compare method
will return -1
//If country1.getCountryId()>country2.getCountryId():then compare method
will return 1
//If country1.getCountryId()==country2.getCountryId():then compare method
will return 0
public class CountrySortByIdComparator implements Comparator<Country>{
@Override
public int compare(Country country1, Country country2) {
return (country1.getCountryId() < country2.getCountryId() ) ? -1:
(country1.getCountryId() > country2.getCountryId() ) ? 1:0 ;
}
}
package org.arpit.javapostsforlearning;
import
import
import
import

java.util.ArrayList;
java.util.Collections;
java.util.Comparator;
java.util.List;

public class ComparatorMain {


/**
* @author Arpit Mandliya
*/
public static void main(String[] args) {
Country indiaCountry=new Country(1, 'India');
Country chinaCountry=new Country(4, 'China');
Country nepalCountry=new Country(3, 'Nepal');
Country bhutanCountry=new Country(2, 'Bhutan');
List<Country> listOfCountries = new ArrayList<Country>();
listOfCountries.add(indiaCountry);
listOfCountries.add(chinaCountry);
listOfCountries.add(nepalCountry);
listOfCountries.add(bhutanCountry);
System.out.println('Before Sort by id : ');
for (int i = 0; i < listOfCountries.size(); i++) {
Country country=(Country)
listOfCountries.get(i);
System.out.println('Country Id:
'+country.getCountryId()+'||'+'Country name: '+country.getCountryName());
}
Collections.sort(listOfCountries,new
CountrySortByIdComparator());
System.out.println('After Sort by id: ');
for (int i = 0; i < listOfCountries.size(); i++) {

Core Java
Country country=(Country)
listOfCountries.get(i);

System.out.println('Country Id:
'+country.getCountryId()+'|| '+'Country name:
'+country.getCountryName());
}
//Sort by countryName
Collections.sort(listOfCountries,new
Comparator<Country>() {
@Override
public int compare(Country o1, Country
o2) {

return
o1.getCountryName().compareTo(o2.getCountryName());
}
});
System.out.println('After Sort by name: ');
for (int i = 0; i < listOfCountries.size(); i++) {
Country country=(Country)
listOfCountries.get(i);
System.out.println('Country Id:
'+country.getCountryId()+'|| '+'Country name:
'+country.getCountryName());
}
}
}
Before Sort by id :
Country Id: 1||Country name: India
Country Id: 4||Country name: China
Country Id: 3||Country name: Nepal
Country Id: 2||Country name: Bhutan
After Sort by id:
Country Id: 1|| Country name: India
Country Id: 2|| Country name: Bhutan
Country Id: 3|| Country name: Nepal
Country Id: 4|| Country name: China
After Sort by name:
Country Id: 2|| Country name: Bhutan
Country Id: 4|| Country name: China
Country Id: 1|| Country name: India
Country Id: 3|| Country name: Nepal

Core Java
Map :

A map contains values based on the key i.e. key and value pair.

Each pair is known as an entry.

Map contains only unique elements.

The Map interface is not an extension of Collection interface.

Instead the interface starts of its own interface hierarchy, for


maintaining key-value associations.

The interface describes a mapping from keys to values, without


duplicate keys

The Map interface provides three (3) collection views, which allow a
map's contents to be viewed as a set of keys, collection of values, or set of
key-value mappings.
keySet set of keys
values collection of values
entrySet set of key-value mappings

Three general-purpose Map implementations: HashMap, TreeMap, and


LinkedHashMap.

Methods :
1. public Object put(object key,Object value)
2. public void putAll(Map map)
3. public Object remove(object key)
4. public Object get(Object key)
5. public boolean containsKey(Object key)
6. public boolean containsValue(Object value)
7. public Set keySet()
8. public Set entrySet()

Entry :

Entry is the subinterface of Map. So we will access it by Map.Entry name.

It provides methods to get key and value.

Methods :
1. public Object getKey()
2. public Object getValue()

Core Java
Why use Map.Entry .?
If you just need keys, use keySet(). If you just need values, use values().
If you're going to use keys and values in your subsequent code, then
you're best off using entrySet().
I frequently see people do this without entrySet(), and it usually looks
something like this:
1.for
2.
3.
4.
5.}

(Iterator
Foo key =
Bar value
// now do

it = map.keySet().iterator(); it.hasNext(); ) {
(Foo) it.next();
= (Bar) map.get(key);
something with key and value

This works, but it's making the JVM do extra work for no good reason. Every time you call
get() you're making the JVM spend time doing a hashcode lookup, or navigating a tree and
evaluating a comparator. These operations may be fast, or not, but why do
them if you don't have to? A Map.Entry gives you both key and value, together, in the
most efficient manner possible.
1.for
2.
3.
4.
5.
6.}

(Iterator
Map.Entry
Foo key =
Bar value
// now do

it = map.entrySet().iterator(); it.hasNext(); ) {
e = (Map.Entry) it.next();
(Foo) e.getKey();
= (Bar) e.getValue();
something with key and value

Under JDK 5 and later it's a little nicer:


1.for
2.
3.
4.
5.}

(Map.Entry<Foo,Bar> e : map.entrySet()) {
Foo key = e.getKey();
Bar value = e.getValue();
// now do something with key and value

The SortedMap interface (with the implementation TreeMap) should be your


friend.
The interface has the methods:
keySet() which returns a set of the keys in ascending order
values() which returns a collection of all values in the ascending order
of the corresponding keys
However, the keys must have a meaningful order. Otherwise you can used
the LinkedHashMap where the order is determined by the insertion order.
Example,
public static void main(String[] args) {
Map<Integer, String> hashMap = new HashMap<Integer, String>();
hashMap.put(10, null);

Core Java
hashMap.put(2, "First");
hashMap.put(1, "First");
hashMap.put(null, null);
hashMap.put(null, "Second");
hashMap.put(3, null);
for (Integer key : hashMap.keySet()) {
System.out.println( key + " " + hashMap.get(key));
}
}

Output:
null Second
1 First
2 First
3 null
10 null

HashMap :

A HashMap contains values based on the key. It implements the Map


interface and extends AbstractMap class.

It contains only unique elements.

It may have one null key and multiple null values.

It maintains no order.

Hierarchy of HashMap Class :


class Simple{
public static void main(String args[]){
HashMap hm=new HashMap();
hm.put(100,"Amit");
hm.put(101,"Vijay");
hm.put(102,"Rahul");
Set set=hm.entrySet();
Iterator itr=set.iterator();
while(itr.hasNext()){
Map.Entry m=(Map.Entry)itr.next();
System.out.println(m.getKey()+" "+m.getValue());
}
}

Core Java
}
The HashMap class uses a hashtable to implement the Map interface. This
allows the execution time of basic operations, such as get( ) and put( ),
to remain constant even for large sets.
The HashMap class supports four constructors. The first form constructs a
default hash map:
HashMap( )
The second form initializes the hash map by using the elements of m:
HashMap(Map m)
The third form initializes the capacity of the hash map to capacity:
HashMap(int capacity)
The fourth form initializes both the capacity and fill ratio of the hash
map by using its arguments:
HashMap(int capacity, float fillRatio)

LinkedHashMap :

A LinkedHashMap contains values based on the key. It implements the


Map interface and extends HashMap class.

It contains only unique elements.

It may have one null key and multiple null values.

It is same as HashMap instead maintains insertion order.

Core Java
Tree Map :

A TreeMap contains values based on the key. It implements the


NavigableMap interface and extends AbstractMap class.

It contains only unique elements.

It cannot have null key but can have multiple null values.

It is same as HashMap instead maintains ascending order.

Example,
public static void main(String[] args) {
// Create a TreeMap and populate it with elements
TreeMap treeMap = new TreeMap();
treeMap.put("key_1","element_1");
treeMap.put("key_2","element_2");
treeMap.put("key_3","element_3");
// Get a set of all the entries (key - value pairs) contained in the TreeMap
Collection entrySet = treeMap.entrySet();
// Obtain an Iterator for the entries Set
Iterator it = entrySet.iterator();
// Iterate through TreeMap entries
System.out.println("TreeMap entries : ");
while(it.hasNext())
System.out.println(it.next());
}
TreeMap entries :
key_1=element_1
key_2=element_2
key_3=element_3

Core Java
HashTable :

A HashTable is an Array of list.

Each list is known as a bucket. The position of the bucket is


identified by calling hashcode() method.

A Hashtable contains values based on the key.

It implements the Map interface and extends Dictionary class.

It contains only unique elements

It may not have any null key or value

It is synchronized

Syntax :
public class Hashtable<K,V> extends Dictionary<K,V> implements Map<K,V>,
Cloneable, Serializable
To successfully store and retrieve objects from a hashtable, the objects
used as keys must implement the hashCode method and the equals method.
This example creates a hashtable of numbers. It uses the names of the
numbers as keys:
Hashtable<String, Integer> numbers = new Hashtable<String, Integer>();
numbers.put("one", 1);
numbers.put("two", 2);
numbers.put("three", 3);
To retrieve a number, use the following code:
Integer n = numbers.get("two");
if (n != null) {
System.out.println("two = " + n);
}

HashMap vs HashTable :
HashMap

HashTable

It is not synchronized

It is synchronized

Can contain one null key and


multiple null values

Can contain neither null key nor


null values

Core Java
HashMap vs. TreeMap vs. Hashtable vs. LinkedHashMap :
There are 4 commonly used implementations of Map in Java SE
1) HashMap,
2) TreeMap,
3) Hashtable and
4) LinkedHashMap.

HashMap is implemented as a hash table, and there is no ordering on


keys or values.

TreeMap is implemented based on red-black tree structure, and it is


ordered by the key.

LinkedHashMap preserves the insertion order

Hashtable is synchronized, in contrast to HashMap.

HashMap :
If key of the HashMap is self-defined objects, then equals() and
hashCode() contract need to be followed.
class TestDog {
String color;
TestDog(String c) {
color = c;
}
public String toString() {
return color + " dog";

Core Java
}
}
public class HashMapDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap<TestDogSample, Integer> hashMap = new
HashMap<TestDogSample, Integer>();
TestDogSample d1 = new TestDogSample("red");
TestDogSample d2 = new TestDogSample("black");
TestDogSample d3 = new TestDogSample("white");
TestDogSample d4 = new TestDogSample("white");
hashMap.put(d1,
hashMap.put(d2,
hashMap.put(d3,
hashMap.put(d4,

10);
15);
5); // HashMap accepts duplicates key here
20);

// print size
System.out.println(hashMap.size());
// loop HashMap
for (Entry<TestDogSample, Integer> entry : hashMap.entrySet())

System.out.println(entry.getKey().toString() + " - "


+ entry.getValue());
}

}
Output :
4
white dog
black dog
red dog
white dog

5
15
10
20

Note here, we add white dogs twice by mistake, but the HashMap takes
it. This does not make sense, because now we are confused how many white
dogs are really there.
The Dog class should be defined as follows:
class TestDogSample {
String color;
TestDogSample(String c) {
color = c;

Core Java
}
public String toString() {
return color + " dog";
}
// Override those two methods hashCode() equals() from Object Class
public int hashCode() {
return color.length();
}
public boolean equals(Object obj) {
return this.color == ((TestDogSample)obj).color;
}
}
public class TestHashMap {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap<TestDogSample, Integer> hashMap = new
HashMap<TestDogSample, Integer>();
TestDogSample d1 = new TestDogSample("red");
TestDogSample d2 = new TestDogSample("black");
TestDogSample d3 = new TestDogSample("white");
TestDogSample d4 = new TestDogSample("white");
hashMap.put(d1,
hashMap.put(d2,
hashMap.put(d3,
duplicates key here
hashMap.put(d4,

10);
15);
5);

// HashMap does not accept

20);

// print size
System.out.println(hashMap.size());
// loop HashMap
for (Entry<TestDogSample, Integer> entry : hashMap.entrySet())
{
}

System.out.println(entry.getKey().toString() + " - "


+ entry.getValue());

}
}
Now the output is:
3
red dog 10

Core Java
white dog 20
black dog 15
The reason is that HashMap doesnt allow two identical elements. By
default, the hashCode() and equals() methods implemented in Object class are
used. The default hashCode() method gives distinct integers for distinct objects,
and the equals() method only returns true when two references refer to the same
object

HashMap vs TreeMap :
HashMap

TreeMap

Lookup-array structure, based on

Tree structure, based on compareTo()

hashCode(), equals()

implementation, O(log(N)) runtime

implementations, O(1) runtime

complexity for inserting and

complexity for inserting and

searching, sorted

searching, unsorted
Does not support duplicate key

Does not support duplicate key

Its not synchronized, To make it


synchronized we have to explicitly
call
Collections.synchronizedMap(mapName
)

Its not synchronized, To make it


synchronized we have to explicitly
call
Collections.synchronizedMap(mapName
)

HashMap allows null as both keys


and values,

Does not contain null key but


contains multiple null values

contain 1 null key and multiple


null values
Its fast compared to treemap

Its slow compared to hashmap

It implements Map interface

It implements one more interface


called NavigableMap

Map implements Hashmap

Unordered map

Map extends SortedMap extends


NavigableMap Implements TreeMap
Ordered map. TreeMap order can be
customized using Comparator and
Comparable interfaces.

Core Java
What is fail-fast?
The concept of fail-fast comes with iterators. When an Iterator object is
created and iteration is going on, the HashMap elements cannot be
modified (like addition or deletion of elements cannot be done). This is
explained programmatically in ConcurrentModificationException.

About Hashing and Hashcode


HashCode

Comparing two strings letter by letter in a for loop is a time taking process.

To make faster, the JVM converts each string into an integer number called hashcode.

Different strings with different sequence of characters have different hashcodes.

Comparison with integer numbers gives maximum performance.

Usage of hashcode numbers for comparison, searching of duplicate elements and identification is
faster.

Hashing

Hashing is process of converting a string or object into a 32-bit integer number.

Two objects are said to be equal if their hashcodes are same.

hashCode() is used in combination of equals() method.

When compared, hashing is done automatically by the JVM.

Hashing, in data structures, is done implicitly in the basic operations with add(), contains(),
remove() and size() etc.

Hashing is more useful to compare the sets of large content.

HashMap vs HashSet:
Parameter
Interface

Method for storing data

Duplicates
Performance

HashMap
This is core difference
among them. HashMap
implements Map
interface
It stores data in a
form of key->value
pair. So it uses
put(key,value) method
for storing data
HashMap allows
duplicate value but not
duplicate keys
It is faster than

HashSet
HashSet implement Set
interface
It uses add(value) method
for storing data

HashSet does not allow


duplicate values.
It is slower than HashMap

Core Java
HashCode Calculation

Value Inertion
null

hashset as values are


stored with unique keys
In hash map hashcode
In this,hashcode is
value is calculated
calculated on the basis of
using key object
value object. Hashcode can
be same for two value
object so we have to
implement equals() method.
If equals() method return
false then two objects are
different.
use put() method to
Use add() method to put
insert key and value
elements into Set.
into HashMap in Java.
HashMap can allow one
HashSet allows only one
null key + multiple
null key
null values

Similarities :
1) Both HashMap and HashSet are hash based collection in Java.
2) Both HashMap and HashSet are not synchronized and can not be shared
between multiple threads.
3) Iterator returned by HashMap's keySet() and HashSet are fail-fast and
they throwConcurrentModificationException if they detect any structural change in
Collection.
4) Both HashMap and HashSet provided constant time performance for basic
operations like put(), get() etc.
5) Both HashSet and HashMap allows null values.
hashCode() vs equals() .?
The contract between hashCode and equals method,
if two objects are equal, that is obj1.equals(obj2) is true then, obj1.hashCode() and
obj2.hashCode() must return same integer.
Why do we need to override equals() and hashcode() .?
1) If 2 objects are same then they must return same value in hashcode()
and equals() method whenever invoked.
2) It is not necessary that 2 different objects must have different
hashcode values. It might be possible that they share common hash buckets
for example,
object1 amy
object2 may
both has different hashcode but may possible to share same hash buckets

Core Java

obj2
obj1

3) JVM assigns unique hashcode values to each object when they are
created in memory and if developers dont override the hashcode method
then there is no way the 2 objects returns same hashcode values
Example,
class employeetest {
private String name;
private int id;
public employeetest(String empname, Integer empid) {
this.name = empname;
this.id = empid;
}
public String toString() {
return name + " " + id;
}
/*public int hashCode() {
return this.name.length();
}*/
public boolean equals(Object obj) {
return this.name == ((employeetest)obj).name;
}
}
public class HashSetDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HashSet<employeetest> set = new HashSet<employeetest>();
set.add(new employeetest("Praveen", 12));
set.add(new employeetest("Praveen", 12));
for (employeetest empObj : set) {
System.out.println(empObj.toString());

Core Java
}
}
}
output :
Praveen 12
Praveen 12
Note: The same code has equals() method as commented and hashcode()
method as uncommented will get the same result like above
If both methods overrides here you will get the exact output and
duplicates are not allowed in collection here
class employeetest {
private String name;
private int id;
public employeetest(String empname, Integer empid) {
this.name = empname;
this.id = empid;
}
public String toString() {
return name + " " + id;
}
public int hashCode() {
return this.name.length();
}

public boolean equals(Object obj) {


return this.name == ((employeetest)obj).name;
}

public class HashSetDemo {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HashSet<employeetest> set = new HashSet<employeetest>();
set.add(new employeetest("Praveen", 12));
set.add(new employeetest("Praveen", 12));
for (employeetest empObj : set) {
System.out.println(empObj.toString());
}
}
}

Core Java
output :
Praveen 12
HashTable, HashMap and HashSet are the Collection classes in java.util
package that make use of hashing algorithm to store objects.
In all these Collection classes except HashSet, objects are stored as
key-value pairs.
For the storage and the retrieval of any user-defined objects it is a
good practice to override the following methods which is mentioned below,

hashCode()
equals()

These methods are available in the Object class and hence available to
all java classes. Using these two methods, an object can be stored or
retrieved from a Hashtable, HashMap or HashSet.
hashCode() method

This method returns a hashcode value as an int for the object.

Default implementation for hashcode() should be overridden in order


to make searching of data faster.

The implementation of hashCode() method for an user-defined object


should be calculated based on the properties of the class which we
wish to consider.

equals() method

This method returns a boolean which specifies whether two objects


are equal or not.

The default implementation of equals() method given by the Object


Class uses the == operator to compare two object references, and
returns true only if they refer to the same object.

But, we can meaningfully re-define this equals() method to have en


equality check based on our own criteria.

Core Java
Design Patterns :

A design pattern is a well-proved solution for solving the specific


problem/task.

Design patterns are programming language independent strategies for solving the
common object-oriented design problems.

A design pattern represents an idea, not a particular


implementation.

By using the design patterns you can make your code more flexible,
reusable and maintainable.

It is the most important part because JAVA internally follows


design patterns.

Understanding the problem without using Design Patterns :


Problem Given:
Suppose you want to create a class for which only a single instance (or
object) should be created and that single object can be used by all other
classes.
Solution :
Singleton design pattern is the best solution of above specific problem. So,
every design pattern has some specification or set of rules for solving the
problems.

Advantage of design pattern

They are reusable in multiple projects.

They provide the solutions that help to define the system


architecture.

They capture the software engineering experiences.

They provide transparency to the design of an application.

They are well-proved and testified solutions since they have been
built upon the knowledge and experience of expert software
developers.

Design patterns don't guarantee an absolute solution to a problem.


They provide clarity to the system architecture and the possibility
of building a better system.

When should we use the design patterns?


We must use the design patterns during the analysis and requirement phase of
SDLC (Software Development Life Cycle).

Core Java
Types of design patterns :
Basically, design patterns are categorized into two parts:
1) Core java (or JavaSE) Design Patterns.

Creational Design Pattern

Structural Design Pattern

Behavioral Design Pattern

2) JavaEE (J2EE) Design Patterns.

Presentation Layer Design Pattern

Business Layer Design Pattern

Data Layer Design Pattern

Core Java Design Patterns


In core java, there are mainly three types of design patterns, which are
further divided into their sub-parts:
Creational Design Pattern

Structural Design Pattern

Behavioral Design Pattern

Factory Pattern

Adapter Pattern

Chain Of Responsibility
Pattern

Abstract Factory
Pattern

Bridge Pattern

Command Pattern

Singleton Pattern

Composite Pattern

Interpreter Pattern

Prototype Pattern

Decorator Pattern

Iterator Pattern

Builder Pattern.

Facade Pattern

Mediator Pattern

Flyweight Pattern

Memento Pattern

Proxy Pattern

Observer Pattern
State Pattern
Strategy Pattern
Template Pattern
Visitor Pattern

Core Java
Creational design patterns :

Creational design patterns are concerned with the way of creating


objects.

These design patterns are used when a decision must be made at the
time of instantiation of a class (i.e. creating an object of a
class).

But everyone knows an object is created by using new keyword in java.


For example,
Employee emp = new Employee();
Hard-Coded code is not the good programming approach. Here, we are
creating the instance by using the new keyword. Sometimes, the nature of
the object must be changed according to the nature of the program. In
such cases, we must get the help of creational design patterns to provide
more general and flexible approach.

Factory Method Pattern :

A Factory Pattern or Factory Method Pattern says that just define


an interface or abstract class for creating an object but let the
subclasses decide which class to instantiate.

In other words, subclasses are responsible to create the instance


of the class. The Factory Method Pattern is also known as Virtual
Constructor.

Advantage of Factory Design Pattern :

Factory Method Pattern allows the sub-classes to choose the type of


objects to create.

Usage of Factory Design Pattern :

When a class doesn't know what sub-classes will be required to


create

When a class wants that its sub-classes specify the objects to be


created.

When the parent classes choose the creation of objects to its


sub-classes.

Core Java
UML for Factory Method Pattern :
We are going to create a Plan abstract class and concrete classes that
extends the Plan abstract class. A factory class GetPlanFactory is
defined as a next step.
GenerateBill class will use GetPlanFactory to get a Plan object. It will
pass information (DOMESTICPLAN / COMMERCIALPLAN / INSTITUTIONALPLAN) to
GetPalnFactory to get the type of object it needs.

Example Code ,
abstract class Plan{
protected double rate;
abstract void getRate();
public void calculateBill(int units){
System.out.println(units*rate);
}
}//end of Plan class
class

DomesticPlan extends Plan{


//@override

Core Java
public void getRate(){
rate=3.50;
}
}//end of DomesticPlan class.
class

CommercialPlan extends Plan{


//@override
public void getRate(){
rate=7.50;
}

}//end of CommercialPlan class.


class

InstitutionalPlan extends Plan{


//@override
public void getRate(){
rate=5.50;
}

}//end of InstitutionalPlan class.


Factory Pattern Class Applies here :
class GetPlanFactory{
//use getPlan method to get object of type Plan
public Plan getPlan(String planType){
if(planType == null){
return null;
}
if(planType.equalsIgnoreCase("DOMESTICPLAN")) {
return new DomesticPlan();
}
else if(planType.equalsIgnoreCase("COMMERCIALPLAN")){
return new CommercialPlan();
}
else if(planType.equalsIgnoreCase("INSTITUTIONALPLAN")) {
return new InstitutionalPlan();
}

Core Java
return null;
}
}//end of GetPlanFactory class.
Implementation Class :
class GenerateBill{
public static void main(String args[])throws IOException{
GetPlanFactory planFactory = new GetPlanFactory();
System.out.print("Enter the name of plan for which the bill will be
generated: ");
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
String planName=br.readLine();
System.out.print("Enter the number of units for bill will be
calculated: ");
int units=Integer.parseInt(br.readLine());
Plan p = planFactory.getPlan(planName);
//call getRate() method and calculateBill()method of DomesticPaln.
System.out.print("Bill amount for "+planName+" of

"+units+" units

is: ");
p.getRate();
p.calculateBill(units);
}
}//end of GenerateBill class.

Singleton design pattern :

Singleton Pattern says that just "define a class that has only one instance
and provides a global point of access to it".

In other words, a class must ensure that only single instance


should be created and single object can be used by all other
classes.

There are two forms of singleton design pattern,

Early Instantiation : creation of instance at load time.

Core Java

Lazy Instantiation : creation of instance when required.

Advantage of Singleton design pattern :

Saves memory because object is not created at each request. Only single
instance is reused again and again.

Usage of Singleton design pattern :

Singleton pattern is mostly used in multi-threaded and database


applications. It is used in logging, caching, thread pools,
configuration settings etc.

Uml of Singleton design pattern :

How to create Singleton design pattern ?


To create the singleton class, we need to have static member of class,
private constructor and static factory method.

Private Static member: It gets memory only once because of static, it contains the
instance of the Singleton class.

Private constructor: It will prevent to instantiate the Singleton class from outside
the class.

Public Static factory method: This provides the global point of access
to the Singleton object and returns the instance to the caller.

Core Java

Understanding early Instantiation of Singleton Pattern :


In such case, we create the instance of the class at the time of
declaring the static data member, so instance of the class is created at
the time of class loading.
Let's see the example of singleton design pattern using early
instantiation.
class A{
private static A obj = new A();

//Early, instance will be created at


load time

private A(){}
public static A getA(){
return obj;
}
public void doSomething(){
//write your code
}
}

Understanding lazy Instantiation of Singleton Pattern :


In such case, we create the instance of the class in synchronized method
or synchronized block, so instance of the class is created when required.
Let's see the simple example of singleton design pattern using lazy
instantiation.
class A{
private static A obj;
private A(){}
public static A getA(){
if (obj == null){
synchronized(A.class){
if (obj == null){
obj = new Singleton();

//instance will be created at

Core Java
request time
}
}
}
return obj;
}
public void doSomething(){
//write your code
}
}

Significance of Classloader in Singleton Pattern :

If singleton class is loaded by two classloaders, two instance of


singleton class will be created, one for each classloader.

Significance of Serialization in Singleton Pattern :

If singleton class is Serializable, you can serialize the singleton instance. Once it is
serialized, you can deserialize it but it will not return the singleton object.

To resolve this issue, you need to override the readResolve() method


that enforces the singleton. It is called just after the object is deserialized.
It returns the singleton object.

public class A implements Serializable {


//your code of singleton
protected Object readResolve() {
return getA();
}
}

Understanding Real Example of Singleton Pattern :


We are going to create a JDBCSingleton class. This JDBCSingleton class
contains its constructor as private and a private static instance jdbc of
itself.
JDBCSingleton class provides a static method to get its static instance
to the outside world. Now, JDBCSingletonDemo class will use
JDBCSingleton class to get the JDBCSingleton object.

Core Java

Example Singleton class ,


class JDBCSingleton {
//Step 1
// create a JDBCSingleton class.
//static member holds only one instance
of the JDBCSingleton class.
private static JDBCSingleton jdbc;
//JDBCSingleton prevents the
instantiation from any other class.
private JDBCSingleton() {

//Now we are providing gloabal point of


access.
public static JDBCSingleton getInstance() {
if (jdbc==null)
{
jdbc=new

JDBCSingleton();

}
return jdbc;
}
// to get the connection from methods like insert, view etc.
private static Connection getConnection()throws
ClassNotFoundException, SQLException
{
Connection con=null;
Class.forName("com.mysql.jdbc.Driver");
con=
DriverManager.getConnection("jdbc:mysql://localhost:3306/ashwanirajput",
"root", "ashwani");
return con;
}

Core Java
//to insert the record into the database
public int insert(String name, String pass) throws SQLException
{
Connection c=null;
PreparedStatement ps=null;
int recordCounter=0;
try {
c=this.getConnection();
ps=c.prepareStatement("insert into
userdata(uname,upassword)values(?,?)");
ps.setString(1, name);
ps.setString(2, pass);
recordCounter=ps.executeUpdate();
} catch (Exception e) { e.printStackTrace(); } finally{
if (ps!=null){
ps.close();
}if(c!=null){
c.close();
}
}
return recordCounter;
}
//to view the data from the database
public

void view(String name) throws SQLException

{
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {

Core Java
con=this.getConnection();
ps=con.prepareStatement("select * from userdata

where uname=?");

ps.setString(1, name);
rs=ps.executeQuery();
while (rs.next()) {
System.out.println("Name=
"+rs.getString(2)+"\t"+"Paasword= "+rs.getString(3));
}
} catch (Exception e) { System.out.println(e);}
finally{
if(rs!=null){
rs.close();
}if (ps!=null){
ps.close();
}if(con!=null){
con.close();
}
}
}
// to update the password for the given username
public int update(String name, String password) throws SQLException
{
Connection c=null;
PreparedStatement ps=null;
int recordCounter=0;
try {
c=this.getConnection();
ps=c.prepareStatement(" update userdata set
upassword=? where uname='"+name+"' ");
ps.setString(1, password);
recordCounter=ps.executeUpdate();
} catch (Exception e) {
if (ps!=null){

e.printStackTrace(); } finally{

Core Java
ps.close();
}if(c!=null){
c.close();
}
}
return recordCounter;
}
// to delete the data from the database
public int delete(int userid) throws SQLException{
Connection c=null;
PreparedStatement ps=null;
int recordCounter=0;
try {
c=this.getConnection();
uid='"+userid+"' ");

ps=c.prepareStatement(" delete from userdata where


recordCounter=ps.executeUpdate();

} catch (Exception e) { e.printStackTrace(); }


finally{
if (ps!=null){
ps.close();
}if(c!=null){
c.close();
}
}
return recordCounter;
}
}// End of JDBCSingleton class
Implementation Class ,
class JDBCSingletonDemo{
static int count=1;
static int

choice;

public static void main(String[] args) throws IOException {


JDBCSingleton jdbc= JDBCSingleton.getInstance();

Core Java
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
do{
System.out.println("DATABASE OPERATIONS");
System.out.println(" --------------------- ");
System.out.println(" 1. Insertion ");
System.out.println(" 2. View

");

System.out.println(" 3. Delete

");

System.out.println(" 4. Update

");

System.out.println(" 5. Exit

");

System.out.print("\n");
System.out.print("Please enter the choice what you want to
perform in the database: ");
choice=Integer.parseInt(br.readLine());
switch(choice) {
case 1:{
System.out.print("Enter the username you want to
insert data into the database: ");
String username=br.readLine();
System.out.print("Enter the password you want to
insert data into the database: ");
String password=br.readLine();
try {
int i= jdbc.insert(username, password);
if (i>0) {
System.out.println((count++) + " Data has
been inserted successfully");
}else{
System.out.println("Data has not been

inserted ");
}

} catch (Exception e) {
System.out.println(e);
}

Core Java
System.out.println("Press Enter key to

continue...");

System.in.read();
}//End of case 1
break;
case 2:{
System.out.print("Enter the username : ");
String username=br.readLine();
try

{
jdbc.view(username);
} catch (Exception e) {
System.out.println(e);
}

System.out.println("Press Enter key to


continue...");
System.in.read();
}//End of case 2
break;
case 3:{
delete: ");

System.out.print("Enter the userid,

you want to

int userid=Integer.parseInt(br.readLine());
try {
int i= jdbc.delete(userid);
if (i>0) {
System.out.println((count++) + " Data has
been deleted successfully");
}else{
System.out.println("Data has not been

deleted");
}

} catch (Exception e) {
System.out.println(e);

Core Java
}
System.out.println("Press Enter key to

continue...");

System.in.read();
}//End of case 3
break;
case 4:{
System.out.print("Enter the username,

you want to

update: ");
String username=br.readLine();
System.out.print("Enter the new password ");
String password=br.readLine();
try {
int i= jdbc.update(username, password);
if (i>0) {
been updated successfully");

System.out.println((count++) + " Data has


}

} catch (Exception e) {
System.out.println(e);
}
System.out.println("Press Enter key to
continue...");
System.in.read();
}// end of case 4
break;
default:
return;
}
} while (choice!=4);
}
}

Core Java
Lazy initialization :
This method uses double-checked locking and it uses synchronized block
here.
public class SingletonDemo {
private static volatile SingletonDemo instance = null;
private SingletonDemo() {

public static SingletonDemo getInstance() {


if (instance == null) {
synchronized (SingletonDemo .class){
// Double checking here
if (instance == null) {
instance = new SingletonDemo ();
}
}
}
return instance;
}
}

An alternate way , and it uses synchronized method here


public class SingletonDemo {
private static SingletonDemo instance = null;
private SingletonDemo() {

public static synchronized SingletonDemo getInstance() {


if (instance == null) {
instance = new SingletonDemo ();
}
return instance;
}

Eager initialization (or) Early initialization :


If the program will always need an instance, or if the cost of creating
the instance is not too large in terms of time/resources, the programmer
can switch to eager initialization, which always creates an instance:
public class Singleton {
private static final Singleton INSTANCE = new Singleton();
private Singleton() {}
public static Singleton getInstance() {
return INSTANCE;

Core Java
}
}
This method has a number of advantages:

The instance is not constructed until the class is used.

There is no need to synchronize the getInstance() method, meaning


all threads will see the same instance and no (expensive) locking is
required.

The final keyword means that the instance cannot be redefined,


ensuring that one (and only one) instance ever exists.

Static block initialization :


public class Singleton {
private static final Singleton instance;
static {
try {
instance = new Singleton();
} catch (IOException e) {
throw new RuntimeException("Darn, an error occurred!", e);
}
}
public static Singleton getInstance() {
return instance;
}

private Singleton() {
// ...
}

Singleton design pattern in JAVA : ( Best Approach here )


Sections in this post:

Eager initialization
Lazy initialization
Static block initialization
Bill pugh solution
Using Enum
Adding readResolve()
Adding serial version id
Conclusion

Core Java
Eager initialization
This is a design pattern where an instance of a class is created much
before it is actually required. Mostly it is done on system start up. In
singleton pattern, it refers to create the singleton instance
irrespective of whether any other class actually asked for its instance
or not.
public class EagerSingleton {
private static volatile EagerSingleton instance = new
EagerSingleton();
// private constructor
private EagerSingleton() {
}
public static EagerSingleton getInstance() {
return instance;
}
}
Above method works fine, but has one drawback. Instance is created
irrespective of it is required in runtime or not. If this instance is not
big object and you can live with it being unused, this is best approach.
Lets solve above problem in next method.

Lazy initialization
In computer programming, lazy initialization is the tactic of delaying the
creation of an object, the calculation of a value, or some other
expensive process until the first time it is needed. In singleton pattern, it
restricts the creation of instance until requested first time. Lets see in code:
public final class LazySingleton {
private static volatile LazySingleton instance = null;
// private constructor
private LazySingleton() {
}

public static LazySingleton getInstance() {


if (instance == null) {
synchronized (LazySingleton.class) {
instance = new LazySingleton();
}
}
return instance;
}

Core Java
On first invocation, above method will check if instance is already
created using instance variable. If there is no instance i.e. instance is
null, it will create an instance and will return its reference. If
instance is already created, it will simply return the reference of
instance.
But, this method also has its own drawbacks. Lets see how. Suppose there
are two threads T1 and T2. Both comes to create instance and execute
instance==null, now both threads have identified instance variable to
null thus assume they must create an instance. They sequentially goes to
synchronized block and create the instances. At the end, we have two
instances in our application.
This error can be solved using double-checked locking. This principle tells us
to recheck the instance variable again in synchronized block in given
below way:
public class EagerSingleton {
private static volatile EagerSingleton instance = null;
// private constructor
private EagerSingleton() {
}

public static EagerSingleton getInstance() {


if (instance == null) {
synchronized (EagerSingleton.class) {
// Double check
if (instance == null) {
instance = new EagerSingleton();
}
}
}
return instance;
}

Above code is the correct implementation of singleton pattern.


Please ensure to use volatile keyword with instance variable otherwise
you can run into out of order write error scenario, where reference of
instance is returned before actually the object is constructed i.e. JVM
has only allocated the memory and constructor code is still not executed.
In this case, your other thread, which refer to uninitialized object may
throw null pointer exception and can even crash the whole application.
Volatile Benefits :
That is because double-checked locking without volatile is not thread-safe in
Java.

Core Java
volatile solves one issue that is visibility issue . If you are writing to one variable that is
declared volatile then the value will be visible to other thread immediately. As we all
know we have different level of cache in os L1, L2, L3 and if we write to
a variable in one thread it is not guaranteed to be visible to other, so
if we use volatile it writes to direct memory and is visible to others.
But volatile does not solve the issue of atomicity i.e. int a; a++; is
not safe. AS there are three machine instructions associated to it.
Static block initialization
If you have little idea about class loading sequence, you can connect to
the fact that static blocks are executed during the loading of class and
even before the constructor is called. We can use this feature in our
singleton pattern also like this:
public class StaticBlockSingleton {
private static final StaticBlockSingleton INSTANCE;
static {

try {

INSTANCE = new StaticBlockSingleton();


} catch (Exception e) {
throw new RuntimeException("Uffff, i was not
expecting this!", e);
}
}
public static StaticBlockSingleton getInstance() {
return INSTANCE;
}
private StaticBlockSingleton() {
// ...
}
}
Above code has one drawback. Suppose there are 5 static fields in class
and application code needs to access only 2 or 3, for which instance
creation is not required at all. So, if we use this static
initialization. we will have one instance created though we require it or
not.
Next section will overcome this problem.

Bill pugh solution


Bill pugh was main force behind java memory model changes. His principle
Initialization-on-demand holder idiom also uses static block but in different way.
It suggest to use static inner class.

Core Java
public class BillPughSingleton {
private BillPughSingleton() {
}
private static class LazyHolder {
private static final BillPughSingleton INSTANCE = new
BillPughSingleton();
}

public static BillPughSingleton getInstance() {


return LazyHolder.INSTANCE;
}

As you can see, until we need an instance, the LazyHolder class will not
be initialized until required and you can still use other static members
of BillPughSingleton class. This is the solution, i will recommend to
use. I also use it in my all projects.
Using Enum
This type of implementation recommend the use of enum. Enum, as written
in java docs, provide implicit support for thread safety and only one
instance is guaranteed. This is also a good way to have singleton with
minimum effort.
public enum EnumSingleton {
INSTANCE;
public void someMethod(String param) {
// some class member
}
}
Adding readResolve()
So, till now you must have taken your decision that how you would like to
implement your singleton. Now lets see other problems that may arise even
in interviews also.
Lets say your application is distributed and it frequently serialize the
objects in file system, only to read them later when required. Please
note that, de-serialization always creates a new instance. Lets understand using an
example:
Our singleton class is:
public class DemoSingleton implements Serializable {
private volatile static DemoSingleton instance = null;
public static DemoSingleton getInstance() {

Core Java

if (instance == null) {
instance = new DemoSingleton();
}
return instance;

private int i = 10;


public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
}

Lets serialize this class and de-serialize it after making some changes:
public class SerializationTest {
static DemoSingleton instanceOne = DemoSingleton.getInstance();
public static void main(String[] args) {
try {
// Serialize to a file
ObjectOutput out = new ObjectOutputStream(new
FileOutputStream(
"filename.ser"));
out.writeObject(instanceOne);
out.close();
instanceOne.setI(20);
// Serialize to a file
ObjectInput in = new ObjectInputStream(new
FileInputStream(
in.readObject();

"filename.ser"));
DemoSingleton instanceTwo = (DemoSingleton)
in.close();
System.out.println(instanceOne.getI());
System.out.println(instanceTwo.getI());
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

}
Output:
20

Core Java
10
Unfortunately, both variables have different value of variable i.
Clearly, there are two instances of our class. So, again we are in same
problem of multiple instances in application.
To solve this issue, we need to include readResolve() method in our
DemoSingleton class. This method will be invoked when you will
de-serialize the object. Inside this method, you must return the existing
instance to ensure single instance application wide.
public class DemoSingleton implements Serializable {
private volatile static DemoSingleton instance = null;
public static DemoSingleton getInstance() {
if (instance == null) {
instance = new DemoSingleton();
}
return instance;
}
protected Object readResolve() {
return instance;
}
private int i = 10;
public int getI() {
return i;
}

public void setI(int i) {


this.i = i;
}

Now when you execute the class SerializationTest, it will give you
correct output.
20
20
Adding serial version id
So far so good. Till now, we have solved the problem of synchronization
and serialization both. Now, we are just one step behind our correct and
complete implementation. And missing part is serial version id.
This is required in condition when you class structure can change in
between you serialize the instance and go again to de-serialize it.
Changed structure of class will cause JVM to give exception while
de-serializing process.

Core Java
java.io.InvalidClassException: singleton.DemoSingleton; local class
incompatible: stream classdesc serialVersionUID = 5026910492258526905,
local class serialVersionUID = 3597984220566440782
at java.io.ObjectStreamClass.initNonProxy(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at singleton.SerializationTest.main(SerializationTest.java:24)

This problem can be solved only by adding a unique serial version id to


class. It will prevent the compiler to throw the exception by telling
that both classes are same, and will load the available instance
variables only.
Conclusion
After having discussed so many possible approaches and other possible
error cases, i will recommend you below code template to design your
singleton class which shall ensure only one instance of class in whole
application in all above discussed scenarios.
public class DemoSingleton implements Serializable {
private static final long serialVersionUID = 1L;
private DemoSingleton() {
// private constructor
}
private static class DemoSingletonHolder {
public static final DemoSingleton INSTANCE = new DemoSingleton();
}
public static DemoSingleton getInstance() {
return DemoSingletonHolder.INSTANCE;
}
protected Object readResolve() {
return getInstance();
}
}

Core Java
Prototype Design Pattern :

Prototype Pattern says that cloning of an existing object instead of creating


new one and can also be customized as per the requirement.

This pattern should be followed, if the cost of creating a new


object is expensive and resource intensive.

Advantage of Prototype Pattern :

It reduces the need of sub-classing.

It hides complexities of creating objects.

The clients can get new objects without knowing which type of
object it will be.

It lets you add or remove objects at runtime.

Usage of Prototype Pattern :

When the classes are instantiated at runtime.

When the cost of creating an object is expensive or complicated.

When you want to keep the number of classes in an application


minimum.

When the client application needs to be unaware of object creation


and representation.

UML for Prototype Pattern :

Example Prototype Interface ,


public interface Prototype {
public PrototypeDemo getCloneObj();

Core Java
}
class PrototypeDemo implements Prototype {
public Integer empId;
public String empName;
public PrototypeDemo() {
// TODO Auto-generated constructor stub
}
public PrototypeDemo(Integer id, String name) {
this.empId = id;
this.empName = name;
}
public String showInfo() {
return empId + " " + empName;
}

@Override
public PrototypeDemo getCloneObj() {
return new PrototypeDemo(empId, empName);
}

Implementation Class,
public class PrototypeTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PrototypeDemo prototype = new PrototypeDemo(12, "Praveen");
System.out.println(prototype.showInfo());
// CLoning Object here
PrototypeDemo clonePrototypeDemo = prototype.getCloneObj();
System.out.println(clonePrototypeDemo.showInfo());
}
}
Output :

12 Praveen

Real Object Output

12 Praveen

Cloning Object Ref output

Core Java
Marker Interface (Tag Interface) :

Marker Interfaces in Java have special significance because of the


fact that they have no methods declared in them which means that
the classes implementing these interfaces don't have to override
any of the methods.

A few of the marker interfaces already exist in the JDK,


1. java.lang.Cloneable
2. java.io.Serializable
3. java.util.EventListener
4. java.util.RandomAccess
5. java.rmi.Remote

Runnable interface is not marker because Runnable interface has the


public void run() method declared inside it.

Marker interface in Java is interfaces with no field or methods or in


simple word empty interface in java is called marker interface. marker
interface in Java is used to indicate something to compiler, JVM or
any other tool but Annotation is better way of doing same thing.

Marker Interface
In java language programming, interfaces with no methods are known as
marker interfaces. Marker interfaces are Serializable, Clonable,
SingleThreadModel, Event listener. Marker Interfaces are implemented by
the classes or their super classes in order to add some functionality.
Example 1,
Suppose you want to persist (save) the state of an object then you have
to implement the Serializable interface otherwise the compiler will throw
an error. To make more clearly understand the concept of marker interface
you should go through one more example.
Example 2,
Suppose the interface Clonable is neither implemented by a class named
Myclass nor it's any super class, then a call to the method clone() on
Myclass's object will give an error. This means, to add this
functionality one should implement the Clonable interface. While the
Clonable is an empty interface but it provides an important
functionality.

Core Java
How to create our own Marker Interface :
Interfaces with no field or methods, in simple word we can say empty
interface, is called Marker or Tag Interface. Marker interface is used as
a tag to pass a message to java compiler so that it can add special
behavior to the class that implementing it. Clonable and Serializable are
example of Marker Interface. Now lets come to our main topic, how can I
create our own marker interface.
Cheque.java
public interface Cheque {
}
BankDraft.java
public interface BankDraft {
}
Payment.java
public class Payment implements BankDraft{
public void paymentByCheque() {
System.out.println("Payment By Cheque");
}

public void paymentByBankDraft() {


System.out.println("Payment by Draft");
}

MainClass.java
public class MainClass {
public static void main(String[] args) {
Payment p = new Payment();
if (p instanceof Cheque) {
p.paymentByCheque();
}
if (p instanceof BankDraft) {
p.paymentByBankDraft ();
}
}

Output
Payment by Draft

Core Java
Description
In above example, we have created two empty interfaces Cheque and
BankDraft. And Payment class implemented BankDraft interface. In
MainClass class both interface behave as tag, output of MainClass depends
on what interface you have implemented in Payment class.
In above case,
public class Payment implements BankDraft
Thats why output is
Payment by Draft
If you have change as follows
public class Payment implements Cheque
Then new output will be
Payment by Cheque

Java - Serialization

It is a mechanism of writing the state of an object into a byte stream.

Advantage : Its mainly used to travel objects state on the network.

All wrapper classes by default implements Serializable interface.

Java provides a mechanism, called object serialization where an


object can be represented as a sequence of bytes that includes the
object's data as well as information about the object's type and
the types of data stored in the object.

Primary purpose of java serialization is to write an object into a stream, so that it


can be transported through a network and that object can be rebuilt
again.

After a serialized object has been written into a file, it can be


read from the file and deserialized that is, the type information
and bytes that represent the object and its data can be used to
recreate the object in memory.

How do you serialize? When you want to serialize an object, that


respective class should implement the marker interface serializable. It
just informs the compiler that this java class can be serialized. You can tag
properties that should not be serialized as transient. You open a
stream and write the object into it. Java API takes care of the
serialization protocol and persists the java object in a file in
conformance with the protocol. De-serialization is the process of
getting the object back from the file to its original form.
Here protocol means, understanding between serializing person and
de-serializing person.

Core Java

Most impressive is that the entire process is JVM independent, meaning an


object can be serialized on one platform and deserialized on an
entirely different platform.

Classes ObjectInputStream and ObjectOutputStream are high-level


streams that contain the methods for serializing and deserializing
an object.

Advantages

It is easy to use and can be customized.


The serialized stream can be encrypted, authenticated and compressed, supporting the
needs of secure Java computing.

The ObjectOutputStream class contains many write methods for writing


various data types, but one method in particular stands out:
public final void writeObject(Object x) throws IOException
The above method serializes an Object and sends it to the output stream.
Similarly, the ObjectInputStream class contains the following method for
deserializing an object:
public final Object readObject() throws IOException, ClassNotFoundException
This method retrieves the next Object out of the stream and deserializes
it. The return value is Object, so you will need to cast it to its
appropriate data type.
public class Employee implements java.io.Serializable
{
public String name;
public String address;
public transient int SSN;
public int number;
public void mailCheck()
{
System.out.println("Mailing a check to " + name
+ " " + address);
}
}
Notice that for a class to be serialized successfully, two conditions
must be met:

The class must implement the java.io.Serializable interface.

All of the fields in the class must be serializable. If a field is not


serializable, it must be marked transient.

Core Java
Serializing an Object:
The ObjectOutputStream class is used to serialize an Object.
following SerializeDemo program instantiates an Employee object
serializes it to a file.

The
and

public class SerializeDemo


{
public static void main(String [] args)
{
Employee e = new Employee();
e.name = "Reyan Ali";
e.address = "Phokka Kuan, Ambehta Peer";
e.SSN = 11122333;
e.number = 101;
try
{
FileOutputStream fileOut =
new FileOutputStream("/tmp/employee.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(e);
out.close();
fileOut.close();
System.out.println("Serialized data is saved in /tmp/employee.ser");
}catch(IOException i)
{
i.printStackTrace();
}
}
}

Serialized Object Inside :

Serialization is simply turning an existing object into a byte array.


This byte array represents
The version of the object,
The class of the object and
The internal state of the object.
This byte array can then be used between JVM's running the same
code to transmit/read the object.

Deserializing an Object:
The following DeserializeDemo program deserializes the Employee object
created in the SerializeDemo program. Study the program and try to
determine its output:

Core Java
public class DeserializeDemo
{
public static void main(String [] args)
{
Employee e = null;
try
{
FileInputStream fileIn = new FileInputStream("/tmp/employee.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
e = (Employee) in.readObject();
in.close();
fileIn.close();
}catch(IOException i)
{
i.printStackTrace();
return;
}catch(ClassNotFoundException c)
{
System.out.println("Employee class not found");
c.printStackTrace();
return;
}
System.out.println("Deserialized Employee...");
System.out.println("Name: " + e.name);
System.out.println("Address: " + e.address);
System.out.println("SSN: " + e.SSN);
System.out.println("Number: " + e.number);
}
}
This would produce the following result:
Deserialized Employee...
Name: Reyan Ali
Address:Phokka Kuan, Ambehta Peer
SSN: 0
Number:101
Here are following important points to be noted:

The try/catch block tries to catch a ClassNotFoundException, which


is declared by the readObject() method. For a JVM to be able to
deserialize an object, it must be able to find the bytecode for the
class. If the JVM can't find a class during the deserialization of
an object, it throws a ClassNotFoundException.

Notice that the return value of readObject() is cast to an Employee


reference.

The value of the SSN field was 11122333 when the object was
serialized, but because the field is transient, this value was not
sent to the output stream. The SSN field of the deserialized
Employee object is 0.

Core Java
Why would we want to do this? (Serialization)
There are several reasons:
1) Communication:

If you have two machines that are running the same code, and they
need to communicate, an easy way is for one machine to build an
object with information that it would like to transmit, and then
serialize that object to the other machine. It's not the best
method for communication, but it gets the job done.

2) Persistence:

If you want to store the state of a particular operation in a


database, it can be easily serialized to a byte array, and stored
in the database for later retrieval.

3) Deep Copy:

If you need an exact replica of an Object, and don't want to go to


the trouble of writing your own specialized clone() class, simply
serializing the object to a byte array, and then de-serializing it
to another object achieves this goal.

4) Caching:

Really just an application of the above, but sometimes an object


takes 10 minutes to build, but would only take 10 seconds to
de-serialize. So, rather than hold onto the giant object in memory,
just cache it out to a file via serialization, and read it in later
when it's needed.

5) Cross JVM Synchronization:

Serialization works across different JVMs that may be running on different


architectures.

Serialization with Inheritance :

If a class implements Serializable then all its subclasses will also be Serializable

Externalizable interface :

The Externalizable interface provides the facility of writing the


state of an object into a byte stream in compress format.

It is not a marker interface.

Core Java
Methods :
public void writeExternal(ObjectOutput out) throws IOException
public void readExternal(ObjectInput in) throws IOException

Serialization with Static data member :

If there is any static data member in a class, it will not be


serialized because static is related to class not to instance.

Example,
class Employee implements Serializable{
int id;
String name;
static String companyName="IBM";//it won't be serialized
public Student(int id, String name) {
this.id = id;
this.name = name;
}
}
Rule: In case of array or collection, all the objects of array or
collection must be serializable, if any object is not serialiizable then
serialization will be failed.

Difference between Serializable and Externalizable :


Serializable

Externalizable

Its used to serialize or persist


java objects

Its used to serialize or persist


java objects

In case of Serializable Java Virtual machine (JVM)


has full control for serializing object

while in case of Externalizable, application gets


control for persisting objects.
writeExternal() and readExternal() method provides
complete control on format and content of
Serialization process to application which can be
leverage to increase performance and speed of
serialization process.

In case of Serializable, default


serialization process is used.

while in case of Externalizable custom Serialization


process is used which is implemented by
application.
JVM gives call back to readExternel() and
writeExternal() of java.io.Externalizalbe interface for
restoring and writing objects into persistence.
Externalizable interface provides
complete control of serialization

Core Java
process to application.
It is a Marker Interface which is
placed in java.io package

Its not a Marker Interface which is


placed in java.io package

Example,
class EmployeeTest implements Externalizable {
public Integer id;
public String name;
public EmployeeTest() {
// TODO Auto-generated constructor stub
}
public EmployeeTest(Integer empid, String empname) {
this.id = empid;
this.name = empname;
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeInt(id);
out.writeObject(name);
}

@Override
public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
id = in.readInt();
name = (String) in.readObject();
}

public class SerializationTest {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
EmployeeTest empTest = new EmployeeTest(686, "Praveen");
ObjectOutputStream output = new ObjectOutputStream(new
FileOutputStream("sample.txt"));
output.writeObject(empTest);
output.close();
ObjectInputStream input = new ObjectInputStream(new
FileInputStream("sample.txt"));
EmployeeTest readObj = (EmployeeTest)
input.readObject();
System.out.println(readObj.id + " " + readObj.name);

Core Java
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Exception Handling :

The exception handling is one of the powerful mechanism provided in JAVA.

It provides the mechanism to handle the runtime errors so that normal flow
of the application can be maintained.

Exception is an abnormal condition.

In JAVA, exception is an event that disrupts the normal flow of the


program. It is an object which is thrown at runtime.

Exception Handling is a mechanism to handle runtime errors.

An exception is a problem that arises during the execution of a


program. An exception can occur for many different reasons,
including the following:
A user has entered invalid data.

exceptions are caused by user error

A file that needs to be opened cannot be found. exceptions are caused


by programmer error
A network connection has been lost in the middle of communications or the JVM
has run out of memory.
exceptions are caused by physical
resources that have failed in some manner

Advantage of Exception Handling :

The core advantage of exception handling is that normal flow of the


application is maintained.

Exception normally disrupts the normal flow of the application that


is why we use exception handling.

Example,
statement 1;
statement 2;
statement 3;
statement 4;

Core Java
statement 5;
statement 6;
statement 7;
statement 8;
statement 9;
statement 10;
Suppose there is 10 statements in your program and there occurs an
exception at statement 5, rest of the code will not be executed i.e.
statement 6 to 10 will not run.
If we perform exception handling, rest of the exception will be executed.
That is why we use exception handling.

Hierarchy of Exception :
All exception classes are subtypes of the java.lang.Exception class. The
exception class is a subclass of the Throwable class. Other than the
exception class there is another subclass called Error which is derived
from the Throwable class.

Types of Exception :
There are mainly two types of exceptions: checked and unchecked where
error is considered as unchecked exception.
The sun microsystem says there are three types of exceptions:

Core Java
1. Checked Exception
2. Unchecked Exception
3. Error
What is the difference between checked and unchecked exceptions ?
1)Checked Exception

The classes that extend Throwable class except RuntimeException and


Error are known as checked exceptions e.g.IOException, SQLException
etc.

Checked exceptions are checked at compile-time.

A checked exception is an exception that is typically a user error


or a problem that cannot be foreseen by the programmer.
For example, if a file is to be opened, but the file cannot be
found, an exception occurs.
These exceptions cannot simply be ignored at the time of
compilation.

2)Unchecked Exception

The classes that extend RuntimeException are known as unchecked


exceptions e.g. ArithmeticException, NullPointerException,
ArrayIndexOutOfBoundsException etc.

Unchecked exceptions are not checked at compile-time rather they


are checked at runtime.

A runtime exception is an exception that occurs that probably could


have been avoided by the programmer. As opposed to checked
exceptions, runtime exceptions are ignored at the time of compilation.

3)Error

Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError,


AssertionError etc.

These are not exceptions at all, but problems that arise beyond the control
of the user or the programmer.

Errors are typically ignored in your code because you can rarely do
anything about an error.
For example, if a stack overflow occurs, an error will arise.
They are also ignored at the time of compilation.

Core Java
Stack Overflow in Java

A stack is the part of the memory.

The local automatic variable is created on this stack and method arguments are passed.

When a process starts, it get a default stack size which is fixed for each process.

The default stack size is 1 Mb

Under abnormal condition, the stack limit exceeds. This is known as stack overflow.
The two most common reason for stack overflow are given below :

infinite recursion

allocating size greater than stack's limit

1. Infinite Recursion
The most common reason of stack overflow is Infinite Recursion. The infinite loop of recursion going on due
to which stack's limit exceeds.
For example, take a look at given below code:
int f(){
g();
}
int g() {
f();
}
f() is calling g() and g() is calling f(). Due to this loop goes on infinitely. This cause stack overflow.

Catching the stack overflow


You can put the suspicious code under the try block and can catch the StackOverflowError exception. Given
below code will give you clear idea how can you do this :
public class Example {
public static void endless() {
endless();
}
public static void main(String args[]) {
try {
endless();
} catch(StackOverflowError t) {
// more general: catch(Error t)
// anything: catch(Throwable t)

Core Java
System.out.println("Caught "+t);
t.printStackTrace();
}
System.out.println("After the error...");
}
}

Catching Exceptions:

A method catches an exception using a combination of the try and


catch keywords.

A try/catch block is placed around the code that might generate an


exception.

Code within a try/catch block is referred to as protected code, and


the syntax for using try/catch looks like the following:

try
{

//Protected code
}catch(ExceptionName e1)
{
//Catch block
}
A catch statement involves declaring the type of exception you are trying
to catch. If an exception occurs in protected code, the catch block (or
blocks) that follows the try is checked. If the type of exception that
occurred is listed in a catch block, the exception is passed to the catch
block much as an argument is passed into a method parameter.
Multiple catch Blocks:
A try block can be followed by multiple catch blocks. The syntax for
multiple catch blocks looks like the following:
try
{

//Protected code
}catch(ExceptionType1 e1)
{
//Catch block
}catch(ExceptionType2 e2)
{
//Catch block
}catch(ExceptionType3 e3)
{
//Catch block
}

Core Java
The previous statements demonstrate three catch blocks, but you can have
any number of them after a single try. If an exception occurs in the
protected code, the exception is thrown to the first catch block in the
list. If the data type of the exception thrown matches ExceptionType1, it
gets caught there. If not, the exception passes down to the second catch
statement. This continues until the exception either is caught or falls
through all catches, in which case the current method stops execution and
the exception is thrown down to the previous method on the call stack.
The throws/throw Keywords:

If a method does not handle a checked exception, the method must


declare it using the throws keyword. The throws keyword appears at
the end of a method's signature.

You can throw an exception, either a newly instantiated one or an


exception that you just caught, by using the throw keyword. Try to
understand the different in throws and throw keywords.

The throw keyword is used to explictily throw an exception.

We can throw either checked or unchecked exception. The throw keyword


is mainly used to throw custom exception.

The following method declares that it throws a RemoteException:


import java.io.*;
public class className
{
public void deposit(double amount) throws RemoteException
{
// Method implementation
throw new RemoteException();
}
//Remainder of class definition
}
A method can declare that it throws more than one exception, in which
case the exceptions are declared in a list separated by commas.
The finally Keyword (finally block) :

The finally keyword is used to create a block of code that follows


a try block.

A finally block of code


exception has occurred.

Using a finally block allows you to run any cleanup-type statements


that you want to execute, no matter what happens in the protected
code.

The finally block is a block that is always executed. It is mainly used


to perform some important tasks such as closing connection, stream etc.

always

executes,

whether

or

not

an

Core Java

Before terminating the program, JVM executes finally block(if any).

finally must be followed by try or catch block.

Why use finally block?

finally block can be used to put "cleanup" code such as closing a file,
closing connection etc.

Rule: For each try block there can be zero or more catch blocks, but only one
finally block.
Note: The finally block will not be executed if program exits(either by
calling System.exit() or by causing a fatal error that causes the process
to abort).
A finally block appears at the end of the catch blocks and has the
following syntax:
try
{

//Protected code
}catch(ExceptionType1 e1)
{
//Catch block
}catch(ExceptionType2 e2)
{
//Catch block
}catch(ExceptionType3 e3)
{
//Catch block
}finally
{
//The finally block always executes.
}
Note:

A catch clause cannot exist without a try statement.

It is not compulsory to have finally clauses when ever a try/catch


block is present.

The try block cannot be present without either catch clause or


finally clause.

Any code cannot be present in between the try, catch, finally


blocks.

Declaring you own Exception:


You can create your own exceptions in Java. Keep the following points in
mind when writing your own exception classes:

Core Java

All exceptions must be a child of Throwable.

If you want to write a checked exception that is automatically enforced


by the Handle or Declare Rule, you need to extend the Exception
class.

If you want to write a runtime exception, you need to extend the


RuntimeException class.

We can define our own Exception class as below:


class MyException extends Exception{}
You just need to extend the Exception class to create your own Exception
class. These are considered to be checked exceptions.
Why use nested try block?

try block within a try block is known as nested try block.

Sometimes a situation may arise where a part of a block may cause


one error and the entire block itself may cause another error. In
such cases, exception handlers have to be nested

Example,
public class ExceptionTest {
public static void main(String[] args) {
try {
try {
int a = 12 / 0;
} catch (ArithmeticException ex) {
System.out.println("ArithmeticException");
}
try {
int a[] = new int[4];
a[5] = 10;
} catch (ArrayIndexOutOfBoundsException ex) {
System.out.println("ArrayIndexOutOfBoundsException"
);

}
String str = null;
int length = str.length();
} catch (NullPointerException ex) {
System.out.println("NullPointerException");
} catch (ArrayStoreException ex) {
System.out.println("ArrayStoreException");
} catch (Exception ex) {

Core Java
System.out.println("Root Exception");
}

}
output :
ArithmeticException
ArrayIndexOutOfBoundsException
NullPointerException
Example,
public class NestedTryTest {
public static void main(String[] args) {
try {
try {
int a = 12 / 0;
int r[] = new int[4];
r[5] = 10;
} catch (ArithmeticException ex) {
System.out.println("ArithmeticException");
} catch (ArrayIndexOutOfBoundsException ex) {
System.out.println("ArrayIndexOutOfBoundsException"

);

}
String str = null;
int length = str.length();
} catch (NullPointerException ex) {
System.out.println("NullPointerException");
} catch (ArrayStoreException ex) {
System.out.println("ArrayStoreException");
} catch (Exception ex) {
System.out.println("Root Exception");
}
}
}
Output :
ArithmeticException
NullPointerException
Example,
public class ExceptionTest {
public static void main(String[] args) {
try {

Core Java
try {

int a = 12 / 0;
} catch (ArithmeticException ex) {
System.out.println("ArithmeticException");
}
try {

int a[] = new int[4];


a[5] = 10;
} catch (ArithmeticException ex) {
System.out.println("ArrayIndexOutOfBoundsException");
}
String str = null;
int length = str.length();
} catch (NullPointerException ex) {
System.out.println("NullPointerException");
} catch (ArrayStoreException ex) {
System.out.println("ArrayStoreException");
} catch (Exception ex) {
// base class
System.out.println("Root Exception");
} catch(ArrayIndexOutOfBoundsException ex) {// shows error cause
need to maintain inheritence hirerachy
System.out.println("ArrayIndexOutOfBoundsException");
}
}
}

Example,
public class ExceptionTest {
public static void main(String[] args) {
try {
try {
int a = 12 / 0;
} catch (ArithmeticException ex) {
System.out.println("ArithmeticException");
}
try {
int a[] = new int[4];
a[5] = 10; // exception handle at outside catch
} catch (ArithmeticException ex) {
System.out.println("ArrayIndexOutOfBoundsException");
}
String str = null;
int length = str.length();
} catch (NullPointerException ex) {
System.out.println("NullPointerException");
} catch (ArrayStoreException ex) {
System.out.println("ArrayStoreException");
}catch(ArrayIndexOutOfBoundsException ex) {
System.out.println("ArrayIndexOutOfBoundsException");
}catch (Exception ex) { // base class
System.out.println("Root Exception");

Core Java
}

Difference between throw and throws:

Throw

Throws

throw is used to explicitly throw


an exception.

throws is used to declare an


exception.

checked exception can not be


propagated without throws.

checked exception can be propagated


with throws.

throw is followed by an instance.

throws is followed by class.

You cannot throw multiple exception You can declare multiple exception
e.g. public void method()throws
IOException,SQLException.
throw is used within the method.

throws is used with the method


signature.

Its mainly used to throw custom


exception
Throw either checked or unchecked
exception

Throws used to declare checked


exception only

ExceptionHandling with MethodOverriding :


There are many rules if we talk about method-overriding with exception
handling. The Rules are as follows:
1) If the superclass method does not declare an exception

If the superclass method does not declare an exception, subclass


overridden method cannot declare the checked exception but it can
declare unchecked exception.

2) If the superclass method declares an exception

If the superclass method declares an exception, subclass overridden


method can declare same, subclass exception or no exception but
cannot declare parent exception.

Example,
1) If the superclass method does not declare an exception
Rule 1: If the superclass method does not declare an exception, subclass overridden method
cannot declare the checked exception.
class Parent{
void msg(){System.out.println("parent");}
}
class Child extends Parent{
void msg()throws IOException{

Core Java
System.out.println("child");
}
public static void main(String args[]){
Parent p=new Child();
p.msg();
}
}
output : Compile Time Error
Rule 2: If the superclass method does not declare an exception, subclass overridden method
cannot declare the checked exception but can declare unchecked exception.
class Parent{
void msg(){System.out.println("parent");}
}
class Child extends Parent{
void msg()throws ArithmeticException{
System.out.println("child");
}
public static void main(String args[]){
Parent p=new Child();
p.msg();
}
}
output :child
2) If the superclass method declares an exception
Rule 1: If the superclass method declares an exception, subclass overridden method can
declare same, subclass exception or no exception but cannot declare parent exception.
class Parent{
void msg()throws ArithmeticException{System.out.println("parent");}
}
class Child extends Parent{
void msg()throws Exception{System.out.println("child");}
public static void main(String args[]){
Parent p=new Child();

Core Java
try{
p.msg();
}catch(Exception e){}
}
}
output : Compile Time Error
Rule 2: Example in case subclass overridden method declares same exception
class Parent{
void msg()throws Exception{System.out.println("parent");}
}
class Child extends Parent{
void msg()throws Exception{System.out.println("child");}
public static void main(String args[]){
Parent p=new Child();
try{
p.msg();
}catch(Exception e){}
}
}
output : child
Rule 3: Example in case subclass overridden method declares subclass exception
class Parent{
void msg()throws Exception{System.out.println("parent");}
}
class Child extends Parent{
void msg()throws ArithmeticException{System.out.println("child");}
public static void main(String args[]){
Parent p=new Child();
try{
p.msg();

Core Java
}catch(Exception e){}
}
}
output : child
Rule 4: Example in case subclass overridden method declares no exception
class Parent{
void msg()throws Exception{System.out.println("parent");}
}
class Child extends Parent{
void msg(){System.out.println("child");}
public static void main(String args[]){
Parent p=new Child();
try{
p.msg();
}catch(Exception e){}
}
}
output : child

Core Java
Synchronization :

Synchronization is the capability of control the access of multiple threads


to any shared resource.

Synchronization is better in case we want only one thread can access the
shared resource at a time.

Why use Synchronization?

To prevent thread interference.

To prevent consistency problem.

Types of Synchronization
1. Process Synchronization
2. Thread Synchronization
Thread Synchronization
There are two types of thread synchronization mutual exclusive and
inter-thread communication.

Mutual Exclusive
Synchronized method.
Synchronized block.
static synchronization.

Cooperation (Inter-thread communication)

Mutual Exclusive
Mutual Exclusive helps keep threads from interfering with one another
while sharing data. This can be done by three ways in java:

by synchronized method

by synchronized block

by static synchronization

Understanding the concept of Lock

Synchronization is built around an internal entity known as the


lock or monitor.

Every object has an lock associated with it.

By convention, a thread that needs consistent access to an object's


fields has to acquire the object's lock before accessing them, and
then release the lock when it's done with them.

Core Java
Understanding the problem without Synchronization
In this example, there is no synchronization, so output is inconsistent.
Example,
Class Table{
void printTable(int n){//method not synchronized
for(int i=1;i<=5;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){System.out.println(e);}
}
}
}
class MyThread1 extends Thread{ User-defined Thread Class
Table t;
MyThread1(Table t){
this.t=t;
}
public void run(){
t.printTable(5);
}
}
class MyThread2 extends Thread{ User-defined Thread Class
Table t;
MyThread2(Table t){
this.t=t;
}
public void run(){
t.printTable(100);
}
}
class Use{
public static void main(String args[]){

Core Java
Table obj = new Table();//only one object
MyThread1 t1=new MyThread1(obj);

First Thread

MyThread2 t2=new MyThread2(obj);

Second Thread

t1.start();
t2.start();
}
}
Output :5

In-Consistent Output

100
10
200
15
300
20
400
25
500

Solution by synchronized method

If you declare any method as synchronized, it is known as


synchronized method.

Synchronized method is used to lock an object for any shared resource.

When a thread invokes a synchronized method, it automatically acquires the


lock for that object and releases it when the method returns.

Example,
Class Table{
synchronized void printTable(int n){//synchronized method
for(int i=1;i<=5;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){System.out.println(e);}
}
}
}

Core Java
class MyThread1 extends Thread{ User-defined Thread Class
Table t;
MyThread1(Table t){
this.t=t;
}
public void run(){
t.printTable(5);
}
}
class MyThread2 extends Thread{ User-defined Thread Class
Table t;
MyThread2(Table t){
this.t=t;
}
public void run(){
t.printTable(100);
}
}
class Use{
public static void main(String args[]){
Table obj = new Table();//only one object
MyThread1 t1=new MyThread1(obj); First Thread
MyThread2 t2=new MyThread2(obj); Second Thread
t1.start();
t2.start();
}
}
Output:5
10
15
20
25
100
200
300
400

Consistent Output

Core Java
500
Same Example of synchronized method by using annonymous class
In this program, we have created the two threads by annonymous class, so
less coding is required.
Example,
Class Table{
synchronized void printTable(int n){//synchronized method
for(int i=1;i<=5;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){System.out.println(e);}
}
}
}
class Use{
public static void main(String args[]){
final Table obj = new Table();

Thread t1=new Thread(){

//only one object Inner class cant


accept non-final instance

First Thread in Annonymous Class

public void run(){


obj.printTable(5);
}
};
Thread t2=new Thread(){
public void run(){
obj.printTable(100);
}
};
t1.start();
t2.start();
}
}
output : 5
10
15

Second Thread in Annonymous Class

Core Java
20
25
100
200
300
400
500

Synchronized block

Synchronized block can be used to perform synchronization on any specific


resource of the method.

Suppose you have 50 lines of code in your method, but you want to
synchronize only 5 lines, you can use synchronized block.

If you put all the codes of the method in the synchronized block,
it will work same as the synchronized method.

Points to remember for Synchronized block

Synchronized block is used to lock an object for any shared resource.

Scope of synchronized block is smaller than the method.

Syntax
synchronized (object reference expression) {
//code block
}
Example,
Class Table{
void printTable(int n){//synchronized method
synchronized(this){//synchronized block
for(int i=1;i<=5;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){System.out.println(e);}
}
}
}

Core Java
}
class Use{
public static void main(String args[]){
final Table obj = new Table();

Thread t1=new Thread(){

//only one object Inner class cant


accept non-final instance

First Thread in Annonymous Class

public void run(){


obj.printTable(5);
}
};
Thread t2=new Thread(){

Second Thread in Annonymous Class

public void run(){


obj.printTable(100);
}
};
t1.start();
t2.start();
}
}
output : 5
10
15
20
25
100
200
300
400
500

Static synchronization
If you make any static method as synchronized, the lock will be on the class not
on object.

Core Java

Example,
class Table{
synchronized static void printTable(int n){
for(int i=1;i<=10;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){}
}
}
}
public class Test {
public static void main(String[] args) {
Thread t1=new Thread(){
public void run(){
Table.printTable(1);
}
};
Thread t2=new Thread(){
public void run(){
Table.printTable(10);
}
};

Core Java
Thread t3=new Thread(){
public void run(){
Table.printTable(100);
}
};
Thread t4=new Thread(){
public void run(){
Table.printTable(1000);
}
};
t1.start();
t2.start();
t3.start();
t4.start();
}
}

DeadLock
Deadlock can occur in a situation when a thread is waiting for an object
lock, that is acquired by another thread and second thread is waiting for
an object lock that is acquired by first thread. Since, both threads are
waiting for each other to release the lock, the condition is called
deadlock.

Example,
public class DeadlockExample {
public static void main(String[] args) {
final String resource1 = "ratan jaiswal";
final String resource2 = "vimal jaiswal";
// t1 tries to lock resource1 then resource2
Thread t1 = new Thread() {
public void run() {
synchronized (resource1) {

Core Java
System.out.println("Thread 1: locked resource 1");
try { Thread.sleep(100);} catch (Exception e) {}
synchronized (resource2) {
System.out.println("Thread 1: locked resource 2");
}
}
}
};
// t2 tries to lock resource2 then resource1
Thread t2 = new Thread() {
public void run() {
synchronized (resource2) {
System.out.println("Thread 2: locked resource 2");
try { Thread.sleep(100);} catch (Exception e) {}
synchronized (resource1) {
System.out.println("Thread 2: locked resource 1");
}
}
}
};
t1.start();
t2.start();
}
}
output :Thread 1: locked resource 1
Thread 2: locked resource 2

Inter-thread communication (Cooperation)

Cooperation(Inter-thread communication) is all about making


synchronized threads communicate with each other.

Cooperation (Inter-thread communication) is a mechanism in which a


thread is paused running in its critical section and another thread
is allowed to enter (or lock) in the same critical section to be
executed.

Core Java
It is implemented by following methods of Object class

wait()

notify()

notifyAll()

wait()

Causes current thread to release the lock and wait until either another thread
invokes the notify() or notifyAll() for this object, or a specified amount of time has
elapsed.

The current thread must own this object's monitor.


Syntax:

public final void wait()throws InterruptedException

public final void wait(long timeout)throws InterruptedException

notify()

Wakes up a single thread that is waiting on this object's monitor. If


any threads are waiting on this object, one of them is chosen to be
awakened. The choice is arbitrary and occurs at the discretion of
the implementation.

public final void notify()

notifyAll()

Wakes up all threads that are waiting on this object's monitor.

public final void notifyAll()

Core Java
JAVA Access modifiers

Access modifiers specifies who can access them. There are four
access modifiers used in java.

They are public, private, protected, no modifer (declaring without


an access modifer). Using no modifier is also sometimes referred
as package-private or default or friendly access.

Usage of these access modifiers is restricted to two levels.

The two levels are class level access modifiers and member level
access modifiers.

I) Class level access modifiers (java classes only)


Only two access modifiers is allowed, public and no modifier (default)
If a class is public, then it CAN be accessed from ANYWHERE.
If a class has no modifer, then it CAN ONLY be accessed from
same package.
II) Member level access modifiers (java variables and java methods)
All the four public, private, protected and no modifer is allowed.
public and no modifier the same way as used in class level.
private members CAN ONLY access.
protected CAN be accessed from same package and a subclass
existing in any package can access.
For better understanding, member level access is formulated as a table:
Access Modifiers
public
protected
no access modifier
private

Same Class
Y
Y
Y
Y

Same Package

Subclass Other packages

Y
Y
Y
N

Y
Y
N
N

First row {public Y Y Y Y} should be interpreted as:


Y A member declared with public access modifier CAN be
accessed by the members of the same class.
Y A member declared with public access modifier CAN be
accessed by the members of the same package.
Y A member declared with public access modifier CAN be
accessed by the members of the subclass.

Y
N
N
N

Core Java
Y A member declared as public CAN be accessed from Other
packages.
Default access Modifier
Your constructors access modifier would be package-private(default). As
you have declared class public so it will be visible everywhere but the
constructor will not. Your constructor will be visible only in it's
package.
package flight.booking;
public class FlightLog // public access modifier
{
private SpecificFlight flight;
FlightLog(SpecificFlight flight) // default access modifier
{
this.flight = flight;
}
}
When you do not write any constructor in your class then compiler generates
a default constructor with the same access modifier of the class.
For following example compiler will generate a default constructor with
public access modifier(same as class).
package flight.booking;
public class FlightLog // public access modifier
{
private SpecificFlight flight;
}
OOPS
Object-Oriented Programming (OOP) is a programming paradigm using "objects" to design
applications.

Objects are key to understanding object-oriented technology


Writing object-oriented programs involves creating classes, creating
objects from those classes, and creating applications, which are
stand-alone executable programs that use those objects.

A class is a template, blueprint,or contract that defines what an


objects data fields and methods will be.

An object is a runtime entity or an instance of a class. You can create many


instances of a class.

Core Java

A Java class uses variables to define data fields and methods to define
actions.

Additionally, a class provides methods of a special type, known as


constructors, which are invoked to create a new object.

A constructor can perform any action, but constructors are designed to


perform initializing actions, such as initializing the data fields of objects.

Objects are made up of attributes and methods.

Attributes are the characteristics that define an object; the


values contained in attributes differentiate objects of the same
class from one another.

To understand this better lets take example of Mobile as object.


Mobile has characteristics like model, manufacturer, cost,
operating system etc.

So if we create Samsung mobile object and IPhone mobile object


we can distinguish them from characteristics.

The values of the attributes of an object are also referred to as the


objects state.

Method is an Object behavior

There are three main features of OOPS Concepts.

Core Java
1)Encapsulation
2)Inheritance
3)Polymorphism
4)Abstraction

Encapsulation

Encapsulation means putting together all the variables (instance variables) and the
methods into a single unit called Class.

It also means hiding data and methods within an Object.

Encapsulation in Java is the process of wrapping up of data (properties)


and behavior (methods) of an object into a single unit; and the unit here is a Class
(or interface).

Encapsulate in plain English means to enclose or be enclosed in or


as if in a capsule. In Java, a class is the capsule (or unit). In
Java, everything is enclosed within a class or interface.

Encapsulation enables data hiding, hiding irrelevant information from the


users of a class and exposing only the relevant details required by the user.
We can expose our operations hiding the details of what is
needed to perform that operation.
We can protect the internal state of an object by hiding its
attributes from the outside world (by making it private), and
then exposing them through setter and getter methods. Now
modifications to the object internals are only controlled
through these methods.

Example,
private String name;
public String getName() {
return name;
}
public void setName(String name){
this.name=name;
}

public class Mobile {


private String manufacturer;
private String operating_system;
public String model;
private int cost;
//Constructor to set properties/characteristics of object

Core Java
Mobile(String man, String o,String m, int c){
this.manufacturer = man;
this.operating_system = o;
this.model = m;
this.cost = c;
}
//Method to get access Model property of Object
public String getModel(){
return this.model;
}
// We can add other method to get access to other properties
}

Inheritance

An important feature of object-oriented programs is inheritancethe


ability to create classes that share the attributes and methods of
existing classes, but with more specific features.

Inheritance is mainly used for code re-usability.

In general one line definition we can tell that deriving a new class
from an existing class, its called as Inheritance.

Java supports multiple inheritance (multiple parents, single child) only through
interfaces.

Class extends one class but implements multiple interfaces.

Inheritance is the property which allows a Child class to inherit


some properties from its parent class. In Java this is achieved by
using extends keyword.

Only properties with access modifier public and protected can be accessed in child
class.

Example,
public class Android extends Mobile{
//Constructor to set properties/characteristics of object
Android(String man, String o,String m, int c){
super(man, o, m, c);
}
//Method to get access Model property of Object
public String getModel(){
return "This is Android Mobile- " + model;

Core Java
}
}
public class Blackberry extends Mobile{
//Constructor to set properties/characteristics of object
Blackberry(String man, String o,String m, int c){
super(man, o, m, c);
}
public String getModel(){
return "This is Blackberry-"+ model;
}
}

Polymorphism

Polymorphism definition is that Poly means many and morphos means


forms.

It describes the feature of languages that allows the same word or


symbol to be interpreted correctly in different situations based on
the context.

There are two types of Polymorphism available in Java.


1. Compile Time Polymorphism Method Overloading Static polymorphism
2. Run Time Polymorphism Method Overriding Dynamic polymorphism

Compile Time Polymorphism

The ability to execute different method implementations by altering the argument


used with the method name is known as method overloading.

Overloading is determined at the compile time.

Different method signature and different number or type of parameters.

Same method signature but different number of parameters.

Same method signature and same number of parameters but of different type

Example,
Example of Overloading
int add(int a,int b)
float add(float a,int b)
float add(int a ,float b)
void add(float a)
int add(int a)
void add(int a)

//error conflict with the method int add(int a)

class Overloadsample {

Core Java
public void print(String s){
System.out.println("First Method with only String- "+ s);
}
public void print (int i){
System.out.println("Second Method with only int- "+ i);
}
public void print (String s, int i){
System.out.println("Third Method with both- "+ s + "--" + i);
}
}
public class PolymDemo {
public static void main(String[] args) {
Overloadsample obj = new Overloadsample();
obj.print(10);
obj.print("Amit");
obj.print("Hello", 100);
}
}
Example: Overloading
Class BookDetails{
String title;
String publisher;
float price;
setBook(String title){
}
setBook(String title, String publisher){
}
setBook(String title, String publisher,float price){
}
}
Example: Overriding
class BookDetails{
String title;
setBook(String title){ }
}
class ScienceBook extends BookDetails{
setBook(String title){}
//overriding
setBook(String title, String publisher,float price){ } //overloading
}

Core Java
Run Time Polymorphism - Method-overriding
Example,
public class OverridingDemo {
public static void main(String[] args) {
//Creating Object of SuperClass and calling getModel Method
Mobile m = new Mobile("Nokia", "Win8", "Lumia",10000);
System.out.println(m.getModel());
//Creating Object of Sublcass and calling getModel Method
Android a = new Android("Samsung", "Android", "Grand",30000);
System.out.println(a.getModel());
//Creating Object of Sublcass and calling getModel Method
Blackberry b = new Blackberry("BlackB", "RIM", "Curve",20000);
System.out.println(b.getModel());
}
}

Abstraction

All programming languages provide abstractions. It can be argued


that the complexity of the problems youre able to solve is directly related to the
kind and quality of abstraction.

An essential element of object-oriented programming is abstraction.


Humans manage complexity through abstraction.

Car Example,

When you drive your car you do not have to be concerned with the
exact internal working of your car(unless you are a mechanic).

What you are concerned with is interacting with your car via its
interfaces like steering wheel, brake pedal, accelerator pedal etc.

Various manufacturers of car has different implementation of car


working but its basic interface has not changed (i.e. you still use
steering wheel, brake pedal, accelerator pedal etc to interact with
your car).

Hence the knowledge you have of your car is abstract.

Manage Abstraction

A powerful way to manage abstraction is through the use of hierarchical


classifications.

This allows you to layer the semantics of complex systems, breaking

Core Java
them into more manageable pieces.

From the outside, the car is a single object. Once inside, you see
that the car consists of several subsystems: steering, brakes,
sound system, seat belts, heating, cellular phone, and so on.

In turn, each of these subsystems is made up of more specialized


units. For instance, the sound system consists of a radio, a CD
player, and/or a tape player.

The point is that you manage the complexity of the car (or any other complex
system)through the use of hierarchical abstractions.

Abstract Class ( Incomplete Class)

An abstract class is something which is incomplete and you can not


create instance of abstract class.

If you want to use it you need to make it complete or concrete by


extending it.

A class is called concrete if it does not contain any abstract


method and implements all abstract method inherited from abstract
class or interface it has implemented or extended.

By the way Java has concept of abstract classes, abstract method but a
variable can not be abstract in Java.

Vehicle Example,
Lets take an example of Java Abstract Class called Vehicle. When I am
creating a class called Vehicle, I know there should be methods like
Start() and Stop() but don't know start and stop mechanism of every vehicle
since they could have different start and stop mechanism e.g some can be
started by kick or some can be by pressing buttons.
Advantage of Abstraction is if there is new type of vehicle introduced we
might just need to add one class which extends Vehicle Abstract class and
implement specific methods. Interface of start and stop method would be
same.
Example,

Core Java
public abstract class VehicleAbstract {
public abstract void start();
public void stop(){
System.out.println("Stopping Vehicle in abstract class");
}
}
class TwoWheeler extends VehicleAbstract{
@Override
public void start() {
System.out.println("Starting Two Wheeler");
}
}
class FourWheeler extends VehicleAbstract{
@Override
public void start() {
System.out.println("Starting Four Wheeler");
}
}
public class VehicleTesting {
public static void main(String[] args) {
VehicleAbstract my2Wheeler = new TwoWheeler();
VehicleAbstract my4Wheeler = new FourWheeler();
my2Wheeler.start();
my2Wheeler.stop();
my4Wheeler.start();
my4Wheeler.stop();
}
}

Core Java
What Is an Object?

Objects are key to understanding object-oriented technology

real-world objects: your dog, your desk, your television set, your
bicycle.

Real-world objects share two characteristics: They all have state and behavior.
Dogs have state (name, color, breed, hungry) and behavior (barking,
fetching, wagging tail). Bicycles also have state (current gear, current
pedal cadence, current speed) and behavior (changing gear, changing pedal
cadence, applying brakes)

Software Objects

Software objects are conceptually similar to real-world objects:


they too consist of state and related behavior.

An object stores its state in fields (variables in some programming


languages) and exposes its behavior through methods (functions in
some programming languages).

Methods operate on an object's internal state and serve as the


primary mechanism for object-to-object communication.

Hiding internal state and requiring all interaction to be performed


through an object's methods is known as data encapsulation a
fundamental principle of object-oriented programming.

Benefits of Objects

Information-hiding: By interacting only with an object's methods, the


details of its internal implementation remain hidden from the
outside world.

Code re-use: If an object already exists (perhaps written by another


software developer), you can use that object in your program. This
allows specialists to implement/test/debug complex, task-specific
objects, which you can then trust to run in your own code.

Pluggability and debugging ease: If a particular object turns out to be


problematic, you can simply remove it from your application and

Core Java
plug in a different object as its replacement. This is analogous to
fixing mechanical problems in the real world. If a bolt breaks, you
replace it, not the entire machine.
What is a Class ?

Class is the blue print of an Object.

Example,
bicycle is an instance of the class of objects known as bicycles. A class is the
blueprint from which individual objects are created.
What Is an Interface ?

Interfaces form a contract between the class and the outside world, and this
contract is enforced at build time by the compiler.

If your class claims to implement an interface, all methods defined


by that interface must appear in its source code before the class
will successfully compile.

What is a Package ?
A package is a namespace that organizes a set of related classes and interfaces.
Example,

Conceptually you can think of packages as being similar to


different folders on your computer.

You might keep HTML pages in one folder, images in another, and
scripts or applications in yet another.

Because software written in the Java programming language can be


composed of hundreds or thousands of individual classes, it makes
sense to keep things organized by placing related classes and
interfaces into packages.

What is multiple inheritance and does java support?


Ans) If a child class inherits the property from multiple classes is known as multiple inheritance.
Java does not allow to extend multiple classes but to overcome this problem it allows to implement
multiple Interfaces.

What is abstraction?
Ans) Abstraction is way of converting real world objects in terms of class. For example creating a class
Vehicle and injecting properties into it. E.g

public class Vehicle {

Core Java
public String colour;
public String model;
}

What is Association?
Ans) Association is a relationship between two classes. In this relationship the object of one instance
perform an action on behalf of the other class. The typical behavior can be invoking the method of other
class and using the member of the other class.
public class MyMainClass{
public void init(){
new OtherClass.init();
}
}

What is Aggregation?
Ans) Aggregation has a relationship between two classes. In this relationship the object of one class is a
member of the other class. Aggregation always insists for a direction.
public class MyMainClass{
OtherClass otherClassObj = new OtherClass();
}

Inner Class
What is an inner class?
Ans) Inner class is a class defined inside other class and act like a member of the enclosing

class.
What are the different types of inner classes?

Ans) There are two main types of inner classes


Static inner class
Inner class
Member class
Anonymous class
Local class
What is static member class?

Consider the following Java code fragment:


public class A

Core Java
{
int y;
public static class B
{
int x;
void f () {}

}
}

This fragment defines the class A which contains an static inner class B.
A static inner class behaves like any ``outer'' class. It may contain
methods and fields, and it may be instantiated like this:
A.B object = new A.B ();
This statement creates an new instance of the inner class B. Given such
an instance, we can invoke the f method in the usual way:
object.f();
Note, it is not necessarily the case that an instance of the outer class
A exists even when we have created an instance of the inner class.
Similarly, instantiating the outer class A does not create any instances
of the inner class B.
The methods of a static inner class may access all the members (fields or methods) of the
inner class but they can access only static members (fields or methods) of the outer class.
Thus, f can access the field x, but it cannot access the field y.
Example,
class OuterTestCLass {
String str1 = "OuterCLass";
static String str2 = "OuterCLass1";
public static class StaticInnerClass {
int n = 10;
String str = "SampleInnerStaticVar";
void print() {
System.out.println(n + " " + str + + str2);
//System.out.println(str1); // only allows static member of outer class
OuterTestCLass outer = new OuterTestCLass();
outer.display();
}
}
public void display() {
System.out.println(str1);
}
/**

Core Java
* @param args
*/
/*public static void main(String[] args) {
// TODO Auto-generated method stub
StaticInnerClassDemo outer = new StaticInnerClassDemo();
outer.display();
StaticInnerClass inner = new StaticInnerClass();
inner.print();
StaticInnerClassDemo.StaticInnerClass obj = new
StaticInnerClassDemo.StaticInnerClass();
obj.print();
}*/
}
public class StaticInnerClassDemo {
public static void main(String[] args) {
/*StaticInnerClass in = new StaticInnerClass();// Throws an Error cause static
inner class cant have instance
in.print();*/
OuterTestCLass.StaticInnerClass inner = new OuterTestCLass.StaticInnerClass();
inner.print();
}
}

Difference between static inner class and non-static inner class ?

A static java inner class cannot have instances.

A non-static java inner class can have instances that belong to the outer class.

Member class

The member class can be declared as public, private, protected, final and abstract.

If you mention private inner class its visible only enclosing class not in outer world

Example,
class InnerClassDemoSample {
private String str = "Welcome Outer private Variable";
static int m = 100;
final int n = 25;
public void demo() {
System.out.println("Outer Class method");
InnerClass inner = new InnerClass();
inner.demoInner();
}
private class innerprivateclass {

Core Java

public void innerprivateclassmethod() {


System.out.println("priavte inner class method");
}

abstract class innerabstract {

abstract void sample();


public void innerabstactmethod() {
System.out.println("abstract inner class method");
}

protected class InnerClass extends innerabstract {


String strinner = "innervariable";
//
static int n = 10; // static variable is not allowed here inside the
non-static inner class
final int n = 10;
public void demoInner() {
System.out.println("outer Class var " + str);
System.out.println("Inner Class var " + strinner + " " + n);
System.out.println(InnerClassDemoSample.m);
System.out.println(new InnerClassDemoSample().n);
}
@Override
void sample() {
// TODO Auto-generated method stub
System.out.println("overriden abstarct inner class method");
}
}
/*public static void main(String[] args) {
InnerClassDemo in = new InnerClassDemo();
in.demo();
InnerClass inp = in.new InnerClass();
inp.demoInner();
}*/
}
class InnerClassDemo {
public static void main(String[] args) {
InnerClassDemoSample.InnerClass inp = new InnerClassDemoSample().new
InnerClass();
inp.demoInner();
inp.sample();
InnerClassDemoSample outer = new InnerClassDemoSample();
outer.demo();
}

Local and Anonymous Classes

You can declare an inner class within the body of a method. These classes are
known as local classes.

Core Java

You can also declare an inner class within the body of a method without naming the
class. These classes are known as anonymous classes.

Local Class

The inner class declared inside the method is called method local inner class.

Method local inner class can only be declared as final or abstract.

Method local class can only access global variables or method local variables if declared
as final

public class InnerClass {


int i = 9;
public void method1() {
final int k = 6;
class MethodLocal {
MethodLocal() {
System.out.println(k + i);
}
}
}
}

class LocalInnerClassDemo {
int m = 10; // it may have any modifier like private, public, final,
static like that
public void method() {
final String s = "testlocal";
String str1 = "teststatic";
abstract class LocalInnerabstractClass {
public abstract void sample();
public void localinnerabstractmethod() {
System.out.println(m);
System.out.println(s);
}
}
final class LocalInnerClass extends LocalInnerabstractClass {
String str = "localinnerclass";
public void localinnermethod() {
System.out.println(str);
System.out.println(m);
//
System.out.println(s + " " + str1); // local variable
also not allowed here only final variable allowed
}
@Override
public void sample() {
System.out.println("overriden local inner abstract mehod");
}
}
LocalInnerClass inner = new LocalInnerClass();

Core Java

inner.localinnermethod();
inner.sample();
inner.localinnerabstractmethod();

}
public class TestLocalInnerClassDemo {
public static void main(String[] args) {
LocalInnerClassDemo inner = new LocalInnerClassDemo();
inner.method();
}
}
Anonymous inner class

These are local classes which are automatically declared and


instantiated in the middle of an expression.

They can specify arguments to the constructor of the superclass,


but cannot otherwise have a constructor.

They can implement only one interface or extend a class.

Anonymous class cannot define any static fields, methods, or


classes, except for static final constants.

Also, like local classes, anonymous classes cannot be public, private,


protected, or static

List<Parent> l = new ArrayList<Parent>();


l.add(new Parent(2));
l.add(new Parent(3));
Collections.sort(l, new Comparator() {
public int compare(Object o1, Object o2) {
Parent prt1 = (Parent) o1;
Parent prt2 = (Parent) o2;
if (prt1.getAge() > prt2.getAge()) {
return -1;
}else if(prt1.getAge()<prt2.getAge()) {
return 1;
} else {
return 0;
}
}
});

What are the advantages of Inner classes?

The inner class is to be used only by one class.

It makes the code more readable and maintainable.

The inner class shares a special relationship with the outer class i.e. the inner class has access to
all members of the outer class and still have its own type is the main advantages of Inner class.

Advantage of inner class is that they can be hidden from the other classes in the same
package and still have the access to all the members (private also) of the enclosing
class. So the outer class members which are going to be used by the inner class can be

Core Java
made private and the inner class members can be hidden from the classes in the same
package. This increases the level of encapsulation.

The advantage of using static nested class is that to instantiate a static nested class you need not
create an instance of the enclosing class which reduces the number of objects the
application creates at runtime.

Inner classes are best used in the event handling mechanism and to implement the helper
classes.

What are disadvantages of using inner classes?


1. Using inner class increases the total number of classes being used by the application. For all the
classes created by JVM and loaded in the memory, JVM has to perform some tasks like creating the
object of type class. JVM may have to perform some routine tasks for these extra classes
created which may result slower performance if the application is using more number of inner
classes.
2. Inner classes get limited support of ide/tools as compared to the top level classes, so
working with the inner classes is sometimes annoying for the developer.

ENUM
Enum in Java is a keyword, a feature which is used to represent fixed
number of well known values in Java,
An enum type is a special data type that enables for a variable to be a
set of predefined constants. The variable must be equal to one of the
values that have been predefined for it.
How to represent enumerable value without Java enum
public class CurrencyDenom
public static final int
public static final int
public static final int
public static final int
}

{
PENNY = 1;
NICKLE = 5;
DIME = 10;
QUARTER = 25;

public class Currency {


private int currency; //CurrencyDenom.PENNY,CurrencyDenom.NICKLE,
// CurrencyDenom.DIME,CurrencyDenom.QUARTER
}
Though this can server our purpose it has some serious limitations:
1) No Type-Safety: First of all its not type-safe; you can assign any
valid int value to currency e.g. 99 though there is no coin to represent
that value.
2) No Meaningful Printing: printing value of any of these constant will
print its numeric value instead of meaningful name of coin e.g. when you
print NICKLE it will print "5" instead of "NICKLE"
Enum in Java is type-safe, provides meaningful String names and has there own namespace.

Core Java
Now let's see same example using Enum in Java:
public enum Currency {PENNY, NICKLE, DIME, QUARTER};
Here Currency is our enum and PENNY, NICKLE, DIME, QUARTER are enum
constants.
Notice curly braces around enum constants because Enum are type like
class and interface in Java.
Also we have followed similar naming convention for enum like class and
interface (first letter in Caps) and since Enum constants are implicitly
static final we have used all caps to specify them like Constants in Java.
What is Enum in Java

Enum is a keyword in java

Java Enum is type like class and interface and can be used to define a
set of Enum constants.

Enum constants are implicitly static and final and you can not change there
value once created.

Enum in Java provides type-safety and can be used inside switch


statment like int variables.

Benefits of Enums in Java:


1) Enum is type-safe you can not assign anything else other than predefined Enum
constants to an Enum variable. It is compiler error to assign something else
unlike the public static final variables used in Enum int pattern and Enum
String pattern.
2) Enum has its own name-space.
3) Best feature of Enum is you can use Enum in Java inside Switch
statement like int or char primitive data type.we will also see example
of using java enum in switch statement in this java enum tutorial.
4) Adding new constants on Enum in Java is easy and you can add new
constants without breaking existing code.

Important points about Enum in Java


1) Enums in Java are type-safe and has there own name-space. It means
your enum will have a type for example "Currency" in below example and
you can not assign any value other than specified in Enum Constants.
public enum Currency {PENNY, NICKLE, DIME, QUARTER};
Currency coin = Currency.PENNY;
coin = 1; //compilation error

Core Java
2) Enum in Java are reference type like class or interface and you can
define constructor, methods and variables inside java Enum which makes it
more powerful
3) You can specify values of enum constants at the creation time as shown
in below example:
public enum Currency {PENNY(1), NICKLE(5), DIME(10), QUARTER(25)};
But for this to work you need to define a member variable and a
constructor because PENNY (1) is actually calling a constructor which
accepts int value , see below example.
public enum Currency {
PENNY(1), NICKLE(5), DIME(10), QUARTER(25);
private int value;
private Currency(int value) {
this.value = value;
}
};
Constructor of enum in java must be private any other access modifier
will result in compilation error. Now to get the value associated with
each coin you can define a public getValue() method inside java enum like
any normal java class.
4) Enum constants are implicitly static and final and can not be changed once created. For
example below code of java enum will result in compilation error:
Currency.PENNY = Currency.DIME;
The final field EnumExamples.Currency.PENNY cannot be re assigned.
5) Enum in java can be used as an argument on switch statment and with
"case:" like int or char primitive type. This feature of java enum makes
them very useful for switch operations. Lets see an example of how to
use java enum inside switch statement:
Currency usCoin = Currency.DIME;
switch (usCoin) {
case PENNY:
System.out.println("Penny coin");
break;
case NICKLE:
System.out.println("Nickle coin");
break;
case DIME:
System.out.println("Dime coin");
break;
case QUARTER:
System.out.println("Quarter coin");
}
6) Since constants defined inside Enum in Java are final you can safely
compare them using "==" equality operator as shown in following example
of Java Enum:
Currency usCoin = Currency.DIME;
if(usCoin == Currency.DIME){

Core Java
System.out.println("enum in java can be compared using ==");
}
By the way comparing objects using == operator is not recommended, Always
use equals() method or compareTo() method to compare Objects.
7) Java compiler automatically generates static values() method for every enum
in java. Values() method returns array of Enum constants in the same
order they have listed in Enum and you can use values() to iterate over
values of Enum in Java as shown in below example:
for(Currency coin: Currency.values()){
System.out.println("coin: " + coin);
}
And it will print:
coin: PENNY
coin: NICKLE
coin: DIME
coin: QUARTER
Notice the order its exactly same with defined order in enums.
8) In Java Enum can override methods also. Lets see an example of
overriding toString() method inside Enum in Java to provide meaningful
description for enums constants.
public enum Currency {
........
@Override
public String toString() {
switch (this) {
case PENNY:
System.out.println("Penny: " + value);
break;
case NICKLE:
System.out.println("Nickle: " + value);
break;
case DIME:
System.out.println("Dime: " + value);
break;
case QUARTER:
System.out.println("Quarter: " + value);
}
return super.toString();

}
};
And here is how it looks like when displayed:
Currency usCoin = Currency.DIME;
System.out.println(usCoin);
output:
Dime: 10

10) You can not create instance of enums by using new operator in Java

Core Java
because constructor of Enum in Java can only be private and Enums
constants can only be created inside Enums itself.
11) Instance of Enum in Java is created when any Enum constants are first
called or referenced in code.
12) Enum in Java can implement the interface and override any method like
normal class Its also worth noting that Enum in java implicitly implement both
Serializable and Comparable interface. Let's see and example of how to implement
interface using Java Enum:
public enum Currency implements Runnable{
PENNY(1), NICKLE(5), DIME(10), QUARTER(25);
private int value;
............
@Override
public void run() {
System.out.println("Enum in Java implement interfaces");
}
}
13) You can define abstract methods inside Enum in Java and can also
provide different implementation for different instances of enum in java.
Lets see an example of using abstract method inside enum in java
public enum Currency implements Runnable{
PENNY(1) {
@Override
public String color() {
return "copper";
}
}, NICKLE(5) {
@Override
public String color() {
return "bronze";
}
}, DIME(10) {
@Override
public String color() {
return "silver";
}
}, QUARTER(25) {
@Override
public String color() {
return "silver";
}
};
private int value;
public abstract String color();
private Currency(int value) {
this.value = value;
}

Core Java
..............
}

In this example since every coin will have different color we made the
color() method abstract and let each instance of Enum to define
there
own color. You can get color of any coin by just calling color() method
as shown in below example of java enum:
System.out.println("Color: " + Currency.DIME.color());
Enum Java valueOf example
One of my reader pointed out that I have not mention about valueOf method
of enum in Java, which is used to convert String to enum in java. It could also
include valueOf() method of enum in java which is added by compiler in any enum
along with values() method. Enum valueOf() is a static method which takes a
string argument and can be used to convert a String into enum. One think
though you would like to keep in mind is that valueOf(String) method of
enum will throw "Exception in thread "main"
java.lang.IllegalArgumentException: No enum const class" if you supply
any string other than enum values.

What is the difference between an enum type and java.lang.Enum?


An enum type, also called enumeration type, is a type whose fields consist of a
fixed set of constants. The purpose of using enum type is to enforce type safety.
While java.lang.Enum is an abstract class, it is the common base class of
all Java language enumeration types. The definition of Enum is:
public abstract class Enum>extends Objectimplements Comparable,
Serializable
All enum types implicitly extend java.lang.Enum.
public enum Season {
SPRING, SUMMER, AUTUM, WINTER
}
or
public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}
all the methods defined in the Enum class are available to all enum
types, such as the
String name() method, which returns the name of this enum constant
int ordinal() method, which returns the ordinal of this enumeration constant, starts
from 0

Core Java
Why Java is Platform Independent .?

Java is considered as Portable because,

Java is Considered as Platform independent because of many


different reasons which are listed below,

Output of a Java compiler is Non Executable Code i.e Bytecode.

Bytecode is a highly optimized set of instructions.

Bytecode is executed by Java run-time system, which is called the


Java Virtual Machine (JVM)

JVM is an interpreter

JVM accepts Bytecode as input and execute it.

Translating a Java program into bytecode makes it much easier to


run a program in a wide variety of environments because only the
JVM needs to be implemented for each platform.

For a given System we have Run-time package , once JVM is installed


for particular system then any java program can run on it.

Internal details of JVM will differ from platform to platform but still all understand
the Same Java Bytecode

Why Java Code is Safe ?

Java program is executed by the JVM.

The JVM prevent JAVA code from generating side effects outside of
the system.

Safety is also enhanced by certain restrictions that exist in the


Java language.

Core Java
Interpreter are slower than Compiler

Java Code is Executed by JVM (interpreter).Other programming language uses


compiler which can create executable code much faster then why we
are using Interpreter.

When a program is interpreted, it generally runs slower than the


same program would run if compiled to executable code.

In Java Compiler will generate ByteCode which is highly optimized.

Thus running highly optimized code using interpreter makes


execution of java program faster.

Core Java
What is the difference between JRE, JVM and JDK?

JDK (Java Development Kit)

Java Developer Kit contains tools needed to develop the Java programs, and JRE to run the
programs.

The tools include compiler (javac.exe), Java application launcher (java.exe), Appletviewer,
etc Compiler converts java code into byte code.

Java application launcher opens a JRE, loads the class, and invokes its main method.

You need JDK, if at all you want to write your own programs, and to compile.

For running java programs, JRE is sufficient.

JRE is targeted for execution of Java files i.e. JRE = JVM + Java Packages Classes(like util,
math, lang, awt,swing etc) + runtime libraries.

JDK is mainly targeted for java development. i.e. You can create a Java file (with the help of
Java packages), compile a Java file and run a java file.

JRE (Java Runtime Environment)

Java Runtime Environment contains JVM, class libraries, and other supporting files.

It does not contain any development tools such as compiler, debugger, etc.

Actually JVM runs the program, and it uses the class libraries, and other supporting files provided in JRE.

If you want to run any java program, you need to have JRE installed in the system

The Java Virtual Machine provides a platform-independent way of executing code;


programmers can concentrate on writing software, without having to be concerned with how

Core Java
or where it will run.

But, note that JVM itself not a platform independent. It only helps Java to be executed
on the platform-independent way.

When JVM has to interpret the byte codes to machine language, then it has to use some native
or operating system specific language to interact with the system.

One has to be very clear on platform independent concept. Even there are many JVMs written
on Java, however they too have little bit of code specific to the operating systems.

JVM (Java Virtual Machine)

As we all aware when we compile a Java file, output is not an exe but its a .class file.

.class file consists of Java byte codes which are understandable by JVM.

Java Virtual Machine interprets the byte code into the machine code depending upon the
underlying operating system and hardware combination.

It is responsible for all the things like garbage collection, array bounds checking, etc

JVM is platform dependent.

Core Java
Just-in-time Compiler (JIT)

JIT is the part of the Java Virtual Machine (JVM) that is used to speed up the
execution time.

JIT compiles parts of the byte code that have similar functionality at the same time,
and hence reduces the amount of time needed for compilation.

Here the term compiler refers to a translator from the instruction set of a
Java virtual machine (JVM) to the instruction set of a specific CPU.

public static void main(string args[]) Explanation.?


class JBT{
public static void main(String args[])
{
System.out.println("Hello JBT");
}
}

Public : It is an Access Modifier, which(AM) defines who can access this


method(In Java World). Public means that this method will be accessible to any
class(If other class can access this class.).
Static :

Keyword which identifies the class related this. It means that this class is

not instance related but class related. It can be accessed without creating the
instance of Class.
Static because it be available for execution without an object instance. you may
know that you need an object instance to invoke any method. So you cannot begin
execution of a class without its object if the main method was not static.
Void : Return Type, It defined what this method can return. Which is void in this
case it means that this method will not return any thing.
void because, once the main method execution is over, the program
terminates. So there can be no data that can be returned by the Main method.
main:

Name of the method.

This method name is searched by JVM as starting

point for an application.

As stated, main() is the method called when a Java application begins.

Keep in mind that Java is case-sensitive. Thus, Main is different from main.

It is important to understand that the Java compiler will compile classes that do

Core Java
not contain a main( ) method.

But the Java interpreter has no way to run these classes. So, if you had typed
Main instead of main, the compiler would still compile your program. However,
the Java interpreter would report an error because it would be unable to
find the main( ) method.

String args[] :

Parameter to main method. This is used to signify that the user

may opt to enter parameters to the java program at command line. We can use
both String[] args or String args[]. The Java compiler would accept both forms.
Definition Of main method in Java .?
The main method in Java belongs to a class but not to an Object. Objects are created at The
main() in Java is the start point in your application, there is no way to start your application from
an instance specific method. That is why the static keyword make perfect sense with the main
method. In fact all the parts of the main method declaration make perfect sense when you think like
the 'jvm' and picture what the main method does (starts the application):

public, because this method must be accessible by the jvm (not written by you).

static, implying this method can be accessed without having an object (because it's
representation never changes), but here the logic is easy understood if you think like the
jvm again; "I don't have any objects to create (instantiate) objects, so I need a static method
to start the application as there simple isn't any logical way to get an instance specific
method up yet as I don't have anything up yet to create objects".

Void This method can't logically return anything because there is nothing up yet to return
anything to. It is the start point of the application. main I am the main method as without me
you won't have an application.

String[] args Send me data you may feel useful for my startup.

*) File name and public class name should be same


example,
public class TestSample {

Core Java
public String toString() {
return "Test_Sample";
}

public static void main(String args []) {


System.out.println(new TestPublic().getValue());
}

File is saved as TestPublic.java


Note : It shows error like The public type TestSample must be defined in its own file
*) for example, the following two class files are saved as TestPublic.java
class TestPublicDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("sample");
}

TestPublicDemo.class

sample.class

}
class sample {

public static void main(String args []) {


System.out.println("sample");
}

*) main method can be overloaded


public class TestPublicDemo {
public static void main(String[] args) {
System.out.println("sample");
main(100);
System.out.println(main("praveen"));
}
public static void main(int a) {
System.out.println(a);
}

public static String main(String str) {


return str;
}

Output :
sample
100
praveen
The main thread of an application will always start at the method with the signature

Core Java
public static void main(String[] args)
and nothing else. For starting an application, JVM will understand the main method signature is
mentioned above and ignore all other main methods.

*) Can main method be overridden ? Nope


No. main is a static method, and is thus not polymorphic. You may hide it be defining another
static main method in a subclass, though.

class Super {
static String greeting() { return "Goodnight"; }
String name() { return "Richard"; }

}
class Sub extends Super {
static String greeting() { return "Hello"; }
String name() { return "Dick"; }
}
class Test {
public static void main(String[] args) {
Super s = new Sub();
System.out.println(s.greeting() + ", " + s.name());
}
}

output is Goodnight, Dick

Access modifiers
1. public
2. private
3. protected
4. default
Class Level

public, default only applicable in class level

final, abstract also applicable in class level

Example,
public class SampleTest {} public
class SampleTest {}

default

final class SampleTest {} default final


abstract final class SampleTest {} The class SampleTest can be either abstract or

final, not both

Core Java
abstract class SampleTest {} default abstract
public abstract class SampleTest {} public abstract
public final class SampleTest { }

public final

Inner Class Level

private, public, protected, default all are applicable in


InnerClass

final, abstract, static all are applicable in InnerClass

final and abstract does not comes together in InnerClass

static final, static abstract is applicable in InnerClass

Example,
private class InnerPrivate {}
public class InnerPublic {}
protected class InnerProtected {}
class InnerDefault {}
final class InnerFinal {}
abstract class InnerAbstract {}
static class InnerStatic {}
static final class InnerStaticFinal {}
static abstract class InnerStaticAbsrtact {}
Field level Access modifiers :
public class StaticTest {
int i = 100;
static String str = "Praveen";
private String s1 = "private string";
public String s2 = "public string";
protected String s3 = "protected String";
final String s4 = "final String";
String s5 = "default String";
// Method level and block level only default, final modifiers allowed for
declaring fields
static {
// static block
static String str = "first"; // shows error
System.out.println("static block is executed");// first
}

Core Java
{
}

// instance block
static String str = "instance string"; // shows error
System.out.println("instance block is executed");
// third

StaticTest() {
// constructor
static String str = "constructor string"; // shows error
System.out.println("constructor is executed"); //fourth
}
static void display() { // Method level only default, final modifiers
allowed for declaring fields
static String s6 = "static method field"; // shows error
private String s7 = "private string";
// shows error
public String s8 = "public string"; // shows error
protected String s9 = "protected String"; // shows error

String c = "static string test";


final String st = "static test";
System.out.println("same class static method");

void print() {
final int count = 100;
System.out.println("same class default method" + this.i + " " +
super.hashCode());
}
public static void main(String[] args) { // shows error
static String c = "test string"; static is not allowed here
method();
}
}

static keyword
The static keyword is used in JAVA mainly for memory management. We may apply static
keyword with variables, methods, blocks and nested class. The static keyword belongs to
the class than instance of the class.
The static can be:
1.variable (also known as class variable)
2.method (also known as class method)
3.block
4.nested class

1) static variable
If you declare any variable as static, it is known static
variable.
The static variable can be used to refer the common property of all objects (that is not unique for each object)

Core Java
e.g. company name of employees,college name of students etc.
The static variable gets memory only once in class area at the time of class
loading.

Advantage of static variable


It makes your program memory efficient (i.e it saves
memory).

Understanding problem without static variable


Class Student{
int rollno;
String name;
String college="ITS";
}

Suppose there are 500 students in my college, now all instance data members will get memory each
time when object is created.All student have its unique rollno and name so instance data member is
good.Here, college refers to the common property of all objects.If we make it static,this field will
get memory only once.
static property is shared to all objects.

Example of static variable


//<b>Programofstaticvariable</b>

class Student{
int rollno;
String name;
static String college="ITS";

Student(int r,String n){


rollno=r;
name=n;
}
voiddisplay(){System.out.println(rollno+""+name+""+college);}

public static void main(Stringargs[]){


Student s1=new Student(111,"Karan");
Student s2=new Student(222,"Aryan");

Core Java
s1.display();
s2.display();
}
}
Output:111 Karan ITS

222 Aryan ITS

Core Java

Program of counter without static variable


In this example, we have created an instance variable named count which is incremented
in the constructor. Since instance variable gets the memory at the time of object creation,
each object will have the copy of the instance variable, if it is incremented, it won't reflect
to other objects. So each objects will have the value 1 in the count variable.
classCounter{
intcount=0;//willgetmemorywheninstanceiscreated

Counter(){
count++;
System.out.println(count);
}

public static void main (String args []){

Counter c1=new Counter();

Core Java
Counter c2=new Counter();
Counter c3=new Counter();

}}
Output:1
1
1

Program of counter by static variable


As we have mentioned above, static variable will get the memory only once, if any
object changes the value of the static variable, it will retain its value.
classCounter{
staticintcount=0;//will get memory only once and retain its value

Counter(){
count++;
System.out.println(count);
}

publicstaticvoidmain(Stringargs[]){

Counterc1=newCounter();
Counterc2=newCounter();
Counterc3=newCounter();

}}
Output:1
2
3

2) static method
If you apply static keyword with any method, it is known as static
method.
A static method belongs to the class rather than object of a class.
A static method can be invoked without the need for creating an instance of a class.

Core Java
static method can access static data member and can change the value of it.

Example of static method


//Program

class

of

changing

the

common

property

of

all

objects(static

field).

Student{
int

rollno;

String

name;

static

String

static

void

college

college

"ITS";

change(){

"BBDIT";

Student(int

r,

rollno

r;

name

n;

String

n){

void

display

(){System.out.println(rollno+"

public

static

void

main(String

args[]){

Student.change();

Student

s1

new

Student

(111,"Karan");

Student

s2

new

Student

(222,"Aryan");

Student

s3

new

Student

(333,"Sonoo");

s1.display();
s2.display();
s3.display();
}
}
Output:111 Karan BBDIT
222 Aryan BBDIT
333 Sonoo BBDIT

"+name+"

"+college);}

Core Java

Another example of static method that performs normal


calculation
//Program

Class

to

get

cube

of

given

number

by

static

method

Calculate{

static

int

return

cube(int

x){

x*x*x;

public
int

static

void

main(String

args[]){

result=Calculate.cube(5);

System.out.println(result);
}
}
Output:125

Restrictions for static method


There are two main restrictions for the static method. They
are:
1. The static method can not use non static data member or call non-static
method directly.
2. this and super cannot be used in static context.
class
int

A{
a=40;//non

public

static

static

void

main(String

args[]){

System.out.println(a);
}
}
Output:Compile Time Error

Que)why main method is static?


Ans) because object is not required to call static method if it were non-static method, JVM
creates object first then call main() method that will lead the problem of extra

Core Java
memory allocation.

Example,
class Example {
String str = "Kumar";
static int amount = 1000;
void demo() {
System.out.println("default method");
}
public void print() {
System.out.println("public method");
}
static void display() {
System.out.println("static method");
}
}
public class StaticTest {
int i = 100;
static String str = "Praveen";
static void display() {
System.out.println("same class static method");
}
void print() {
System.out.println("same class default method" + this.i + " " +
super.hashCode());
Compiled Successfully
}
public static void method() {
// this and super cant be used in static method
System.out.println(this.i);
Compiler error
System.out.println(super.toString());
Compiler error
// Outer class member
Example.display();
System.out.println(Example.amount);
new Example().demo();
new Example().print();
System.out.println(new Example().str);
// Same class member
System.out.println(str);
// Directly we can access (advantage)
System.out.println(new StaticTest().i); // can access indirectly
display(); // Directly we can access (advantage)
new StaticTest().print(); // can access indirectly
}
public static void main(String[] args) {
method();
}

Core Java
}

Output :
static method
1000
default method
public method
Kumar
Praveen
100
same class static method
same class default method

3)static block
Is used to initialize the static data member.
It is executed before main method at the time of class loading.

Example of static block


class

A{

static{System.out.println("static

public

static

void

block

main(String

System.out.println("Hello

is

invoked");}

args[]){

main");

}
}
Output:static block is invoked
Hello main

Que)Can we execute a program without main() method?


Ans)Yes, one of the way is static block but in previous version of JDK not in JDK 1.7.
class

A{

static{
System.out.println("static
System.exit(0);
}
}

block

is

invoked");

Core Java
Output:static block is invoked (if not JDK7)

Order Of Execution
public class OrderOfExecution {
static {
// static block
System.out.println("static block is executed");// first
}
{
}

// instance block
System.out.println("instance block is executed");

// third

public OrderOfExecution() { // constructor


System.out.println("constructor is executed"); //fourth
}
public static void main(String[] args) { // main method

System.out.println("main block");
new OrderOfExecution();

//second

Output :
static block is executed
main block
instance block is executed
constructor is executed

StackOverflow error
Example,
public class OrderOfExecution {
static {
// static block
System.out.println("static block is executed");// first
new OrderOfExecution();
}
{
}

// instance block
System.out.println("instance block is executed");
new OrderOfExecution();

// third

OrderOfExecution() {
// constructor
System.out.println("constructor is executed"); //fourth
}
public static void main(String[] args) { // main method

}
}

System.out.println("main block");
new OrderOfExecution();

//second

Core Java
Output
instance block is executed
instance block is executed
instance block is executed
instance block is executed
instance block is executed
instance block is executed
instance block is executed
instance block is executed
instance block is executed
Exception in thread "main" java.lang.StackOverflowError
at sun.nio.cs.UTF_8.updatePositions(UTF_8.java:58)
at sun.nio.cs.UTF_8$Encoder.encodeArrayLoop(UTF_8.java:392)
at sun.nio.cs.UTF_8$Encoder.encodeLoop(UTF_8.java:447)
at java.nio.charset.CharsetEncoder.encode(CharsetEncoder.java:544)
at sun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:252)
at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:106)
at java.io.OutputStreamWriter.write(OutputStreamWriter.java:190)
at java.io.BufferedWriter.flushBuffer(BufferedWriter.java:111)
at java.io.PrintStream.write(PrintStream.java:476)
at java.io.PrintStream.print(PrintStream.java:619)
at java.io.PrintStream.println(PrintStream.java:756)

Java Static Variables

Java instance variables are given separate memory for storage. If there is a
need for a variable to be common to all the objects of a single java class, then the
static modifier should be used in the variable declaration.

Any java object that belongs to that class can modify its static variables.

Also, an instance is not a must to modify the static variable and it can be accessed
using the java class directly.

Static variables can be accessed by java instance methods also.

When the value of a constant is known at compile time it is declared final using
the static keyword.

Java Static Methods

Similar to static variables, java static methods are also common to classes and not
tied to a java instance.

Good practice in java is that, static methods should be invoked with using the class
name though it can be invoked using an object.
ClassName.methodName(arguments) or objectName.methodName(arguments)

General use for java static methods is to access static fields.

Static methods can be accessed by java instance methods.

Java static methods cannot access instance variables or instance methods


directly.

Core Java

Java static methods cannot use the this and 'super' keyword.

Java Static Classes

For java classes, only an inner class can be declared using the static modifier.

For java a static inner class it does not mean that, all their members are static.
These are called nested static classes in java.

Use of static inner class .?

Final Keyword in Java


1) final variable

If you make any variable as final, you cannot change the value of final variable(It will be
constant).

Final variable alone is not a constant it means just assign the value once it cant be re-assign

final with static form is considered a constant in java language specification.

Public static final String str = Praveen;

Final variables are by default read-only.

Example of final
variable
There is a final variable speedlimit, we are going to change the value of this variable, but It
can't be changed because final variable once assigned a value can never be changed.
class

Bike{

final

int

speedlimit=90;//final

void

run(){

variable

Core Java
speedlimit=400;
}

public
Bike

static

void

obj=new

main(String

args[]){

Bike();

obj.run();
}
Output:Compile Time Error

2) final method
If you make any method as final, you cannot override it.

Example of final method


class

Bike{

final

void

run(){System.out.println("running");}

class

Honda

void

extends

Bike{

run(){System.out.println("running

public

static

Honda

honda=

void
new

main(String

safely

args[]){

Honda();

honda.run();
}
}
Output:Compile Time Error

3) final class
If you make any class as final, you cannot extend it.

with

100kmph");}

Core Java

Example of final class Immutable class


final

class

class

Bike{}

Honda

void

extends

Bike{

run(){System.out.println("running

public

static

Honda

honda=

void

main(String

new

safely

with

100kmph");}

args[]){

Honda();

honda.run();
}
Output:Compile Time Error

Q) Is final method inherited?


Ans)Yes, final method is inherited but you cannot override it. For Example:
class

Bike{

final

void

run(){System.out.println("running...");}

}
class

Honda

public
new

extends

static

Bike{

void

main(String

args[]){

Honda().run();

}
}
Output:running...

Q) What is blank final variable?


A final variable that is not initialized at the time of declaration is known as blank
final variable. If you want to create a variable that is initialized at the time of creating
object and once initialized may not be changed, it is useful. For example PAN CARD number
of an employee. It can be initialized only in constructor.

Example of blank final variable


class

Student{

Core Java
int

id;

String
final

name;
String

PAN_CARD_NUMBER;

...
}

Que) Can we initialize blank final variable?


Yes, but only in constructor. For example:
class

Bike{

final

int

speedlimit;//blank

final

variable

Bike(){
speedlimit=70;
System.out.println(speedlimit);
}

public
new

Static

void

main(String

args[]){

Bike();

}
}
Output:70

static blank final variable


A static final variable that is not initialized at the time of declaration is known as
static blank final variable. It can be initialized only in static block.

Example of static blank final variable


class

A{

static

static{

public

final

int

data;//static

final

data=50;}

Static

void

main(String

System.out.println(A.data);
}

blank

args[]){

variable

Core Java
}

Q) What is final parameter?


If you declare any parameter as final, you cannot change the value of it.
class
int

Bike{
cube(final

int

n=n+2;//can't

be

n){
changed

as

is

final

n*n*n;
}

public
Bike

Static

void

b=new

main(String

args[]){

Bike();

b.cube(5);
}
}
Output:Compile Time Error

Q) Can we declare a constructor final?


No, because constructor is never inherited.

Benefits of final keyword in Java


Here are few benefits or advantage of using final

keyword

in

Java:

1. Final keyword improves performance. Not just JVM can cache


variable but also application can cache frequently use final variables.
2. Final variables are safe to share in
additional synchronization overhead.
3. Final keyword

allows

JVM

multi-threading

final

environment without

to optimized method, variable or

class.

Final and Immutable Class in Java

Final keyword helps to write immutable class. Immutable classes are the
one which can not be modified once it gets created and String is primary
example of immutable and final class which I have discussed in detail on
Why String is final or immutable in Java.

Core Java

Immutable classes offer several benefits one of them is that they are
effectively read-only and can be safely shared in between multiple
threads without any synchronization overhead.

You can not make a class immutable without making it final and hence final
keyword is required to make a class immutable in java.

Example of Final in Java


Java has several system classes in JDK which are final, some example of final
classes are String, Integer, Double and other wrapper classes. You can also use
final keyword to make your code better whenever it required.
Important points on final in Java
1. Final keyword can be applied to member variable, local variable, method or
class in Java.
2. Final member variable must be initialized at the time of declaration or
inside constructor, failure to do so will result in compilation error.
3. You can not reassign value to
4.

Local final variable

final variable in

Java.

must be initializing during declaration.

5. Only final variable is accessible inside anonymous


6.

Final method

7.

Final

8. Final
in Java.

class

can not be

overridden in Java.

can not be inheritable in

is different than

class in Java.

finally

Java.

keyword

which is used on

Exception handling

9. Final should not be confused with finalize() method which is declared in object
class and called before an object is garbage collected by JVM.
10. All variable declared inside
11. Final and abstract
abstract in java.

java

interface are implicitly final.

are two opposite

keyword

and a final

class

can not be

12. Final methods are bonded during compile time also called static binding.
13. Final variables which is not initialized during declaration are called blank final
variable and must be initialized on all constructor either explicitly or by calling this().
Failure to do so compiler will complain as "final variable (name) might not be
initialized".
14. Making a class, method or variable final in Java helps to improve performance
because JVM gets an opportunity to make assumption and optimization.

Core Java
15. As per Java code
written in all Caps e.g.

convention

final variables are treated as constant

private final int COUNT=10;


16. Making a collection reference variable final means only reference can not be
changed but you can add, remove or change object inside collection. For example:
private final List Loans = new ArrayList();
list.add(home loan);
// valid
list.add("personal loan"); // valid
loans = new Vector();
// not valid
public class FinalTest {
final String s = "Immutable";
final Integer count = 10;
static final Double d = 12.232;
static final List<String> str = new ArrayList<String>();
final List<String> demo = new ArrayList<String>();
public List<String> getList() {
//
//

s = "mutable";
count = 20;

// cant re-assign
// cant re-assign

demo = new ArrayList<String>(); // cant re-assign

str.add("A");
str.add("B");
str.add("C");
str.add("D");
return str;

/*for (String s : str) {


System.out.println(s);
}*/

public List<String> getChangedList() {


List<String> str = getList();
str.add("OOPS");
return str;
}
public static void main(String[] args) {
//

d = 23.3434;

// cant re-assign

List<String> str = new FinalTest().getChangedList();


for (String s : str) {
System.out.println(s);
}

and

Core Java
demo.addAll(str);

// cant re-assign

for(String s : demo) {
// cant re-assign
System.out.println(s);
}

Abstract class in Java


Abstraction is a process of hiding the implementation details and showing only
functionality to the user.
Another way, it shows only important things to the user and hides the internal details
for example sending sms, you just type the text and send the message. You don't know the
internal processing about the message delivery.
Abstraction lets you focus on what the object does instead of how it does it.

Ways to achieve Abstaction


There are two ways to achieve abstraction in java
1.Abstract class (0 to 100%)
2.Interface (100%)

Abstract class
A class that is declared as abstract is known as abstract class. It needs to be extended and
its method implemented. It cannot be instantiated.

Syntax to declare the abstract class


abstract class <class_name>{}

abstract method
A method that is declared as abstract and does not have implementation is known as
abstract method.

Syntax to define the abstract method


abstract return_type <method_name>();//no braces{}

Example of abstract class that have abstract method


In this example, Bike the abstract class that contains only one abstract method run. It
implementation is provided by the Honda class.
abstract class Bike{

Core Java
abstract void run();
}

class Honda extends Bike{


void run(){System.out.println("running safely..");}

public static void main(String args[]){


Bike obj = new Honda();
obj.run();
}
Output:running safely..

Understanding the real scenario of abstract class


In this example, Shape is the abstract class, its implementation is provided by the Rectangle
and Circle classes. Mostly, we don't know about the implementation class (i.e. hidden to the
end user) and object of the implementation class is provided by the factory method.
A factory method is the method that returns the instance of the class. We will learn about the
factory method later.
In this example, if you create the instance of Rectangle class, draw method of Rectangle class
will be invoked.
abstract class Shape{
abstract void draw();
}

class Rectangle extends Shape{


void draw(){System.out.println("drawing rectangle");}
}

class Circle extends Shape{


void draw(){System.out.println("drawing circle");}
}

class Test{
public static void main(String args[]){
Shape s=new Circle();
//In real scenario, Object is provided through factory method
s.draw();

Core Java
}
}
Output:drawing circle

Abstract class having constructor, data member, methods etc.


Note: An abstract class can have data member, abstract method,
method body, constructor and even main() method.
//example of abstract class that have method body

abstract class Bike{


abstract void run();
void changeGear(){System.out.println("gear changed");}
}

class Honda extends Bike{


void run(){System.out.println("running safely..");}

public static void main(String args[]){


Bike obj = new Honda();
obj.run();
obj.changeGear();
}
}
Output:running safely..
gear changed
//example of abstract class having constructor, field and method
abstract class Bike
{
int limit=30;
Bike(){System.out.println("constructor is invoked");}
void getDetails(){System.out.println("it has two wheels");}
abstract void run();
}

class Honda extends Bike{

Core Java
void run(){System.out.println("running safely..");}

public static void main(String args[]){


Bike obj = new Honda();
obj.run();
obj.getDetails();
System.out.println(obj.limit);
}
}
Output:constructor is invoked
running safely..
it has two wheels
30

Rule: If there is any abstract method in a class, that class must be


abstract.
class Bike{
abstract void run();
}
Output:compile time error

Rule: If you are extending any abstract class that have abstract method,
you must either provide the implementation of the method or make this
class abstract.

Another real scenario of abstract class


The abstract class can also be used to provide some implementation of the interface. In such
case, the end user may not be forced to override all the methods of the interface.
interface A{
void a();
void b();
void c();
void d();
}

abstract class B implements A{


public void c(){System.out.println("I am C");}
}

Core Java
class M extends B{
public void a(){System.out.println("I am a");}
public void b(){System.out.println("I am b");}
public void d(){System.out.println("I am d");}
}

class Test{
public static void main(String args[]){
A a=new M();
a.a();
a.b();
a.c();
a.d();
}}
Output:I am a
I am b
I am c
I am d

Example,
abstract class A {
static final String str = "Praveen";
final String s;
// private public protected default all r applicable
static final String s1;
abstract public void demo();
abstract protected void display();
abstract void cal();
public final void welcome() {
System.out.println("public final method in abstract class");
}
public void print() {
System.out.println("print method in abstract class");
}
public A() {
s = "blank final field initialization";
System.out.println("abstract class constructor");
}
{
System.out.println("abstract class instance block");
}
static {
s1 = "static blank field initialization";

Core Java
}

System.out.println("abstract class static block");

}
class C extends A {
public C() {
super();
System.out.println("C class constructor");
}
public C(String s) {
System.out.println("extend class Constructor with Argument " + s);
}
static {
System.out.println("C class static block");
}
{
}

field

System.out.println("C class instance block");

@Override
public void demo() {
System.out.println("overriden from A + " + str); // static final
}
@Override
protected void display() {
System.out.println("i am display + " + s); // final field
}
@Override
void cal() {
System.out.println("i am cal + " + s1); // static final field
}

}
public class AbstractTest {
public static void main(String[] args) {
//

A a1 = new A();
// abstract class cant be instantiated
A a = new C("Testing");
a.cal();
a.demo();
a.display();
a.print();
a.welcome();

Output :
abstract class
C class static
abstract class
abstract class

static block
block
instance block
constructor

Core Java
C class instance block
extend class Constructor with Argument Testing
i am cal + static blank field initialization
overriden from A + Praveen
i am display + blank final field initialization
print method in abstract class
public final method in abstract class

Abstract class & Interface with Object Slicing Concept


interface D {
// by default interface fields are public static final variable
String sample = "interface blank field";
static String test = "interface blank static final field";
// by default public abstract modifiers only permitted
void AA();
void BB();
void CC();
// The interface D cannot define an initializer either static or instance
block
/*static {
}
{
}*/
}
abstract class A implements D {
static final String str = "Praveen";
final String s;
// private public protected default all r applicable
static final String s1;
abstract public void demo();
abstract protected void display();
abstract void cal();
public final void welcome() {
System.out.println("public final method in abstract class");
}
public void print() {
System.out.println("print method in abstract class");
}
public A() {
s = "blank final field initialization";
System.out.println("abstract class constructor"); 4
}
{
System.out.println("abstract class instance block"); 3
}

Core Java
static {
s1 = "static blank field initialization";
System.out.println("abstract class static block"); 1
}
public void AA() {
System.out.println("interface AA method");
}
}
class C extends A {
public C() {
super();
System.out.println("C class constructor");
}
public C(String s) {
System.out.println("extend class Constructor with Argument " + s); 6
}
static {
System.out.println("C class static block"); 2
}
{
}

field

System.out.println("C class instance block"); 5

@Override
public void demo() {
System.out.println("overriden from A + " + str); // static final
}
@Override
protected void display() {
System.out.println("i am display + " + s); // final field
}
@Override
void cal() {
System.out.println("i am cal + " + s1); // static final field
}
@Override
public void BB() {
System.out.println("Interface BB method");
}
@Override
public void CC() {
System.out.println("Interface CC method");
}
public void DD() {
System.out.println("C class method");
}
public static void EE() {

Core Java
}

System.out.println("C class static method");

}
public class AbstractTest {
public static void main(String[] args) {
//

A a1 = new A();

// Object is created for "C" Class but the reference type is pointed to A
// So reference variable a is access only A and D type scope data members only

Object Slicing
A a = new C("Testing"); // Object Slicing happens here
a.cal();
a.demo();
a.display();
a.print();
a.welcome();
a.DD();
// shows error DD() is not comes under A type Scope

Object Slicing
// reference variable d access only D type scope data members only
D d = new C();
d.DD();
// shows error DD() is not comes under D type scope
d.cal();
// shows error DD() is not comes under D type scope
d.demo();
// shows error DD() is not comes under D type scope
d.AA();
d.BB();
d.CC();
System.out.println(D.sample);
System.out.println(D.test);

Object Slicing
// reference variable c access all A & D & C data members
C c = new C();
c.AA();
c.DD();
c.demo();
}
}

Output :
abstract class static block
C class static block
abstract class instance block
abstract class constructor
C class instance block
extend class Constructor with Argument Testing
i am cal + static blank field initialization
overriden from A + Praveen
i am display + blank final field initialization
print method in abstract class
public final method in abstract class

Core Java
abstract class instance block
abstract class constructor
C class instance block
C class constructor
interface AA method
Interface BB method
Interface CC method
interface blank field
interface blank static final field
abstract class instance block
abstract class constructor
C class instance block
C class constructor
interface AA method
C class method
overriden from A + Praveen
We Can define abstract keyword in class without declaring abstract method
Example,
abstract class E {
public void AAA() {
System.out.println("AAA method in E");
}
public void BBB() {
System.out.println("BBB method in E");
}
}

Note :

But it act like a normal concrete class

The meaning of abstract keyword here is useless

Interface

An interface is a blueprint of a class. It has static constants and abstract methods.

The interface is a mechanism to achieve 100% fully abstraction in java. There


can be only abstract methods in the interface.

It is used to achieve fully abstraction and multiple inheritance in Java.

Interface also represents IS-A relationship.

It cannot be instantiated just like abstract class.

Why use Interface?


There are mainly three reasons to use interface. They are given below.

Core Java
It is used to achieve fully abstraction.
By interface, we can support the functionality of multiple
inheritance.
It can be used to achieve loose coupling.

The java compiler adds public and abstract keywords before the interface
method and public, static and final keywords before data
members.
In other words, Interface fields are public, static and final by default, and methods are public
and abstract

Understanding relationship between classes and interfaces


As shown in the figure given below, a class extends another class, an interface extends
another interface but a class implements an interface.

Core Java

Simple example of Interface


In this example, Printable interface have only one method, its implementation is provided
in the A class.
interface printable{
void print();
}

class A implements printable{


public void print(){System.out.println("Hello");}

public static void main(String args[]){


A obj = new A();
obj.print();
}
}
Output:Hello

Multiple inheritance in Java by interface


If a class implements multiple interfaces, or an interface extends multiple interfaces i.e. known as multiple
inheritance.

Core Java

interface Printable{
void print();
}

interface Showable{
void show();
}

class A implements Printable,Showable{

public void print(){System.out.println("Hello");}


public void show(){System.out.println("Welcome");}

public static void main(String args[]){


A obj = new A();
obj.print();
obj.show();
}
}
Output:Hello
Welcome

Core Java
Q) Multiple inheritance is not supported in case of class but it is supported in case of
interface, why?
As we have explained in the inheritance chapter, multiple inheritance is not supported
in case of class. But it is supported in case of interface because there is no ambiguity
as implementation is provided by the implementation class. For example:
interface Printable{
void print();

same method

interface Showable{
void print();

same method

class A implements Printable,Showable{

public void print(){System.out.println("Hello");}

public static void main(String args[]){


A obj = new A();
obj.print();
}
}
Output:Hello
As you can see in the above example, Printable and Showable interface have same
methods but its implementation is provided by class A, so there is no ambiguity.

Note: A class implements interface but One interface extends another


interface .
interface Printable{
void print();
}

interface Showable extends Printable{


void show();
}

Core Java
class A implements Showable{

public void print(){System.out.println("Hello");}


public void show(){System.out.println("Welcome");}

public static void main(String args[]){


A obj = new A();
obj.print();
obj.show();
}
}
Output:Hello
Welcome

Que) What is marker or tagged interface ?


An interface that have no member is known as marker or tagged interface. For example:
Serializable, Cloneable, Remote etc. They are used to provide some essential
information to the JVM so that JVM may perform some useful operation.
//How Serializable interface is written?
public interface Serializable{
}

Nested Interface

An interface which is declared within another interface or class is known as


nested interface.

The nested interfaces are used to group related interfaces so that they can be easy
to maintain.

The nested interface must be referred by the outer interface or class. It can't be
accessed directly.

Points to remember for nested interfaces


There are given some points that should be remembered by the java programmer.
Nested interface must be public if it is declared inside the interface but it

Core Java
can have any access modifier if declared within the class.
Nested interfaces are declared static implicitly.

Syntax of nested interface which is declared within the interface


interface interface_name{
...
interface nested_interface_name{ // Only public static modifier
...
}
}

Syntax of nested interface which is declared within the class


class class_name{
...
interface nested_interface_name{ // it can have any modifiers like inner class
...
}
}

Example of nested interface which is declared within the interface


In this example, we are going to learn how to declare the nested interface and how we can
access it.
interface Showable{
void show();
interface Message{
void msg();
}
}

class Test implements Showable.Message{


public void msg(){System.out.println("Hello nested interface");}

public static void main(String args[]){


Showable.Message message=new Test();//upcasting here
message.msg();

Core Java
}
}
Output:hello nested interface
As you can see in the above example, we are accessing the Message interface by its outer
interface Showable because it cannot be accessed directly. It is just like almirah inside the
room, we cannot access the almirah directly because we must enter the room first. In
collection framework, sun microsystem has provided a nested interface Entry.
Entry is the subinterface of Map i.e. accessed by Map.Entry.

Internal code generated by the java compiler for nested interface Message
The java compiler internally creates public and static interface as displayed
below:.
public static interface Showable$Message
{
public abstract void msg();
}

Example of nested interface which is declared within the class


Let's see how can we define an interface inside the class and how can we
access it.
class A{
interface Message{
void msg();
}
}

class Test implements A.Message{


public void msg(){System.out.println("Hello nested interface");}

public static void main(String args[]){


A.Message message=new Test();//upcasting here
message.msg();
}
}
Output:hello nested interface

Core Java
Can we define a class inside the interface ?
Yes, Of course! If we define a class inside the interface, java compiler creates a static
nested class. Let's see how can we define a class within the interface:
interface M{
class A{}
}
Example ,
interface AA {
void demo();
void display();
void print();
}
interface BB extends AA {
void cal();
void printValue();
}
// Nested Class
interface CC {
String str = "must be initialized";
void sample();
void print();
class Start {
// by default compiler treat as static class
public void start() {
System.out.println("i am start method inside interface");
}
}
}
// Nested Interfaces
interface DD {
void sample();
public interface EE { // public and no-modifier only allowed nested
interface inside interface by default compiler treat as public static interface
void color();
}
}
class YYY extends CC.Start implements DD, DD.EE {
String str = "Praveen";
static String s = "Kumar";
@Override
public void sample() {
System.out.println("I am sample from DD type");

Core Java
}
@Override
public void color() {
System.out.println("I am white color");
}
static interface FF {
// private, public, protected, no-modifier, static
all are applicable nested interface inside class
void logo();
}
static interface GG {
}
}
class XXX extends YYY implements BB, CC, YYY.FF{
public void demo() {
System.out.println("i am demo");
}
public void display() {
System.out.println("i am display");
}
//
//
//

public void print() {


System.out.println("i am print");
}
public void cal() {
System.out.println("i am cal");
}
public void printValue() {
System.out.println("i am printValue");
}
public void welcome() {
System.out.println("I am welcome");
}
@Override
public void sample() {
System.out.println("I am sample");
}
@Override
public void print() {
System.out.println("I am print");
}
@Override
public void logo() {
System.out.println("I am nested interface logo");
}

}
public class InterfaceTest {

Core Java
public static void main(String[] args) {
System.out.println("It Comes under AA type Scope");
AA a = new XXX();
a.demo();
a.display();
a.print();
System.out.println("It Comes under BB type Scope");
BB b = new XXX();
b.cal();
b.demo();
b.display();
b.print();
b.printValue();
System.out.println("It Comes under XXX type Scope");
XXX x = new XXX();
x.cal();
x.demo();
x.display();
x.print();
x.printValue();
x.welcome();
x.logo();
System.out.println("It Comes under CC type Scope");
CC c = new XXX();
c.sample();
c.print();
System.out.println(CC.str);
System.out.println("It Comes under DD type Scope");
DD d = new YYY();
d.sample();
System.out.println("It Comes under DD.EE type Scope");
DD.EE e = new YYY();
// Nested Interfaces instantiation
e.color();
System.out.println("It Comes under YYY type Scope");
YYY y = new XXX();
y.color();
y.sample();
System.out.println("It Comes under DD type Scope");
DD d1 = new XXX();
d1.sample();
System.out.println("It Comes under DD.EE type Scope");
DD.EE e1 = new XXX();
e1.color();
System.out.println("It Comes under YYY.FF type Scope");
YYY.FF f = new XXX();
f.logo();
System.out.println("It Comes under CC.Start type Scope");
CC.Start s = new XXX();
s.start();

Core Java
}

Output
It Comes under AA type Scope
i am demo
i am display
I am print
It Comes under BB type Scope
i am cal
i am demo
i am display
I am print
i am printValue
It Comes under XXX type Scope
i am cal
i am demo
i am display
I am print
i am printValue
I am welcome
I am nested interface logo
It Comes under CC type Scope
I am sample
I am print
must be initialized
It Comes under DD type Scope
I am sample from DD type
It Comes under DD.EE type Scope
I am white color
It Comes under YYY type Scope
I am white color
I am sample
It Comes under DD type Scope
I am sample
It Comes under DD.EE type Scope
I am white color
It Comes under YYY.FF type Scope
I am nested interface logo
It Comes under CC.Start type Scope
i am start method inside interface

Abstract interface in Java

Core Java
When you're defining an interface in Java, it's always abstract, though you don't need to declare them as
such.
So there's no difference between
abstract interface I
{
void method();
}
....and...
interface I
{
void method();
}

Whats the difference between an interface and an abstract class in


Java?
Its best to start answering this question with a brief definition of abstract classes and interfaces and then
explore the differences between the two.
A class must be declared abstract when it has one or more abstract methods. A method is declared
abstract when it has a method heading, but no body which means that an abstract method has no
implementation code inside curly braces like normal methods do.

When to use abstract methods in Java?


Why you would want to declare a method as abstract is best illustrated by an example. Take a look at the
code below:

/* the Figure class must be declared as abstract


because it contains an abstract method

*/

public abstract class Figure


{
/* because this is an abstract method the
body will be blank

*/

public abstract float getArea();

Core Java

public class Circle extends Figure


{
private float radius;

public float getArea()


{
return (3.14 * (radius * 2));
}
}

public class Rectangle extends Figure


{
private float length, width;

public float getArea(Figure other)


{
return length * width;
}
}

In the Figure class above, we have an abstract method called getArea(), and because the Figure class
contains an abstract method the entire Figure class itself must be declared abstract. The Figure base
class has two classes which derive from it called Circle and Rectangle. Both the Circle and Rectangle
classes provide definitions for the getArea method, as you can see in the code above.

Core Java
But the real question is why did we declare the getArea method to be abstract in the Figure class? Well,
what does the getArea method do? It returns the area of a specific shape. But, because the Figure class
isnt a specific shape (like a Circle or a Rectangle), theres really no definition we can give the getArea
method inside the Figure class. Thats why we declare the method and the Figure class to be abstract.
Any classes that derive from the Figure class basically has 2 options:
1. The derived class must provide a definition for the getArea method OR
2. The derived class must be declared abstract itself.

A non abstract class is called a concrete class


You should also know that any non abstract class is called a concrete class.

Java interface versus abstract class


An interface differs from an abstract class because an interface is not a class. An interface is essentially a
type that can be satisfied by any class that implements the interface.
Any class that implements an interface must satisfy 2 conditions:

It must have the phrase "implements Interface_Name" at the beginning of the class definition.

It must implement all of the method headings listed in the interface definition.

This is what an interface called "Dog" would look like:

public interface Dog


{
public boolean Barks();

public boolean isGoldenRetriever();


}

Now, if a class were to implement this interface, this is what it would look like:

public class SomeClass implements Dog


{
public boolean Barks{

Core Java

// method definition here

public boolean isGoldenRetriever{


// method definition here
}
}

Now that we know the basics of interfaces and abstract classes, lets get to the heart of the question and
explore the differences between the two. Here are the three major differences:

Abstract classes and inheritance


1. Abstract classes are meant to be inherited from, and when one class inherits from another it means
that there is a strong relationship between the 2 classes. For instance, if we have an abstract base class
called "Canine", any deriving class should be an animal that belongs to the Canine family (like a Dog or a
Wolf). The reason we use the word "should" is because it is up to the Java developer to ensure that
relationship is maintained.

With an interface on the other hand, the relationship between the interface itself and the class
implementing the interface is not necessarily strong. For example, if we have a class called "House", that
class could also implement an interface called "AirConditioning". Having air conditioning not really an
essential part of a House (although some may argue that point), and the relationship is not as strong as,
say, the relationship between a TownHouse class and the "House" class or the relationship between an
Apartment class that derives from a House class.
Because a TownHouse is a type of House, that relationship is very strong, and would be more
appropriately defined through inheritance instead of interfaces.
So, we can summarize this first point by saying that an abstract class would be more appropriate when
there is a strong relationship between the abstract class and the classes that will derive from it. Again,
this is because an abstract class is very closely linked to inheritance, which implies a strong relationship.
But, with interfaces there need not be a strong relationship between the interface and the classes that
implement the interface.

Core Java

Interfaces are a good substitute for multiple inheritance


2. Java does not allow multiple inheritance see the discussion on Java Multiple Inheritance if you need a
refresher on this. In Java, a class can only derive from one class, whether its abstract or not. However, a
class can implement multiple interfaces which could be considered as an alternative to for multiple
inheritance. So, one major difference is that a Java class can inherit from only one abstract class, but can
implement multiple interfaces.

Abstract classes can have some implementation code


3. An abstract class may provide some methods with definitions so an abstract class can have
non-abstract methods with actual implementation details. An abstract class can also have constructors
and instance variables as well. An interface, however, can not provide any method definitions it can
only provide method headings. Any class that implements the interface is responsible for providing the
method definition/implementation.

When to use abstract class and interface in Java


Here are some guidelines on when to use an abstract class and when to use interfaces in Java:

An abstract class is good if you think you will plan on using inheritance since it
provides a common base class implementation to derived classes.

An abstract class is also good if you want to be able to declare non-public members. In
an interface, all methods must be public.

If you think you will need to add methods in the future, then an abstract class is a
better choice. Because if you add new method headings to an interface, then all of the
classes that already implement that interface will have to be changed to implement the
new methods. That can be quite a hassle.

Interfaces are a good choice when you think that the API will not change for a while.

Interfaces are also good when you want to have something similar to multiple
inheritance, since you can implement multiple interfaces.

Access Modifiers
There are two types of modifiers in java: access modifier and non-access modifier. The
access modifiers specifies accessibility (scope) of a data member, method, constructor or class.
There are 4 types of access modifiers:
1.private
2.default
3.protected

Core Java
4.public
There are many non-access modifiers such as static, abstract, synchronized, native, volatile,
transient etc. Here, we will learn access modifiers.

1) private
The private access modifier is accessible only within
class.

Simple example of private access modifier


In this example, we have created two classes A and Simple. A class contains private data
member and private method. We are accessing these private members from outside the
class, so there is compile time error.
class A{
private int data=40;
private void msg(){System.out.println("Hello java");}
}

public class Simple{


public static void main(String args[]){
A obj=new A();
System.out.println(obj.data);//Compile Time Error
obj.msg();//Compile Time Error
}
}

Role of Private Constructor:


If you make any class constructor private, you cannot create the instance of that class
from outside the class. For example:
class A{
private A(){}//private constructor

void msg(){System.out.println("Hello java");}


}

public class Simple{


public static void main(String args[]){
A obj=new A();//Compile Time Error

Core Java
}
}

Note: A class cannot be private or protected except nested class.

2) default
If you don't use any modifier, it is treated as default bydefault. The default modifier is
accessible only within package.

Example of default access modifier


In this example, we have created two packages pack and mypack. We are accessing the A
class from outside its package, since A class is not public, so it cannot be accessed from
outside the package.
//save by A.java

package pack;
class A{
void msg(){System.out.println("Hello");}
}
//save by B.java

package mypack;
import pack.*;

class B{
public static void main(String args[]){
A obj = new A();//Compile Time Error
obj.msg();//Compile Time Error
}
}
In the above example, the scope of class A and its method msg() is default so it cannot be
accessed from outside the package.

3) protected
The protected access modifier is accessible within package and outside the package but

Core Java
through inheritance only.
The protected access modifier can be applied on the data member, method and constructor. It
can't be applied on the class.

Example of protected access modifier


In this example, we have created the two packages pack and mypack. The A class of pack
package is public, so can be accessed from outside the package. But msg method of this
package is declared as protected, so it can be accessed from outside the class only through
inheritance.
//save by A.java

package pack;
public class A{
protected void msg(){System.out.println("Hello");}
}
//save by B.java

package mypack;
import pack.*;

class B extends A{
public static void main(String args[]){
B obj = new B();
obj.msg();
}
}
Output:Hello

4) public
The public access modifier is accessible everywhere. It has the widest scope among all
other modiers.

Example of public access modifier


//save by A.java

package pack;
public class A{

Core Java
public void msg(){System.out.println("Hello");}
}
//save by B.java

package mypack;
import pack.*;

class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
Output:Hello

Understanding all java access modifiers


Let's understand the access modifiers by a simple table.
Access Modifier

Private
Default

Protected
Public

Visibility
priority

within class within package

Low
Next to
private
Next to
default
high

outside package by subclass only

outside package

Applying access modifier with method overriding


If you are overriding any method, overridden method (i.e. declared in subclass)
must not be more restrictive.
class A{
protected void msg(){System.out.println("Hello java");}
}

public class Simple extends A{


void msg(){System.out.println("Hello java");}//C.T.Error
public static void main(String args[]){

Core Java
Simple obj=new Simple();
obj.msg();
}
}
The default modifier is more restrictive than protected. That is why there is compile time
error.

Overriding Run Time Polymorphism


Rules
1) Check the visibility of the overriding super class method
2) If suppose super class method has default modifier, the overridden method (ie
sub class overridden method) may have any one of the modifier like
default/protected/public modifier.
3) If suppose overriding method has protected, the overridden method should be
protected / public
4) Should mention the higher visibility of subclass overridden method

With Same Package


package com.core.demo;
class QQQ {
public void first() {
System.out.println("I am first");
}
void second() {
System.out.println("I am second");
}
protected void third() {
System.out.println("I am third");
}
}
public class OveridingTest extends QQQ {
//
void first() {
from QQQ
//
//
}

//Cannot reduce the visibility of the inherited method

public void first() {


System.out.println("I am first from subclass");
}
protected void second() {
System.out.println("I am second from subclass");
}
public void third() {

Core Java
}

System.out.println("I am third from subclass");

void fourth() {
System.out.println("I am fourth");
}
public static void main(String[] args) {
QQQ q = new OveridingTest();
q.first();
q.second();
q.third();
q.fourth(); // this method is not comes under the scope of QQQ type
QQQ q1 = new QQQ();
q1.first();
q1.second();
q1.third();
q1.fourth(); // Obviously its not possible cant access subclass
method by using super class object references
}

Output
I am first from subclass
I am second from subclass
I am third from subclass
I am first
I am second
I am third
Difference Package
package com.core.basics;
import com.core.demo.OveridingTest;
class HHH extends OveridingTest {
void demo() {
System.out.println("I am demo from HHH");
}
public void display() {
System.out.println("I am display from HHH");
}
protected void print() {
System.out.println("I am print from HHH");
}
// The method HHH.fourth() does not override the inherited method
// from OveridingTest since it is private to a different package
void fourth() {
System.out.println("I am fourth from outside package overriden");

Core Java
}
public void first() {
System.out.println("I am first from outside package overriden");
}
public void second() {
System.out.println("I am second from outside package overriden");
}

class TTT extends HHH {


void color() {
System.out.println("I am color from TTT");
}
protected void print() {
System.out.println("I am print overriden from HHH");
}
public void demo() {
System.out.println("I am demo overriden from HHH");
}
}
public class OverridenDemoOutside {
public static void main(String[] args) {
HHH h = new TTT();
h.demo();
h.display();
h.first();
h.fourth();
h.print();
h.second();
h.third();
}

Output
I
I
I
I
I
I
I

am
am
am
am
am
am
am

demo overriden from HHH


display from HHH
first from outside package overriden
fourth from outside package overriden
print overriden from HHH
second from outside package overriden
third from subclass

Simple Example
package com.core.demo;
class Animal {
void eat() {
System.out.println("eating");
}

Core Java
}
class Dog extends Animal {
void eat() {
System.out.println("eating fruits");
}
void run() {
System.out.println("Dog runs Fast");
}
}
class BabyDog extends Dog {
void eat() {
System.out.println("drinking milk");
}
void run() {
System.out.println("BabyDog runs slow");
}
void size() {
System.out.println("small");
}
}
public class TestOveride {
public static void main(String[] args) {
Animal a1, a2, a3;
a1 = new Animal();
a2 = new Dog();
a3 = new BabyDog();
a1.eat();
a2.eat();
a3.eat();

// Object Slicing
// Object Slicing
// Object Slicing

// Overridden method will be called


// Overridden method will be called

Dog d;
d = new BabyDog();
// Object Slicing
d.eat();
d.run();
// Overridden method will be called
}

Output
eating
eating fruits
drinking milk
drinking milk
BabyDog runs slow

Runtime Polymorphism
Runtime polymorphism or Dynamic Method Dispatch is a process in which a call to an

Core Java
overridden method is resolved at runtime rather than compile-time.
In this process, an overridden method is called through the reference variable of a superclass.
The determination of the method to be called is based on the object being referred to by the
reference variable.
Let's first understand the upcasting before Runtime Polymorphism.

Upcasting
When reference variable of Parent class refers to the object of Child class, it is known as
upcasting. For example:

class A{}
class B extends A{}
A a=new B();//upcasting

Example of Runtime Polymorphism


In this example, we are creating two classes Bike and Splendar. Splendar class extends Bike
class and overrides its run() method. We are calling the run method by the reference variable
of Parent class. Since it refers to the subclass object and subclass method overrides the Parent
class method, subclass method is invoked at runtime.
Since method invocation is determined by the JVM not compiler, it is known as
runtime polymorphism.
class Bike{
void run(){System.out.println("running");}
}
class Splender extends Bike{
void run(){System.out.println("running safely with 60km");}

public static void main(String args[]){


Bike b = new Splender();//upcasting
b.run();
}
}

Core Java
Output: running safely with 60km.

Runtime Polymorphism with data member


Method is overriden not the datamembers, so runtime polymorphism can't be
achieved by data members.
In the example given below, both the classes have a datamember speedlimit, we are
accessing the datamember by the reference variable of Parent class which refers to the
subclass object. Since we are accessing the datamember which is not overridden, hence it will
access the datamember of Parent class always.

Rule: Runtime polymorphism can't be achieved by data members.


class Bike{
int speedlimit=90;
}
class Honda extends Bike{
int speedlimit=150;

public static void main(String args[]){


Bike obj=new Honda();
System.out.println(obj.speedlimit);//90
}
Output: 90

Runtime Polymorphism with Multilevel Inheritance


Let's see the simple example of Runtime Polymorphism with multilevel inheritance.
class Animal{
void eat(){System.out.println("eating");}
}

class Dog extends Animal{


void eat(){System.out.println("eating fruits");}
}

class BabyDog extends Dog{


void eat(){System.out.println("drinking milk");}

public static void main(String args[]){

Core Java
Animal a1,a2,a3;
a1=new Animal();
a2=new Dog();
a3=new BabyDog();

a1.eat();
a2.eat();
a3.eat();
}
}
Output: eating
eating fruits
drinking Milk

Try for Output


class Animal{
void eat(){System.out.println("animal is eating...");}
}

class Dog extends Animal{


void eat(){System.out.println("dog is eating...");}
}

class BabyDog extends Dog{


public static void main(String args[]){
Animal a=new BabyDog();
a.eat();
}}
Output: Dog is eating

Since, BabyDog is not overriding the eat() method, so eat() method of Dog class is invoked.

Method Overloading in Java


If a class have multiple methods by same name but different parameters, it is known
as Method Overloading.
If we have to perform only one operation, having same name of the methods increases the
readability of the program.

Core Java
Suppose you have to perform addition of the given numbers but there can be any number of
arguments, if you write the method such as a(int,int) for two parameters, and b(int,int,int) for
three parameters then it may be difficult for you as well as other programmers to understand
the behaviour of the method because its name differs. So, we perform method overloading to
figure out the program quickly.

Advantage of method overloading?


Method overloading increases the readability of the program.

Different ways to overload the method


There are two ways to overload the method in
java
1. By changing number of
arguments
2. By changing the data type

In java, Methood Overloading is not possible by changing the return type of the
method.

1)Example of Method Overloading by changing the no. of arguments


In this example, we have created two overloaded methods, first sum method performs
addition of two numbers and second sum method performs addition of three numbers.
class Calculation{
void sum(int a,int b){System.out.println(a+b);}
void sum(int a,int b,int c){System.out.println(a+b+c);}

public static void main(String args[]){


Calculation obj=new Calculation();
obj.sum(10,10,10);
obj.sum(20,20);

}
}
Output:30
40

2)Example of Method Overloading by changing data type of argument


In this example, we have created two overloaded methods that differs in data type. The

Core Java
first sum method receives two integer arguments and second sum method receives two
double arguments.
class Calculation{
void sum(int a,int b){System.out.println(a+b);}
void sum(double a,double b){System.out.println(a+b);}

public static void main(String args[]){


Calculation obj=new Calculation();
obj.sum(10.5,10.5);
obj.sum(20,20);

}
1. }
Output:21.0
40

Que) Why Method Overloading is not possible by changing the


return type of method?
In java, method overloading is not possible by changing the return type of the method
because there may occur ambiguity. Let's see how ambiguity may occur:
because there was problem:
class Calculation{
int sum(int a,int b){System.out.println(a+b);}
double sum(int a,int b){System.out.println(a+b);}

public static void main(String args[]){


Calculation obj=new Calculation();
int result=obj.sum(20,20); //Compile Time Error

}
}

int result=obj.sum(20,20); //Here how can java determine which sum() method should be called

Can we overload main() method?


Yes, by method overloading. You can have any number of main methods in a class by method
overloading. Let's see the simple example:
class Simple{

Core Java
public static void main(int a){
System.out.println(a);
}

public static void main(String args[]){


System.out.println("main() method invoked");
main(10);
}
}
Output:main() method invoked
10

Method Overloading and Type Promotion


One type is promoted to another implicitly if no matching data-type is found. Let's
understand the concept by the figure given below:

As displayed in the above diagram, byte can be promoted to short, int, long, float or
double. The short datatype can be promoted to int,long,float or double. The char datatype
can be promoted to int,long,float or double and so on.

Core Java
Example of Method Overloading with TypePromotion
class Calculation{
void sum(int a,long b){System.out.println(a+b);}
void sum(int a,int b,int c){System.out.println(a+b+c);}

public static void main(String args[]){


Calculation obj=new Calculation();
obj.sum(20,20);//now second int literal will be promoted to long
obj.sum(20,20,20);

}
}
Output:40
60

Example of Method Overloading with TypePromotion if matching found


If there are matching type arguments in the method, type promotion is not
performed.
class Calculation{
void sum(int a,int b){System.out.println("int arg method invoked");}
void sum(long a,long b){System.out.println("long arg method invoked");}

public static void main(String args[]){


Calculation obj=new Calculation();
obj.sum(20,20);//now int arg sum() method gets invoked
}
}
Output:int arg method invoked

Example of Method Overloading with TypePromotion in case ambiguity


If there are no matching type arguments in the method, and each method promotes similar
number of arguments, there will be ambiguity.
class Calculation{
void sum(int a,long b){System.out.println("a method invoked");}
void sum(long a,int b){System.out.println("b method invoked");}

Core Java
public static void main(String args[]){
Calculation obj=new Calculation();
obj.sum(20,20);//now ambiguity
}
}
Output:Compile Time Error

One type is not DE-promoted implicitly for example double cannot be


DE-promoted to any type implicitly.

Simple Example,
public class MethodOverloading {
public void sum() {
System.out.println("with out args");
}
public void sum(int a) {
System.out.println("with one arg");
}
public void sum(long a) {
System.out.println("with long arg");
}
public int sum(double a) {
return 10;
}
public void sum(int a, long b) {
System.out.println("2 args");
}
public void sum(long a, int b) {
System.out.println("2 args");
}
public static void main(String[] args) {
MethodOverloading obj = new MethodOverloading();
System.out.println(obj.sum(12.2323));
obj.sum(10, 10); // The method sum(int, long) is ambiguous for the
type MethodOverloading
obj.sum(10);
obj.sum();
}
}

Output

Core Java
10
with one arg
with out args

String Handling
Generally string is a sequence of characters. But in java, string is an object. String class is
used to create string object.

How to create String object?


There are two ways to create String
object:
1. By string literal
2. By new keyword

1) String literal
String literal is created by double quote. For
Example:
String s="Hello";
Each time you create a string literal, the JVM checks the string constant pool first.
If the string already exists in the pool, a reference to the pooled instance returns.
If the string does not exist in the pool, a new String object instantiates, then is
placed in the pool. For example:
String s1="Welcome";
String s2="Welcome";//no new object will be created

Core Java

In the above example only one object will be created. First time JVM will find no string
object with the name "Welcome" in string constant pool, so it will create a new object.
Second time it will find the string with the name "Welcome" in string constant pool,so it will
not create new object whether will return the reference to the same instance.
Note: String objects are stored in a special memory area known as string constant
pool inside the Heap memory.

Why java uses concept of string literal?


To make Java more memory efficient (because no new objects are created if it
exists already in string constant pool).

2) By new keyword
String s=new String("Welcome");//creates two objects and one reference variable
In such case, JVM will create a new String object in normal(nonpool) Heap memory and
the literal "Welcome" will be placed in the string constant pool. The variable s will refer to
the object in Heap(nonpool).

Immutable String:
In java, strings are immutable (unmodifiable) objects. For
example
class Simple{
public static void main(String args[]){
String s="Sachin";
s.concat(" Tendulkar");//concat() method appends the string at the end
System.out.println(s);//will print Sachin because strings are immutable objects
}
}
Output:Sachin

Core Java

As you can see in the above figure that two objects will be created but no reference variable
refers to "Sachin Tendulkar".But if we explicitly assign it to the reference variable, it will
refer to "Sachin Tendulkar" object.For example:
class Simple{
public static void main(String args[]){
String s="Sachin";
s=s.concat(" Tendulkar");
System.out.println(s);
}
}
Output:Sachin Tendulkar

Why string objects are immutable in java?


Because java uses the concept of string literal. Suppose there are 5 reference variables,all
refers to one object "sachin".If one reference variable changes the value of the object, it will

Core Java
be affected to all the reference variables. That is why string objects are immutable in java.

String comparison in Java

We can compare two given on the basis of content and reference. It is used in authentication
(equals() method), sorting (compareTo() method) etc.
There are three ways to compare String objects:
1.By equals() method
2.By = = operator
3.By compareTo() method

1) By equals() method:
equals() method compares the original content of the string. It compares values of
string for equality. String class provides two methods:

public boolean equals(Object another){} compares this string to the specified


object.

public boolean equalsIgnoreCase(String another){} compares this String to


another String, ignoring case.

//<b><i>Example of equals(Object) method</i></b>

class Simple{
public static void main(String args[]){
String s1="Sachin";
String s2="Sachin";

Core Java
String s3=new String("Sachin");
String s4="Saurav";
System.out.println(s1.equals(s2));//true
System.out.println(s1.equals(s3));//true
System.out.println(s1.equals(s4));//false
}
}
Output:true
true
false
//<b><i>Example of equalsIgnoreCase(String) method</i></b>

class Simple{
public static void main(String args[]){

String s1="Sachin";
String s2="SACHIN";

System.out.println(s1.equals(s2));//false
System.out.println(s1.equalsIgnoreCase(s2));//true
}
}
Output:false
true

2) By == operator:
The = = operator compares references
not values.
//<b><i>Example of == operator</i></b>

class Simple{
public static void main(String args[]){

String s1="Sachin";
String s2="Sachin";
String s3=new String("Sachin");

Core Java
System.out.println(s1==s2);//true (because both refer to same instance)
System.out.println(s1==s3);//false(because s3 refers to instance created in nonpool)
}
}
Output:true
false

3) By compareTo() method:
compareTo() method compares values and returns an int which tells if the values
compare less than, equal, or greater than.
Suppose s1 and s2 are two string variables.If:
s1 == s2 :0
s1 > s2 :positive value
s1 < s2 :negative value
//<b><i>Example of compareTo() method:</i></b>

class Simple{
public static void main(String args[]){

String s1="Sachin";
String s2="Sachin";
String s3="Ratan";

System.out.println(s1.compareTo(s2));//0
System.out.println(s1.compareTo(s3));//1(because s1>s3)
System.out.println(s3.compareTo(s1));//-1(because s3 < s1 )
}
}
Output:0
1
-1

String Concatenation in Java

Core Java
Concating strings form a new string i.e. the combination of multiple strings.
There are two ways to concat string objects:
1.By + (string concatenation) operator
2.By concat() method

1) By + (string concatenation) operator


String concatenation operator is used to add strings.For
Example:
//Example of string concatenation operator

class Simple{
public static void main(String args[]){

String s="Sachin"+" Tendulkar";


System.out.println(s);//Sachin Tendulkar
}
}
Output:Sachin Tendulkar
The compiler transforms this to:
String s=(new StringBuilder()).append("Sachin").append(" Tendulkar").toString();
String concatenation is implemented through the StringBuilder(or StringBuffer) class
and its append method. String concatenation operator produces a new string by appending
the second operand onto the end of the first operand. The string concatenation operator can
concat not only string but primitive values also. For Example:
class Simple{
public static void main(String args[]){

String s=50+30+"Sachin"+40+40;
System.out.println(s);//80Sachin4040
}
}
Output:80Sachin4040

Core Java
Note:If either operand is a string, the resulting operation will be string concatenation. If
both operands are numbers, the operator will perform an addition.

2) By concat() method
concat() method concatenates the specified string to the end of
current string.
Syntax:public String concat(String another){}
//<b><i>Example of concat(String) method</i></b>

class Simple{
public static void main(String args[]){

String s1="Sachin ";


String s2="Tendulkar";

String s3=s1.concat(s2);

System.out.println(s3);//Sachin Tendulkar
}
}
Output:Sachin Tendulkar

Substring in Java

A part of string is called substring. In other words, substring is a subset of another string.
In case of substring startIndex starts from 0 and endIndex starts from 1 or

Core Java
startIndex is inclusive and endIndex is exclusive.
You can get substring from the given String object by one of the two methods:
1.public String substring(int startIndex): This method returns new String object
containing the substring of the given string from specified startIndex (inclusive).
2.public String substring(int startIndex,int endIndex): This method returns new
String object containing the substring of the given string from specified startIndex to
endIndex.

In case of string:
startIndex: starts from index
0(inclusive).
endIndex: starts from index
1(exclusive).

Example of java substring


//Example of substring() method

class Simple{
public static void main(String args[]){

String s="Sachin Tendulkar";


System.out.println(s.substring(6));//Tendulkar
System.out.println(s.substring(0,6));//Sachin
}
}
Output:Tendulkar
Sachin

Methods of String class


java.lang.String class provides a lot of methods to work on string. Let's see the commonly
used methods of String class.
Method

1)public boolean equals(Object


anObject)

Description

Compares this string to the specified object.

Core Java
2)public boolean

Compares this String to another String,

equalsIgnoreCase(String another)

ignoring case.

3)public String concat(String str)

Concatenates the specified string to the end of


this string.

4)public int compareTo(String str)

Compares two strings and returns int

5)public int

Compares two strings, ignoring case

compareToIgnoreCase(String str)

differences.

6)public String substring(int

Returns a new string that is a substring of this

beginIndex)

string.

7)public String substring(int

Returns a new string that is a substring of this

beginIndex,int endIndex)

string.

8)public String toUpperCase()

9)public String toLowerCase()

10)public String trim()

Converts all of the characters in this String to


upper case
Converts all of the characters in this String to
lower case.
Returns a copy of the string, with leading and
trailing whitespace omitted.

11)public boolean startsWith(String

Tests if this string starts with the specified

prefix)

prefix.

12)public boolean endsWith(String


suffix)

Tests if this string ends with the specified suffix.

13)public char charAt(int index)

Returns the char value at the specified index.

14)public int length()

Returns the length of this string.

15)public String intern()

Returns a canonical representation for the string


object.

First seven methods have already been discussed. Now Let's take the example of other
methods:

toUpperCase() and toLowerCase() method


//<b><i>Example of toUpperCase() and toLowerCase() method</i></b>

class Simple{
public static void main(String args[]){

Core Java
String s="Sachin";
System.out.println(s.toUpperCase());//SACHIN
System.out.println(s.toLowerCase());//sachin
System.out.println(s);//Sachin(no change in original)
}
}
Output:SACHIN
sachin
Sachin

trim() method
//<b><i>Example of trim() method</i></b>

class Simple{
public static void main(String args[]){

String s=" Sachin ";


System.out.println(s);// Sachin
System.out.println(s.trim());//Sachin
}
}
Output: Sachin
Sachin

startsWith() and endsWith() method


//<b><i>Example of startsWith() and endsWith() method</i></b>

class Simple{
public static void main(String args[]){

String s="Sachin";
System.out.println(s.startsWith("Sa"));//true
System.out.println(s.startsWith("n"));//false
}

Core Java
}
Output:true
false

charAt() method
//<b><i>Example of charAt() method</i></b>

class Simple{
public static void main(String args[]){

String s="Sachin";
System.out.println(s.charAt(0));//S
System.out.println(s.charAt(3));//h
}
}
Output:S
h

length() method
//<b><i>Example of length() method</i></b>

class Simple{
public static void main(String args[]){

String s="Sachin";
System.out.println(s.length());//6
}
}
Output:6

intern() method
A pool of strings, initially empty, is maintained privately by the class String.
When the intern method is invoked, if the pool already contains a string equal to this String
object as determined by the equals(Object) method, then the string from the pool is returned.

Core Java
Otherwise, this String object is added to the pool and a reference to this String object is
returned.
//<b><i>Example of length() method</i></b>

class Simple{
public static void main(String args[]){

String s=new String("Sachin");


String s2=s.intern();
System.out.println(s2);//Sachin
}
}
Output:Sachin

StringBuffer class:
The StringBuffer class is used to created mutable (modifiable) string. The StringBuffer class
is same as String except it is mutable i.e. it can be changed.

Note: StringBuffer class is thread-safe i.e. multiple threads


cannot access it simultaneously .So it is safe and will result in an
order.
Commonly used Constructors of StringBuffer class:
1. StringBuffer(): creates an empty string buffer with the initial capacity of 16.
2. StringBuffer(String str): creates a string buffer with the specified string.
3. StringBuffer(int capacity): creates an empty string buffer with the specified
capacity as length.

Commonly used methods of StringBuffer class:


1. public synchronized StringBuffer append(String s): is used to append the specified
string with this string. The append() method is overloaded like append(char),
append(boolean), append(int), append(float), append(double) etc.

2. public synchronized StringBuffer insert(int offset, String s): is used to insert the
specified string with this string at the specified position. The insert() method is
overloaded like insert(int, char), insert(int, boolean), insert(int, int), insert(int,
float), insert(int, double) etc.

Core Java
3. public synchronized StringBuffer replace(int startIndex, int endIndex, String
str): is used to replace the string from specified startIndex and endIndex.

4. public synchronized StringBuffer delete(int startIndex, int endIndex): is used to


delete the string from specified startIndex and endIndex.

5. public synchronized StringBuffer reverse(): is used to reverse the string.


6. public int capacity(): is used to return the current capacity.
7. public void ensureCapacity(int minimumCapacity): is used to ensure the
capacity at least equal to the given minimum.

8. public char charAt(int index): is used to return the character at the specified
position.

9. public int length(): is used to return the length of the string i.e. total number of
characters.

10.

public String substring(int beginIndex): is used to return the substring from

the specified beginIndex.


11.

public String substring(int beginIndex, int endIndex): is used to return

the substring from the specified beginIndex and endIndex.

What is mutable string?


A string that can be modified or changed is known as mutable string. StringBuffer
and StringBuilder classes are used for creating mutable string.

simple example of StringBuffer class by append() method


The append() method concatenates the given argument with this
string.
1. class A{
2. public static void main(String args[]){

3.
4. StringBuffer sb=new StringBuffer("Hello ");
5. sb.append("Java");//now original string is changed

6.
7. System.out.println(sb);//prints Hello Java
8. }
9. }

Core Java

Example of insert() method of StringBuffer class


The insert() method inserts the given string with this string at the given
position.
1. class A{
2. public static void main(String args[]){

3.
4. StringBuffer sb=new StringBuffer("Hello ");
5. sb.insert(1,"Java");//now original string is changed

6.
7. System.out.println(sb);//prints HJavaello
8. }
9. }

Example of replace() method of StringBuffer class


The replace() method replaces the given string from the specified beginIndex and
endIndex.
1. class A{
2. public static void main(String args[]){

3.
4. StringBuffer sb=new StringBuffer("Hello");
5. sb.replace(1,3,"Java");

6.
7. System.out.println(sb);//prints HJavalo
8. }
9. }

Example of delete() method of StringBuffer class


The delete() method of StringBuffer class deletes the string from the specified beginIndex
to endIndex.
1. class A{
2. public static void main(String args[]){

3.
4. StringBuffer sb=new StringBuffer("Hello");

Core Java
5. sb.delete(1,3);

6.
7. System.out.println(sb);//prints Hlo
8. }
9. }

Example of reverse() method of StringBuffer class


The reverse() method of StringBuilder class reverses the current
string.
1. class A{
2. public static void main(String args[]){

3.
4. StringBuffer sb=new StringBuffer("Hello");
5. sb.reverse();

6.
7. System.out.println(sb);//prints olleH
8. }
9. }

Example of capacity() method of StringBuffer class


The capacity() method of StringBuffer class returns the current capacity of the buffer. The
default capacity of the buffer is 16. If the number of character increases from its current
capacity, it increases the capacity by (oldcapacity*2)+2. For example if your current
capacity is 16, it will be (16*2)+2=34.
1. class A{
2. public static void main(String args[]){

3.
4. StringBuffer sb=new StringBuffer();
5. System.out.println(sb.capacity());//default 16

6.
7. sb.append("Hello");
8. System.out.println(sb.capacity());//now 16

9.
10.

sb.append("java is my favourite language");

Core Java
11.

System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2

12.

13.

Example of ensureCapacity() method of StringBuffer class


The ensureCapacity() method of StringBuffer class ensures that the given capacity is the
minimum to the current capacity. If it is greater than the current capacity, it increases the
capacity by (oldcapacity*2)+2. For example if your current capacity is 16, it will be
(16*2)+2=34.
1. class A{
2. public static void main(String args[]){

3.
4. StringBuffer sb=new StringBuffer();
5. System.out.println(sb.capacity());//default 16

6.
7. sb.append("Hello");
8. System.out.println(sb.capacity());//now 16

9.
10.

sb.append("java is my favourite language");

11.

System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2

12.
13.

sb.ensureCapacity(10);//now no change

14.

System.out.println(sb.capacity());//now 34

15.
16.

sb.ensureCapacity(50);//now (34*2)+2

17.

System.out.println(sb.capacity());//now 70

18.
19.

20.

StringBuilder class:
The StringBuilder class is used to create mutable (modifiable) string. The StringBuilder class
is same as StringBuffer class except that it is non-synchronized. It is available since JDK1.5.

Core Java

Commonly used Constructors of StringBuilder class:


1. StringBuilder(): creates an empty string Builder with the initial capacity of 16.
2. StringBuilder(String str): creates a string Builder with the specified string.
3. StringBuilder(int length): creates an empty string Builder with the specified
capacity as length.

Commonly used methods of StringBuilder class:


1. public StringBuilder append(String s): is used to append the specified string with this
string. The append() method is overloaded like append(char), append(boolean),
append(int), append(float), append(double) etc.

2. public StringBuilder insert(int offset, String s): is used to insert the specified string
with this string at the specified position. The insert() method is overloaded like
insert(int, char), insert(int, boolean), insert(int, int), insert(int, float), insert(int,
double) etc.

3. public StringBuilder replace(int startIndex, int endIndex, String str): is used to


replace the string from specified startIndex and endIndex.

4. public StringBuilder delete(int startIndex, int endIndex): is used to delete the string
from specified startIndex and endIndex.

5. public StringBuilder reverse(): is used to reverse the string.


6. public int capacity(): is used to return the current capacity.
7. public void ensureCapacity(int minimumCapacity): is used to ensure the
capacity at least equal to the given minimum.

8. public char charAt(int index): is used to return the character at the specified
position.

9. public int length(): is used to return the length of the string i.e. total number of
characters.

10.

public String substring(int beginIndex): is used to return the substring from

the specified beginIndex.


11.

public String substring(int beginIndex, int endIndex): is used to return

the substring from the specified beginIndex and endIndex.

Core Java

simple program of StringBuilder class by append() method


The append() method concatenates the given argument with this
string.
1. class A{
2. public static void main(String args[]){

3.
4. StringBuilder sb=new StringBuilder("Hello ");
5. sb.append("Java");//now original string is changed

6.
7. System.out.println(sb);//prints Hello Java
8. }
9. }

Example of insert() method of StringBuilder class


The insert() method inserts the given string with this string at the given
position.
1. class A{
2. public static void main(String args[]){

3.
4. StringBuilder sb=new StringBuilder("Hello ");
5. sb.insert(1,"Java");//now original string is changed

6.
7. System.out.println(sb);//prints HJavaello
8. }
9. }

Example of replace() method of StringBuilder class


The replace() method replaces the given string from the specified beginIndex and
endIndex.
1. class A{
2. public static void main(String args[]){

3.
4. StringBuilder sb=new StringBuilder("Hello");
5. sb.replace(1,3,"Java");

6.
7. System.out.println(sb);//prints HJavalo

Core Java
8. }
9. }

Example of delete() method of StringBuilder class


The delete() method of StringBuilder class deletes the string from the specified beginIndex
to endIndex.
1. class A{
2. public static void main(String args[]){

3.
4. StringBuilder sb=new StringBuilder("Hello");
5. sb.delete(1,3);

6.
7. System.out.println(sb);//prints Hlo
8. }
9. }

Example of reverse() method of StringBuilder class


The reverse() method of StringBuilder class reverses the current
string.
1. class A{
2. public static void main(String args[]){

3.
4. StringBuilder sb=new StringBuilder("Hello");
5. sb.reverse();

6.
7. System.out.println(sb);//prints olleH
8. }
9. }

Example of capacity() method of StringBuilder class


The capacity() method of StringBuilder class returns the current capacity of the Builder. The
default capacity of the Builder is 16. If the number of character increases from its current
capacity, it increases the capacity by (oldcapacity*2)+2. For example if your current
capacity is 16, it will be (16*2)+2=34.
1. class A{

Core Java
2. public static void main(String args[]){

3.
4. StringBuilder sb=new StringBuilder();
5. System.out.println(sb.capacity());//default 16

6.
7. sb.append("Hello");
8. System.out.println(sb.capacity());//now 16

9.
10.

sb.append("java is my favourite language");

11.

System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2

12.

13.

Example of ensureCapacity() method of StringBuilder class


The ensureCapacity() method of StringBuilder class ensures that the given capacity is the
minimum to the current capacity. If it is greater than the current capacity, it increases the
capacity by (oldcapacity*2)+2. For example if your current capacity is 16, it will be
(16*2)+2=34.
1. class A{
2. public static void main(String args[]){

3.
4. StringBuilder sb=new StringBuilder();
5. System.out.println(sb.capacity());//default 16

6.
7. sb.append("Hello");
8. System.out.println(sb.capacity());//now 16

9.
10.

sb.append("java is my favourite language");

11.

System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2

12.
13.

sb.ensureCapacity(10);//now no change

14.

System.out.println(sb.capacity());//now 34

15.
16.

sb.ensureCapacity(50);//now (34*2)+2

17.

System.out.println(sb.capacity());//now 70

18.

Core Java
19.

20.

How to create Immutable class?


There are many immutable classes like String, Boolean, Byte, Short, Integer, Long,
Float, Double etc. In short, all the wrapper classes and String class is immutable.
We can also create immutable class by creating final class that have final data members as
the example given below:

Example to create Immutable class


In this example, we have created a final class named Employee. It have one final
datamember, a parameterized constructor and getter method.
1. public final class Employee{
2. final String pancardNumber;

3.
4. public Employee(String pancardNumber){
5. this.pancardNumber=pancardNumber;
6. }

7.
8. public String getPancardNumber(){
9. return pancardNumber;
10.

11.
12.

The above class is immutable because:


The instance variable of the class is final i.e. we cannot change the value of
it after creating an object.
The class is final so we cannot create the subclass.
There is no setter methods i.e. we have no option to change the value of the
instance variable.
These points makes this class as immutable.

Core Java

Understanding toString() method


If you want to represent any object as a string, toString() method comes into existence.
The toString() method returns the string representation of the object.
If you print any object, java compiler internally invokes the toString() method on the
object. So overriding the toString() method, returns the desired output, it can be the state of
an object etc. depends on your implementation.

Advantage of the toString() method


By overriding the toString() method of the Object class, we can return values of
the object, so we don't need to write much code.

Understanding problem without toString() method


Let's see the simple code that prints reference.
class Student{
int rollno;
String name;
String city;

Student(int rollno, String name, String city){


this.rollno=rollno;
this.name=name;
this.city=city;
}

public static void main(String args[]){


Student s1=new Student(101,"Raj","lucknow");
Student s2=new Student(102,"Vijay","ghaziabad");

System.out.println(s1);//compiler writes here s1.toString()


System.out.println(s2);//compiler writes here s2.toString()
}
}
Output: Student@1fee6fc
Student@1eed786
As you can see in the above example, printing s1 and s2 prints the hashcode values
of the objects but I want to print the values of these objects. Since java compiler

Core Java
internally calls toString() method, overriding this method will return the specified values.

Example of toString() method


Now let's see the real example of toString() method.
class Student{
int rollno;
String name;
String city;

Student(int rollno, String name, String city){


this.rollno=rollno;
this.name=name;
this.city=city;
}

public String toString(){//overriding the toString() method


return rollno+" "+name+" "+city;
}
public static void main(String args[]){
Student s1=new Student(101,"Raj","lucknow");
Student s2=new Student(102,"Vijay","ghaziabad");

System.out.println(s1);//compiler writes here s1.toString()


System.out.println(s2);//compiler writes here s2.toString()
}
}
Output: 101 Raj lucknow
102 Vijay ghaziabad

StringTokenizer in Java
The java.util.StringTokenizer class allows you to break a string into tokens. It is simple way
to break string.
It doesn't provide the facility to differentiate numbers, quoted strings, identifiers etc. like
StreamTokenizer class. We will discuss about the StreamTokenizer class in I/O chapter.

Constructors of StringTokenizer class


There are 3 constructors defined in the StringTokenizer class.

Core Java
Constructor

Description

StringTokenizer(String

creates StringTokenizer with specified string.

str)
StringTokenizer(String

creates StringTokenizer with specified string and delimeter.

str, String delim)


StringTokenizer(String
str, String delim,
boolean returnValue)

creates StringTokenizer with specified string, delimeter and


returnValue. If return value is true, delimiter characters are
considered to be tokens. If it is false, delimiter characters serve
to separate tokens.

Methods of StringTokenizer class


The 6 useful methods of StringTokenizer class are as follows:
Public method

Description

boolean

checks if there is more tokens available.

hasMoreTokens()

returns the next token from the StringTokenizer

String nextToken()

object.

String nextToken(String

returns the next token based on the delimeter.

delim)
boolean

same as hasMoreTokens() method.

hasMoreElements()
Object nextElement()
int countTokens()

same as nextToken() but its return type is Object.


returns the total number of tokens.

Simple example of StringTokenizer class


Let's see the simple example of StringTokenizer class that tokenizes a string "my name is
khan" on the basis of whitespace.
import java.util.StringTokenizer;
public class Simple{
public static void main(String args[]){
StringTokenizer st = new StringTokenizer("my name is khan"," ");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
}

Core Java
}
Output:my
name
is
khan

Example of nextToken(String delim) method of StringTokenizer class


import java.util.*;

public class Test {


public static void main(String[] args) {
StringTokenizer st = new StringTokenizer("my,name,is,khan");

// printing next token


System.out.println("Next token is : " + st.nextToken(","));
}
}
Output:Next token is : my

StringTokenizer class is deprecated now. It is recommended to use


split() method of String class or regex (Regular Expression).

Generics in Java
The Java Generics programming is introduced in J2SE 5 to deal with type-safe objects.
Before generics, we can store any type of objects in collection i.e. non-generic. Now generics,
forces the java programmer to store specific type of objects.

Advantage of Java Generics


There are mainly 3 advantages of generics. They are as follows:
1) Type-safety : We can hold only a single type of objects in generics. It doesnt allow to
store other objects.
2) Type casting is not required: There is no need to typecast the object.
Before Generics, we need to type cast.
List list = new ArrayList();
list.add("hello");
String s = (String) list.get(0);//typecasting

Core Java
After Generics, we don't need to typecast the object.
List<String> list = new ArrayList<String>();
list.add("hello");
String s = list.get(0);

3) Compile-Time Checking: It is checked at compile time so problem will not occur at


runtime. The good programming strategy says it is far better to handle the problem at compile
time than runtime.
1. List<String> list = new ArrayList<String>();
2. list.add("hello");
3. list.add(32);//Compile Time Error

Syntax to use generic collection


1. ClassOrInterface<Type>
Simple example to use Generics
1. ArrayList<String>

Full Example of Java Generics


Here, we are using the ArrayList class, but you can use any collection class such as ArrayList,
LinkedList, HashSet, TreeSet, HashMap, Comparator etc.
1. import java.util.*;
2. class Simple{
3. public static void main(String args[]){
4. ArrayList<String> list=new ArrayList<String>();
5. list.add("rahul");
6. list.add("jai");
7. //list.add(32);//compile time error

8.
9. String s=list.get(1);//type casting is not required
10.

System.out.println("element is: "+s);

11.
12.

Iterator<String> itr=list.iterator();

13.

while(itr.hasNext()){

14.

System.out.println(itr.next());

15.

16.

Core Java
17.

Output:element is: jai


rahul
jai

Example of Java Generics using Map


Now we are going to use map elements using generics. Here, we need to pass key and value.
Let us understand it by a simple example:
1. import java.util.*;
2. class Test{
3. public static void main(String args[]){
4. Map<Integer,String> map=new HashMap<Integer,String>();
5. map.put(1,"vijay");
6. map.put(4,"umesh");
7. map.put(2,"ankit");

8.
9. //Now use Map.Entry for Set and Iterator
10.

Set<Map.Entry<Integer,String>> set=map.entrySet();

11.
12.

Iterator<Map.Entry<Integer,String>> itr=set.iterator();

13.

while(itr.hasNext()){

14.

Map.Entry e=itr.next();//no need to typecast

15.

System.out.println(e.getKey()+" "+e.getValue());

16.

17.
18.

}}

Output:1 vijay
2 ankit
4 umesh

Generic class
A class that can refer to any type is known as generic class. Here, we are using T type
parameter to create the generic class of specific type.
Lets see the simple example to create and use the generic class.
Creating generic class:

Core Java
1. class MyGen<T>{
2. T obj;
3. void add(T obj){this.obj=obj;}
4. T get(){return obj;}
5. }
The T type indicates that it can refer to any type (like String, Integer, Employee etc.). The type
you specify for the class, will be used to store and retrieve the data.
Using generic class:
Lets see the code to use the generic class.
1. class UseGen{
2. public static void main(String args[]){
3. MyGen<Integer> m=new MyGen<Integer>();
4. m.add(2);
5. //m.add("vivek");//Compile time error
6. System.out.println(m.get());
7. }}
Output:2

Understanding Type Parameters


The type parameters naming conventions are important to learn generics thoroughly. The
commonly type parameters are as follows:

1.T - Type
2.E - Element
3.K - Key
4.N - Number
5.V - Value
6.S,U,V etc. - 2nd, 3rd, 4th types

Generic Method
Like generic class, we can create generic method that can accept any type of
argument.

Core Java
Lets see a simple example of java generic method to print array elements. We are using here
E to denote the element.
1. public class GenericMethodDemo{

2.
3.

public static < E > void printArray(E[] elements) {

4.

for ( E element : elements){

5.

System.out.println(element );

6.

7.

System.out.println();

8.

9.

public static void main( String args[] ) {

10.

Integer[] intArray = { 10, 20, 30, 40, 50 };

11.

Character[] charArray = { 'J', 'A', 'V', 'A', 'T','P','O','I','N','T' };

12.
13.

System.out.println( "Printing Integer Array" );

14.

printArray( intArray );

15.
16.

System.out.println( "Printing Character Array" );

17.

printArray( charArray );

18.
19.

}
}

Output:Printing Integer Array


10
20
30
40
50
Printing Character Array
J
A
V
A
T
P
O

Core Java
I
N
T
Simple Example
// Generic Class
class MyGen<T> {
T obj;
void add(T obj) {
this.obj = obj;
}
T get() {
return obj;
}
}
public class GenericTest {
// Generic Method
public static <E> void display(E[] elements) {
for (E e : elements) {
System.out.println(e);
}
}
// Generic Method
public static <E> void removeDuplicates(List<E> list) {
// REmove duplicate
Set<E> set = new HashSet<E>(list);
List<E> lis = new ArrayList<E>(set);
for (E element : lis) {
System.out.println(element);
}
}
public static void main(String[] args) {
// Type safety: The method add(Object) belongs to the raw type List.
// References to generic type List<E> should be parameterized
List li = new ArrayList();
li.add("A");
li.add('B');
li.add(32);
li.add(12.23);
for (Object obj : li) {
System.out.println(obj);
}
List<String> list = new ArrayList<String>();
// list.add(1); // Compile Time Checking and shows error

Core Java
list.add("A");
list.add("B");
list.add("B");
for (String str : list) {
System.out.println(str);
}
Integer[] intArray = {12, 23, 34};
String[] strArray = {"A", "G", "E"};

display(intArray);
removeDuplicates(list);
MyGen<String> a = new MyGen<String>();
a.add("Welcome");
System.out.println(a.get());
}
}

Output
A
B
32
12.23
A
B
B
12
23
34
A
B
Welcome

Static Import:

The static import feature of Java 5 facilitate the java programmer to access any
static member of a class directly.

There is no need to qualify it by the class name.

Advantage of static import:


Less coding is required if you have access any static member of a class
oftenly.

Core Java

Disadvantage of static import:


If you overuse the static import feature, it makes the program unreadable and
unmaintainable.

Simple Example of static import


import static java.lang.System.*;
class StaticImportExample{
public static void main(String args[]){

out.println("Hello");//Now no need of System.out


out.println("Java");

}
}

Output:Hello
Java

What is the difference between import and static import?

The import allows the java programmer to access classes of a package without
package qualification whereas the static import feature allows to access the static
members of a class without the class qualification.

The import provides accessibility to classes and interface whereas static import
provides accessibility to static members of the class.

Enum
An enum is a data type which contains fixed set of constants. It can be used for days
of the week (SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY and
SATURDAY) , directions (NORTH, SOUTH, EAST and WEST) etc. The enum constants are
static and final implicitly. It is available from Java 5. Enums can be thought of as classes
that have fixed set of constants.

Points to remember for Enum:


1. enum improves type safety

Core Java
2. enum can be easily used in switch
3. enum can be traversed
4. enum can have fields, constructors and methods
5. enum may implement many interfaces but cannot extend any class because it
internally extends Enum class

Simple Example of enum in java:


1.
2. class EnumExample1{

3.
4. public enum Season { WINTER, SPRING, SUMMER, FALL }

5.
6. public static void main(String[] args) {
7. for (Season s : Season.values())
8. System.out.println(s);

9.
10.

}}

11.
Output:WINTER
SPRING
SUMMER
FALL

What is the purpose of values() method in enum?


The java compiler internally adds the values() method when it creates an enum.
The values() method returns an array containing all the values of the enum.

Internal code generated by the compiler for the above


example of enum type
The java compiler internally creates a static and final class that extends the Enum class as
shown in the below example:
1. public static final class EnumExample1$Season extends Enum
2. {

3. private EnumExample1$Season(String s, int i)


4.
5.

{
super(s, i);

Core Java
6.

7.
8.

public static EnumExample1$Season[] values()

9.

10.

return (EnumExample1$Season[])$VALUES.clone();

11.

12.
13.

public static EnumExample1$Season valueOf(String s)

14.

15.

return (EnumExample1$Season)Enum.valueOf(EnumExample1$Season, s);

16.

17.
18.

public static final EnumExample1$Season WINTER;

19.

public static final EnumExample1$Season SPRING;

20.

public static final EnumExample1$Season SUMMER;

21.

public static final EnumExample1$Season FALL;

22.

private static final EnumExample1$Season $VALUES[];

23.
24.

static

25.

26.

WINTER = new EnumExample1$Season("WINTER", 0);

27.

SPRING = new EnumExample1$Season("SPRING", 1);

28.

SUMMER = new EnumExample1$Season("SUMMER", 2);

29.

FALL = new EnumExample1$Season("FALL", 3);

30.

$VALUES = (new EnumExample1$Season[] {

31.

WINTER, SPRING, SUMMER, FALL

32.

});

33.

34.
35.

Defining enum:
The enum can be defined within or outside the class because it is

Core Java
similar to a class.

Example of enum that is defined outside the class:


1.
2. enum Season { WINTER, SPRING, SUMMER, FALL }

3.
4. class EnumExample2{
5. public static void main(String[] args) {

6.
7. Season s=Season.WINTER;
8. System.out.println(s);

9.
10.

}}

11.
Output:WINTER

Example of enum that is defined within the class:


1. class EnumExample2{
2. enum Season { WINTER, SPRING, SUMMER, FALL; }//semicolon(;) is optional here

3.
4. public static void main(String[] args) {
5. Season s=Season.WINTER;//enum type is required to access WINTER
6. System.out.println(s);

7.
8. }}

9.
Output:WINTER

Initializing specific value to the enum constants:


The enum constants have initial value that starts from 0, 1, 2, 3 and so on. But we can
initialize the specific value to the enum constants by defining fields and constructors. As
specified earlier, Enum can have fields, constructors and methods.

Example of specifying initial value to the enum constants


1. class EnumExample4{
2. enum Season{

Core Java
3. WINTER(5), SPRING(10), SUMMER(15), FALL(20);

4.
5. private int value;
6. private Season(int value){
7. this.value=value;
8. }
9. }
10.

public static void main(String args[]){

11.

for (Season s : Season.values())

12.

System.out.println(s+" "+s.value);

13.
14.

}}

15.
Output:WINTER 5
SPRING 10
SUMMER 15
FALL 20

Constructor of enum type is private if you don't declare private


compiler internally have private constructor
1. enum Season{
2. WINTER(10),SUMMER(20);
3. private int value;
4. Season(int value){
5. this.value=value;
6. }
7. }

8.

Internal code generated by the compiler for the above


example of enum type
1. final class Season extends Enum
2. {

3.
4.

public static Season[] values()

5.

Core Java
6.

return (Season[])$VALUES.clone();

7.

8.
9.

public static Season valueOf(String s)

10.

11.

return (Season)Enum.valueOf(Season, s);

12.

13.
14.

private Season(String s, int i, int j)

15.

16.

super(s, i);

17.

value = j;

18.

19.
20.

public static final Season WINTER;

21.

public static final Season SUMMER;

22.

private int value;

23.

private static final Season $VALUES[];

24.
25.

static

26.

27.

WINTER = new Season("WINTER", 0, 10);

28.

SUMMER = new Season("SUMMER", 1, 20);

29.

$VALUES = (new Season[] {

30.

WINTER, SUMMER

31.

});

32.
33.

}
}

Can we create the instance of enum by new keyword?


No, because it contains private constructors
only.

Core Java
Can we have abstract method in enum?
Yes, ofcourse! we can have abstract methods and can provide the implementation of these
methods.

Applying enum on switch statement


We can apply enum on switch statement as in the given
example:

Example of applying enum on switch statement


1. class EnumExample5{
2. enum Day{ SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY}

3.
4. public static void main(String args[]){

5.
6. Day day=Day.MONDAY;

7.
8. switch(day){
9. case SUNDAY:

10.

System.out.println("sunday");

11.

break;

12.

case MONDAY:

13.

System.out.println("monday");

14.

break;

15.

default:

16.

System.out.println("other day");

17.

18.
19.

}}

20.

Autoboxing and Unboxing:


The automatic conversion of primitive data types into its equivalent Wrapper type
is known as boxing and opposite operation is known as unboxing. This is the new
feature of Java5. So java programmer doesn't need to write the conversion code.

Core Java

Advantage of Autoboxing and Unboxing:


No need of conversion between primitives and Wrappers manually so less coding is
required.

Simple Example of Autoboxing in java:


1.
2. class BoxingExample1{

3. public static void main(String args[]){


4.

int a=50;

5.

Integer a2=new Integer(a);//Boxing

6.
7.

Integer a3=5;//Boxing

8.
9.

System.out.println(a2+" "+a3);

10.

11.

12.
Output:50 5

Simple Example of Unboxing in java:


The automatic conversion of wrapper class type into corresponding primitive type, is known
as Unboxing. Let's see the example of unboxing:

1.
2. class UnboxingExample1{

3. public static void main(String args[]){


4.

Integer i=new Integer(50);

5.

int a=i;

6.
7.

System.out.println(a);

8. }
9. }

10.
Output:50

Core Java

Autoboxing and Unboxing with comparison operators


Autoboxing can be performed with comparison operators. Let's see the example of boxing
with comparison operator:

1.
2. class UnboxingExample1{

3. public static void main(String args[]){


4.

Integer i=new Integer(50);

5.
6.

if(i<100){

7.

System.out.println(i);

8.

//unboxing internally

9. }
10.

11.
Output:50

New Features in Java


J2SE 4 Features
The important feature of J2SE 4 is assertions. It is used for testing.

Assertion (Java 4)

J2SE 5 Features
The important features of J2SE 5 are generics and assertions. Others are auto-boxing, enum,
var-args, static import, for-each loop (enhanced for loop etc.

For-each loop (Java 5)

Varargs (Java 5)

Static Import (Java 5)

Autoboxing and Unboxing (Java 5)

Enum (Java 5)

Covariant Return Type (Java 5)

Annotation (Java 5)

Core Java

Generics (Java 5)

JavaSE 6 Features
The important feature of JavaSE 6 is premain method (also known as instrumentation).

Instrumentation (premain method) (Java 6)

JavaSE 7 Features
The important features of JavaSE 7 are try with resource, catching multiple exceptions etc.

String in switch statement (Java 7)

Binary Literals (Java 7)

The try-with-resources (Java 7)

Caching Multiple Exceptions by single catch (Java 7)

Underscores in Numeric Literals (Java 7)

Variable Argument (Varargs):


1. The varrags allows the method to accept zero or muliple arguments.
2. Before varargs either we use overloaded method or take an array as the method
parameter but it was not considered good because it leads to the maintenance
problem.
3. If we don't know how many argument we will have to pass in the method, varargs is
the better approach.

Advantage of Varargs:

We don't have to provide overloaded


methods so less code.

Syntax of varargs:
The varargs uses ellipsis i.e. three dots after the data type. Syntax is as
follows:
return_type method_name(data_type... variableName){}

Simple Example of Varargs in java:

Core Java
class VarargsExample1{

static void display(String... values){


System.out.println("display method invoked ");
}

public static void main(String args[]){

display();//zero argument
display("my","name","is","varargs");//four arguments
}
}

Output:display method invoked


display method invoked

Another Program of Varargs in java:


class VarargsExample{

static void display(String... values){


System.out.println("display method invoked ");
for(String s:values){
System.out.println(s);
}
}

public static void main(String args[]){

display();//zero argument
display("hello");//one argument
display("my","name","is","varargs");//four arguments
}
}

Core Java
Output:display method invoked
display method invoked
hello
display method invoked
my
name
is
varargs

Rules for varargs:


While using the varargs, you must follow some rules otherwise program code won't compile.
The rules are as follows:
There can be only one variable argument in the method.
Variable argument (varargs) must be the last argument.

Examples of varargs that fails to compile:


void method(String... a, int... b){}//Compile time error
void method(int... a, String b){}//Compile time error

Example of Varargs that is the last argument in the


method:
class VarargsExample3{

static void display(int num, String... values){


System.out.println("number is "+num);
for(String s:values){
System.out.println(s);
}
}

public static void main(String args[]){

Core Java
display(500,"hello");//one argument
display(1000,"my","name","is","varargs");//four arguments
}
}

Output:number is 500
hello
number is 1000
my
name
is
varargs

Example
public class VarArgsTest {
//Array of String in varargs
public static void retrive(String... str) {
for (String s : str) {
System.out.println(s);
}
}
// List of collection as mentioned here as Varargs
public static <E> void getData(List<E>... list) {
for (List<E> e : list) {
for (E e2 : e) {
System.out.println(e2);
}
}
}
public static void main(String[] args) {
String [] str = {"A", "B", "C"};
List<String> li = new ArrayList<String>();
li.add("A");
li.add("B");
li.add("C");

}
}

retrive(str);
getData(li);

Core Java
Try-With-Resources Example In JDK 7
In Java, normally we open a file in a try block, and close the file in the finally block, see following :
try{

//open file or resources


}catch(IOException){

//handle exception
}finally{

//close file or resources


}

Since JDK 7, a new try-with-resources approach is introduced. When a try block is end, it will close
or release your opened file automatically.
try(open file or resource here){

//...
}

//after try block, file will close automatically.

1. JDK 6 or early version


Classic way to read a file.
package com.mkyong.io;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

Core Java
public class Example1 {
public static void main(String[] args) {
BufferedReader br = null;
try {
String line;
br = new BufferedReader(new
FileReader("C:\\testing.txt"));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}

2. JDK 7
In JDK7, finally is no longer required. The file will be closed automatically after try block.
package com.mkyong.io;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

Core Java

public class Example2 {


public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new
FileReader("C:\\testing.txt")))
{
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

Strings in switch Statements


In the JDK 7 release, you can use a String object in the expression of a switch statement:
public String getTypeOfDayWithSwitchStatement(String dayOfWeekArg) {
String typeOfDay;
switch (dayOfWeekArg) {
case "Monday":
typeOfDay = "Start of work week";
break;
case "Tuesday":
case "Wednesday":
case "Thursday":
typeOfDay = "Midweek";
break;
case "Friday":
typeOfDay = "End of work week";
break;
case "Saturday":
case "Sunday":
typeOfDay = "Weekend";
break;

Core Java
default:
throw new IllegalArgumentException("Invalid day of the
week: " + dayOfWeekArg);
}
return typeOfDay;
}
The switch statement compares the String object in its expression with the expressions
associated with each case label as if it were using the String.equals method; consequently,
the comparison of String objects in switch statements is case sensitive. The Java compiler
generates generally more efficient bytecode from switch statements that use String objects
than from chained if-then-else statement

String in Switch support


One of the new features added in Java 7 is the capability to switch on a String.
With Java 6, or less
String color = "red";
if (color.equals("red")) {
System.out.println("Color is Red");
} else if (color.equals("green")) {
System.out.println("Color is Green");
} else {
System.out.println("Color not found");
}

String color = "red";


if (color.equals("red")) {
System.out.println("Color is Red");
} else if (color.equals("green")) {
System.out.println("Color is Green");
} else {
System.out.println("Color not found");
}

With Java 7:
String color = "red";
switch (color) {
case "red":
System.out.println("Color is Red");
break;
case "green":
System.out.println("Color is Green");
break;
default:
System.out.println("Color not found");
}

The switch statement when used with a String uses the equals() method to compare the
given expression to each value in the case statement and is therefore case-sensitive and will

Core Java
throw a NullPointerException if the expression is null. It is a small but useful feature which not
only helps us write more readable code but the compiler will likely generate more efficient
bytecode as compared to the if-then-else statement.

Catching Multiple Exception in single catch :


Whenever try block will thrown any exception of these type then and then only we can handle
the exception.
catch (IOException ex) {
System.out.println("IO Exception");
throw ex;
} catch (SQLException ex) {
System.out.println("SQL Exception");
throw ex;
}

1.If in a try block we need to handle multiple exceptions then we need to write exception
handler for each type of exception.
2.We can combine the multiple exceptions in catch block using Pipe (|)
Operator.
catch (IOException|SQLException ex) {
System.out.println("Exception thrown");
throw ex;
}
above single catch block can handle IO as well as SQL exceptions. So it is better to
use Pipe Operator to handle multiple exceptions instead of writing individual catch block for
each exception.

Parameter Accepted by Catch Block is Final :


1.If a catch block handles more than one exception type, then the catch parameter is
implicitly final.
2.In this example, the catch parameter ex is final.
3.We cannot assign any values to it within the catch block.

Binary Literals in Java Java 7 Feature

Binary literals are new features in Java 7.

As you all know that we can write integral types (byte, short, int, and long)
in Binary and Hexadecimal formats but from Java 7 onwards we can write
these numbers in binary format also.

Core Java

The number should be prefixed with 0b or 0B to be treated as binary


literal.

This feature is very helpful to bit-oriented systems like processors,


network protocols and bitmapped hardware device.

Early the programmer used to transform from binary to


decimal/hexadecimal and vice versa.

Using this feature will remove this transformation and chances of error will
be less in this conversion.

Also code using bitwise operations will be more readable with this feature.
package com.journaldev.util;
public class Java7Literals {
public static void main(String[] args) {
int i=0b0111;
byte b=(byte) 0b0111;
long l=(long) 0B0111L;
System.out.println("i="+i);
System.out.println("b="+b);
System.out.println("l="+l);
}
}
Output of the above program is:i=7
b=7
l=7
x=7

Underscores in Numeric Literals Java 7 Feature


One of the Java 7 features is underscores in numeric literals. You can place
underscores between digits of any numeric literal like int, byte, short, float, long,
double. Using underscores in numeric literals will allow you to divide them in
groups for better readability.
Lets see underscores in numeric literals in action:
public class UnderscoreNumericLiterals {

Core Java
public static void main(String[] args) {
long ccNumber = 1234_5678_9012_3456L;
long ssn = 999_99_9999L;
float pi = 3.14_15F;
long hexadecimalBytes = 0xFF_EC_DE_5E;
long hexadecimalWords = 0xCAFE_BABE;
long maxOfLong = 0x7fff_ffff_ffff_ffffL;
byte byteInBinary = 0b0010_0101;
long longInBinary = 0b11010010_01101001_10010100_10010010;
int add = 12_3 + 3_2_1;
System.out.println("ccNumber="+ccNumber);
System.out.println("ssn="+ssn);
System.out.println("pi="+pi);
System.out.println("hexadecimalBytes="+hexadecimalBytes);
System.out.println("hexadecimalWords="+hexadecimalWords);
System.out.println("maxOfLong="+maxOfLong);
System.out.println("byteInBinary="+byteInBinary);
System.out.println("longInBinary="+longInBinary);
System.out.println("add="+add);
}
}

Output

ccNumber=1234567890123456
ssn=999999999
pi=3.1415
hexadecimalBytes=-1253794
hexadecimalWords=-889275714
maxOfLong=9223372036854775807
byteInBinary=37
longInBinary=-764832622
add=444

Underscores in Numeric Literals Tips

Underscores can be placed only between digits.

You cant put underscores next to decimal places, L/F suffix or radix
prefix. So 3._14, 110_L, 0x_123 are invalid and will cause compilation error.

Multiple underscores are allowed between digits, so 12___3 is a valid


number.

You cant put underscores at the end of literal. So 123_ is invalid and
cause compile time error.

When you place underscore in the front of a numeric literal, its treated as

Core Java
an identifier and not a numeric literal. So dont confuse with it.
int _10=0;
int x = _10;
You cant use underscores where you are expecting a String with digits.
For example Integer.parseInt(12_3); will throw
java.lang.NumberFormatException.

Object Cloning in Java


The object cloning is a way to create exact copy of an object. For this purpose, clone()
method of Object class is used to clone an object.
The java.lang.Cloneable interface must be implemented by the class whose object clone we
want to create. If we don't implement Cloneable interface, clone() method generates
CloneNotSupportedException.

The clone() method is defined in the Object class. Syntax of the clone() method is as follows:
protected Object clone() throws CloneNotSupportedException

Why use clone() method ?


The clone() method saves the extra processing task for creating the exact copy of an object.
If we perform it by using the new keyword, it will take a lot of processing to be performed that
is why we use object cloning.

Advantage of Object cloning


Less processing task.

Core Java

Example of clone() method (Object cloning)


Let's see the simple example of object cloning
class Student implements Cloneable{
int rollno;
String name;

Student(int rollno,String name){


this.rollno=rollno;
this.name=name;
}

public Object clone()throws CloneNotSupportedException{


return super.clone();
}

public static void main(String args[]){


try{
Student s1=new Student(101,"amit");

Student s2=(Student)s1.clone();

System.out.println(s1.rollno+" "+s1.name);
System.out.println(s2.rollno+" "+s2.name);

}catch(CloneNotSupportedException c){}

}
}
Output:101 amit
101 amit
As you can see in the above example, both reference variables have the same value. Thus, the
clone() copies the values of an object to another. So we don't need to write explicit code to
copy the value of an object to another.
If we create another object by new keyword and assign the values of another object to this
one, it will require a lot of processing on this object. So to save the extra processing task we
use clone() method.

What is Deep Copy and Shallow Copy in Java

Core Java
Introduction:
The action of copying the attributes of one object into another of same data type is called
object copy. In java, we have the following approaches of copying one object into another:

Shallow Copy: here if the field which is to be copied is a primitive type, then the value
is copied else if the field which is to be copied is a memory address (or an object itself)
then the address is copied. Thus if the address is changed by one object, the change
gets reflected everywhere.
Deep Copy: here the data is copied in both the situations. This approach is costlier
and slower.
Lazy Copy: This is a combination of the above two approaches. Initially the shallow
copy approach is used and then checked if the data is shared by many objects
and the program needs to modify an object, the deep copy approach is used.
So we can select the type of copying based on the following two conditions:

Use shallow copying when no encapsulation is required.


Use deep copying when encapsulation is required.

Shallow Copy:
In shallow copy, a new object is created which contains the exact copy of the values in the
original object. Shallow copy follows the bit-wise copy approach. In shallow copy if the field
is a memory address, then the address is copied. Thus if the address is changed by one
object, the change gets reflected everywhere.

Figure 1: The flow chart describes shallow copy


Implementation:
class Employee {
private String name;
public Employee(String name) {
this.name = name;
}
public String getName() {
return name;

Core Java
}

public void setName(String name) {


this.name = name;
}

class Department implements Cloneable {


private String deptname;
private Employee emp;
public Department(String deptname, String empname) {
this.deptname = deptname;
this.emp = new Employee(empname);
}
public String getDeptname() {
return deptname;
}
public void setDeptname(String deptname) {
this.deptname = deptname;
}
public Employee getEmp() {
return emp;
}
public void setEmp(Employee emp) {
this.emp = emp;
}
public Object clone() {
// shallow copy
Object obj = null;
try {

obj = super.clone();
} catch(CloneNotSupportedException ex) {
ex.printStackTrace();
}
}

return obj;

}
public class CloneDemo {
public static void main(String[] args) {
Department dept = new Department("Developing", "Praveen");
System.out.println("Original Object before cloning");
System.out.println(dept.getDeptname() + " " +
dept.getEmp().getName());
Department dep = (Department) dept.clone();
System.out.println("Cloned object");
System.out.println(dep.getDeptname() + " " +

Core Java
dep.getEmp().getName());
dept.setDeptname("Testing");
dept.getEmp().setName("Shankar");
System.out.println("Original Object after it is updated:");
System.out.println(dept.getDeptname() + " " +
dept.getEmp().getName());
System.out.println("Cloned object after updating Original Object");
System.out.println(dep.getDeptname() + " " +
dep.getEmp().getName());
}
}
Output :

Original Object before cloning


Developing Praveen
Cloned object
Developing Praveen
Original Object after it is updated:
Testing Shankar
Cloned object after updating Original Object
Developing Shankar

Deep Copy:
In deep copy, not only all the fields of an object are copied, all the dynamically
allocated memory address which are pointed by that object are also copied.

Figure 2: The diagram describes deep copy process

Implementation:
class Employee {
private String name;

Core Java
public Employee(String name) {
this.name = name;
}
public String getName() {
return name;
}

public void setName(String name) {


this.name = name;
}

class Department implements Cloneable {


private String deptname;
private Employee emp;
public Department(String deptname, String empname) {
this.deptname = deptname;
this.emp = new Employee(empname);
}
public String getDeptname() {
return deptname;
}
public void setDeptname(String deptname) {
this.deptname = deptname;
}
public Employee getEmp() {
return emp;
}
public void setEmp(Employee emp) {
this.emp = emp;
}
public Object clone() {
// Deep copy
Department department = new Department(deptname, emp.getName());
return department;
}

public class CloneDemo {


public static void main(String[] args) {
Department dept = new Department("Developing", "Praveen");
System.out.println("Original Object before cloning");
System.out.println(dept.getDeptname() + " " +
dept.getEmp().getName());
Department dep = (Department) dept.clone();
System.out.println("Cloned object");
System.out.println(dep.getDeptname() + " " +
dep.getEmp().getName());

Core Java
dept.setDeptname("Testing");
dept.getEmp().setName("Shankar");
System.out.println("Original Object after it is updated:");
System.out.println(dept.getDeptname() + " " +
dept.getEmp().getName());
System.out.println("Cloned object after updating Original Object");
System.out.println(dep.getDeptname() + " " +
dep.getEmp().getName());
}
}

output
Original Object before cloning
Developing Praveen
Cloned object
Developing Praveen
Original Object after it is updated:
Testing Shankar
Cloned object after updating Original Object
Developing Praveen
Simple Explanation of Deep and Shallow Copying

When creating copies of arrays or objects one can make a deep copy or a shallow copy. This
explanation uses arrays.
Recall array variables in Java are references or pointers. A shallow copy can be made by simply
copying the reference.
public class Ex{
private int[] data;
public Ex(int[] values){
data = values;
}
public void showData(){
System.out.println( Arrays.toString(data) );
}
}
The above code shows shallow copying. data simply refers to the same array as vals.

Core Java

This can lead to unpleasant side effects if the elements of values are changed via some other reference.

public class UsesEx{


public static void main(String[] args){
int[] vals = {-5, 12, 0};
Ex e = new Ex(vals);
e.showData(); // prints out [-5, 12, 0]
vals[0] = 13;
e.showData(); // prints out [13, 12, 0]
// Very confusiin, because I didn't intentionally change anything about the
// object e refers to.
}
}
A deep copy means actually creating a new array and copying over the values.
public class Ex{
private int[] data;
public Ex(int[] values){
data = new int[values.length];
for(int i = 0; i < data.length; i++)
data[i] = values[i];

Core Java
}
public void showData(){
System.out.println( Arrays.toString(data) );
}
}
The above code shows deep copying.

Changes to the array values refers to will not result in changes to the array data refers to.

Garbage Collection
Manually we can call
System.gc()
Runtime.getRuntime().gc()

Is it good practice to call Garbage Collector manually?


No, it is definitely not good practice.
You can use System.gc(). Note that this is not guaranteed to call the garbage collector - it only
gives a hint to the system that it might be a good idea to do garbage collection.
The garbage collector in Oracle's JVM contains a lot of sophisticated logic to determine when
and what to cleanup.
You can call Garbage Collector explicitly, but JVM decides whether to process the call or not. Ideally, you should
never write code dependent on call to garbage collector.

Core Java
JVM internally uses some algorithm to decide when to make this call. When you make call
using System.gc(), it is just a request to JVM and JVM can anytime decide to ignore it.

Different ways to create objects in Java


This is a trivia. Yeah, its a bit tricky question and people often get confused. I had searched a
lot to get all my doubts cleared.
There are four different ways (I really dont know is there a fifth way to do this) to create
objects in java:

1. Using new keyword


This is the most common way to create an object in java. I read somewhere that almost 99% of
objects are created in this way.
MyObject object = new MyObject();
2. Using Class.forName()
If we know the name of the class & if it has a public default constructor we can create an object
in this way.
MyObject object = (MyObject) Class.forName("subin.rnd.MyObject").newInstance();
3. Using clone()
The clone() can be used to create a copy of an existing object.
MyObject anotherObject = new MyObject();
MyObject object = anotherObject.clone();
4. Using object deserialization
Object deserialization is nothing but creating an object from its serialized form.
ObjectInputStream inStream = new ObjectInputStream(anInputStream );
MyObject object = (MyObject) inStream.readObject();
Now you know how to create an object. But its advised to create objects only when it is
necessary to do so.

Our current project is Q2O2A which is developed the product to the client Cbeyond
This Product consist of collection of Offers
Offers contains the collection of bundle families
bundle families contains the collection of bundles
bundles which is the combination of product and their available quantity

Core Java
Each offer has default product is configured
Customer is to select offer and available bundles
Once the offer is selected by customer it has been listed
Apart from default product configuration customer can select additional bundles and their
product configuration
Those kind of whole process is nothing but purchsing PC in a showroom
For example,
While purchasing Desktop or laptop sales person would explain the default configruation
of each and every desktop or laptop for customer satisfication
If suppose the customer is not satified the configuration he can ask extra configuration
depends upon PC
Once the purchasing is over sales person generate quote to verify it by customer via
email or any mediator
Customer verify the quote once if it gets successful the offer has to become order for
the product
The same logic has applied in our project and finally the contract has created by sales
person and approved by customer
Once if all gets over the product has to become Order
Service URL
1) authenticate
Method Declaration
public abstract String authenticate(final String emailId, final String password);
2) getGroups
Method Declaration
public abstract List<ProductGroup> getGroups(final String userId, final String deviceId,
final String includeImageData);
3) getUserProducts
Method Declaration
public abstract ProductDTO getUserProducts(final String userId, final int groupId,
final String deviceId, final String includeImageData, final Boolean filter);
4) getNewProducts
Method Declaration

Core Java
public abstract ProductDTO getNewProducts(final String userId, final String deviceId,
final String includeImageData);
5) downloadTitle
Method Declaration
public abstract void downloadTitle(final HttpServletResponse response, final int productId,
final String userId, final String deviceId);
6) getDownloadURI
Method Declaration
public abstract String getDownloadURI(int productId, String userId, String deviceId);
7) getImage
Method Declaration
public abstract void getImage(final HttpServletResponse response, final String userId,
final Integer productId, final Integer productGroupId, final String deviceId);

Set Request Headers are If iPad user,


USER_NAME
PASSWORD
DOMAIN
DEVICE_ID
INSTALLATION_ID
Set Request Paramater are If Browserapp user,
USER_NAME
PASSWORD
DOMAIN
1) If request contains /logout pattern get HttpSession and invalidate the current session
and return
2) Check request extension which contains the following like,
jpg,png,gif,css return true and process next request or resource via doFilter(req,
res)

Core Java
3) Initialize session while doing first request create current HTTPSession and add Attribute
WKESession Object with attribute name "WkeSession"
WKESession Object Contains Map and put the corresponding value like,
SESSION_ID as Key and Current Session ID as Value
public static WKESession initializeSession(final HttpSession httpSession) {
WKESession httpWKSession = (WKESession) httpSession.getAttribute(WK_SESSION);
if (httpWKSession == null) {
httpWKSession = new WKESession();
httpSession.setAttribute(WK_SESSION, httpWKSession);
}
httpWKSession.getMap().put(SESSION_ID, httpSession.getId());
return httpWKSession;
}
4) Check whether the first request user is in session or not
Username is stored in WKESession Object and that Object is store in Current
Session
If user is already in session the process next request or resource
or else step 5
5) If request comes from browserapp get parameter like username, password, domain
from request parameter
6) Via iPad request from client they encrypt the password use Base64 and set it in
request header
Example,
Base64.encodeBase64String(text.getBytes()) --> for encryption in server side
Base64.decodeBase64(cipherText)

--> for decryption in server side

7) In filter layer decrypt the password if request from iPad and do Authentication
8) While hit the login service URL in EDIS Server they would send XML REsponse. Using

Core Java
DOMParser to parse the XML REsponse and get userId or else get error from EDIS
9) If get successful store userid, username, domain all data in WKESession Object and
store it in SEssion or else throw error via user defined Exception APPException which
extends RunTimeException whatever got from EDIS
10) If browserapp request gets success and create Message Class and set status and
serialize it using JSONSerializer and write it in PrintWriter
Example,
JSONSerializer uses to serialize Java Object to JSON text
JSONSerializer serializer = new JSONSerializer();
return serializer.serialize( person );
What this statement does is output the json from the instance of person. So the JSON
we might see for this could look like:
{ "class": "com.mysite.Person",
"firstname": "Charlie",
"lastname": "Rose",
"age", 23
"birthplace": "Big Sky, Montanna"
}
11) DB Connection use jdbcDaoSupport to connect DB for data manipulation
12) ActivityInterceptor extends HandlerInterceptorAdapter which is used to logging
purpose whatever the user request the service those are all logged in seperate table as
identity
Service URL :
1) edisURL + getUserProductGroups.htm
2) edisURL + getUserProducts.htm
3) edisURL + getNewProducts.htm
4) edisURL + downloadTitle.htm

Core Java
5) edisURL + getDownloadURI.htm
6) ofsURL + logProductDownload.htm
7) edisURL + getImage.htm
8) ofsURL + downloadSupplement.htm
9) ofsURL + hasSupplement.htm
10) ofsURL + deleteProduct.htm
11) ofsURL + deleteAllProduct.htm

Use ObjectMapper to map JSON Object to corresponding Java Object


Its applicable when requesting hasSupplement.htm with JSON request
Supplement Upload :
1) via AJAX we can pass request to server from client
2) constrct message String and using JSON.stringify(message) to convert JSON and send
to server via request parameter
JSON.stringify -> Converts a JavaScript value to a JavaScript Object Notation
(JSON) string.
3) In server side using JSONDeserializer to deserialize (get message from request
parameter or Reader and convert it to corresponding JAVA CLass)
4) ServiceDelegator class Using Reflection to invoke the coresponding method in
corresponding class which got from client request
5) Using ServletFileUpload(FileItemFactory) which is 3rd party apache jar to upload file
and maintain in OFS server local workspace
6) Upload Supplement HTML and Supplement Zip file

Core Java

Вам также может понравиться