Sunday, 14 May 2017

Java Applet ~ mkniit

Applet in Java

  • Applets are small Java applications that can be accessed on an Internet server, transported over Internet, and can be automatically installed and run as apart of a web document. Any applet in Java is a class that extends the java.applet.Applet class.
  • An Applet class does not have any main() method.
  • It is viewed using JVM. The JVM can use either a plug-in of the Web browser or a separate runtime environment to run an applet application.
  • JVM creates an instance of the applet class and invokes init() method to initialize an Applet.

Read More »

Java Legacy Classes ~ mkniit

Legacy Classes

Early version of java did not include the Collection framework. It only defined several classes and interface that provide method for storing objects. When Collection framework were added in J2SE 1.2, the original classes were reengineered to support the collection interface. These classes are also known as Legacy classes. All legacy claases and interface were redesign by JDK 5 to support Generics.
The following are the legacy classes defined by java.util package
  1. Dictionary
  2. HashTable
  3. Properties
  4. Stack
  5. Vector
There is only one legacy interface called Enumeration
NOTE: All the legacy classes are syncronized
Read More »

Java Comparator Interface ~ mkniit

In Java, Comparator interface is used to order the object in your own way. It gives you ability to decide how element are stored within sorted collection and map.
Comparator Interface defines compare() method. This method compare two object and return 0 if two object are equal. It returns a positive value if object1 is greater than object2. Otherwise a negative value is return. The method can throw a ClassCastException if the type of object are not compatible for comparison.

Example

Student class
class Student
int roll;
  String name;
  Student(int r,String n)
  {
      roll = r;
      name = n;
  }
  public String toString()
  {
      return roll+" "+name;
  }
Read More »

Java Map Interface ~ mkniit

A Map stores data in key and value association. Both key and values are objects. The key must be unique but the values can be duplicate. Although Maps are a part of Collection Framework, they can not actually be called as collections because of some properties that they posses. However we can obtain a collection-view of maps.
InterfaceDescription
MapMaps unique key to value.
Map.EntryDescribe an element in key and value pair in a map. This is an inner class of map.
NavigableMapExtends SortedMap to handle the retrienal of entries based on closest match searches
SortedMapExtends Map so that key are maintained in an ascending order.
Read More »

Java Iterator and ListIterator ~ mkniit

Accessing a Collection

To access, modify or remove any element from any collection we need to first find the element, for which we have to cycle throught the elements of the collection. There are three possible ways to cycle through the elements of any collection.
  1. Using Iterator interface
  2. Using ListIterator interface
  3. Using for-each loop

Accessing elements using Iterator

Iterator Interface is used to traverse a list in forward direction, enabling you to remove or modify the elements of the collection. Each collection classes provide iterator() method to return an iterator.
Read More »

Java Collection Classes ~ mkniit

The Collection classes

Java provides a set of Collection classes that implements Collection interface. Some of these classes provide full implementations that can be used as it is and other abstract classes provides skeletal implementations that can be used as starting points for creating concrete collections.

ArrayList class

  1. ArrayList class extends AbstractList class and implements the List interface.
  2. ArrayList supports dynamic array that can grow as needed. ArrayList has three constructors.
    ArrayList()
    
    ArrayList( Collection C )
    
    ArrayList( int capacity )
    
  3. ArrayLists are created with an initial size, when this size is exceeded, it gets enlarged automatically.
  4. It can contain Duplicate elements and maintains the insertion order.
Read More »

Java Collection Interfaces ~ mkniit

Interfaces of Collection Framework

The collection framework has a lot of Interfaces, setting the fundamental nature of various collection classes. Lets study the most important Interfaces in the collection framework.

The Collection Interface

  1. It is at the top of collection heirarchy and must be implemented by any class that defines a collection. Its general declaration is,
    interface Collection < E >
    
  2. Following are some of the commonly used methods in this interface.
    MethodsDescription
    add( E obj )Used to add objects to a collection. Doesn't add duplicate elements to the collection.
    addAll( Collection C )Add all elements of collection C to the invoking collection
    remove( Object obj )To remove an object from collection
    removeAll( Collection C )Removes all element of collection C from the invoking collection
    contains( Object obj )To determine whether an object is present in collection or not
    isEmpty()Returns true if collection is empty, else returns false
    size()returns number of elements present in collection
Read More »

Java Introduction to Collection ~ mkniit

Collection Framework

Collection framework was not part of original Java release. Collections was added to J2SE 1.2. Prior to Java 2, Java provided adhoc classes such as Dictionary, Vector, Stack and Properties to store and manipulate groups of objects. Collection framework provides many important classes and interfaces to collect and organize group of alike objects.

Important Interfaces of Collection API

InterfaceDescription
CollectionEnables you to work with groups of object; it is at the top of collection hierarchy
DequeExtends queue to handle double ended queue.
ListExtends collection to handle sequences list of object.
QueueExtends collection to handle special kind of list in which element are removed only from the head.
SetExtends collection to handle sets, which must contain unique element.
SortedSetExtends sets to handle sorted set.
Read More »

Java Generics ~ mkniit

A class or interface that operates on parameterized type is called Generic. Generics was first introduced in Java5. Now it is one of the most profound feature of java programming language. It provides facility to write algorithm independent of any specific type of data. Generics also provide type safety.
Using Generics, it becomes possible to create a single class or method that automatically works with all types of data(Integer, String, Float etc). It expanded the ability to reuse code safely and easily.

Example of Generic class

class Gen <T>
{
 T ob;     //an object of type T is declared
 Gen(T o)  //constructor
 {
  ob = o;
 }
 public T getOb()
 {
  return ob;
 }
}

class Test
{
 public static void main (String[] args)
 {
  Gen < Integer> iob = new Gen(100);     //instance of Integer type Gen Class. 
  int  x = iob.getOb();
  System.out.print in(x);
  Gen < String> sob = new Gen("Hello");  //instance of String type Gen Class.
  String str = sob.getOb();
 }
}
Read More »

Java Java Networking ~ mkniit

Networking in Java

Java is a premier language for network programming. java.net package encapsulate large number of classes and interface that provides an easy-to use means to access network resources. Here are some important classes and interfaces of java.net package.

Some Important Classes

CLASSES
CacheRequestCookieHandler
CookieManagerDatagrampacket
Inet AddressServerSocket
SocketDatagramSocket
ProxyURL
URLConnection
Read More »

Java Serialization ~ mkniit

Serialization and Deserialization in Java

Serialization is a process of converting an object into a sequence of bytes which can be persisted to a disk or database or can be sent through streams. The reverse process of creating object from sequence of bytes is called deserialization.
A class must implement Serializable interface present in java.io package in order to serialize its object successfully. Serializable is a marker interface that adds serializable behaviour to the class implementing it.
Java provides Serializable API encapsulated under java.io package for serializing and deserializing objects which include,
  • java.io.serializable
  • java.io.Externalizable
  • ObjectInputStream
  • and ObjectOutputStream etc.
Read More »