Tuesday 9 February 2016

JAVA REFLECTION API

Java Reflection is a process of examining or modifying the run time behavior of a class at run time.
The java.lang.Class class provides many methods that can be used to get metadata, examine and change the run time behavior of a class.
The java.lang and java.lang.reflect packages provide classes for java reflection.

Where it is used

The Reflection API is mainly used in:
  • IDE (Integrated Development Environment) e.g. Eclipse, MyEclipse, NetBeans etc.
  • Debugger
  • Test Tools etc.

java.lang.Class class

The java.lang.Class class performs mainly two tasks:
  • provides methods to get the metadata of a class at run time.
  • provides methods to examine and change the run time behavior of a class.

Commonly used methods of Class class:

MethodDescription
1) public String getName()returns the class name
2) public static Class forName(String className)throws ClassNotFoundExceptionloads the class and returns the reference of Class class.
3) public Object newInstance()throws InstantiationException,IllegalAccessExceptioncreates new instance.
4) public boolean isInterface()checks if it is interface.
5) public boolean isArray()checks if it is array.
6) public boolean isPrimitive()checks if it is primitive.
7) public Class getSuperclass()returns the superclass class reference.
8) public Field[] getDeclaredFields()throws SecurityExceptionreturns the total number of fields of this class.
9) public Method[] getDeclaredMethods()throws SecurityExceptionreturns the total number of methods of this class.
10) public Constructor[] getDeclaredConstructors()throws SecurityExceptionreturns the total number of constructors of this class.
11) public Method getDeclaredMethod(String name,Class[] parameterTypes)throws NoSuchMethodException,SecurityExceptionreturns the method class instance.

How to get the object of Class class?

There are 3 ways to get the instance of Class class. They are as follows:
  • forName() method of Class class
  • getClass() method of Object class
  • the .class syntax

1) forName() method of Class class

  • is used to load the class dynamically.
  • returns the instance of Class class.
  • It should be used if you know the fully qualified name of class.This cannot be used for primitive types.

Collections in Java

Collections in java is a framework that 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, manipulation, deletion etc. can be performed by Java Collections.
Java Collection simply means a single unit of objects. Java Collection framework provides many interfaces (Set, List, Queue, Deque etc.) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet etc).

What is Collection in java

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

What is framework in java

  • provides readymade architecture.
  • represents set of classes and interface.
  • is optional.

What is Collection framework

Collection framework represents a unified architecture for storing and manipulating group of objects. It has:
  1. Interfaces and its implementations i.e. classes
  2. 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.
hierarchy of collection framework


Methods of Collection interface

There are many methods declared in the Collection interface. They are as follows:
No.MethodDescription
1public boolean add(Object element)is used to insert an element in this collection.
2public boolean addAll(collection c)is used to insert the specified collection elements in the invoking collection.
3public boolean remove(Object element)is used to delete an element from this collection.
4public boolean removeAll(Collection c)is used to delete all the elements of specified collection from the invoking collection.
5public boolean retainAll(Collection c)is used to delete all the elements of invoking collection except the specified collection.
6public int size()return the total number of elements in the collection.
7public void clear()removes the total no of element from the collection.
8public boolean contains(object element)is used to search an element.
9public boolean containsAll(Collection c)is used to search the specified collection in this collection.
10public Iterator iterator()returns an iterator.
11public Object[] toArray()converts collection into array.
12public boolean isEmpty()checks if collection is empty.
13public boolean equals(Object element)matches two collection.
14public int hashCode()returns the hashcode number for collection.


Iterator interface

Iterator interface provides the facility of iterating the elements in forward direction only.

Methods of Iterator interface

There are only three methods in the Iterator interface. They are:
  1. public boolean hasNext() it returns true if iterator has more elements.
  2. public object next() it returns the element and moves the cursor pointer to the next element.
  3. public void remove() it removes the last elements returned by the iterator. It is rarely used.

No comments:

Post a Comment