• No results found

It is the package which is mainly based on Collections

In document Core Java Notes (Page 31-50)

A Collection is a group of objects.Collection Frame work standardises the way in which groups of objects are handled by ur programs.These are available in java.util package.

Some imp. classes in java.util package.

ArrayList

Some imp. interfaces in java.util package are:

Collection

Collection interface is the foundation upon which the collections framework is built.

A collection can not directly store values of type int,char,double etc..If you want to

store such objects,you have to use primitive wrapers.

List :The List interface extends Colletion and declares the behavior of a collection that stores a sequence of elements.A List may contain duplicate elements.

Set : A Set extends Collection and declares the behavior of a collection that does not allow duplicate elements.

LinkedList:The LinkedList class extends

AbstractSequentialList and implements the List interface.

ArrayList : ArrayList class extends AbstractList and implements the List interface.ArrayList supports dynamic arrays that can grow as needed.It can dynamically

increase or decrease in size.ArrayList are created with an initial size.when this size is exceeded ,it is automatically enlarged.When objects are removed,the array may be shrunk.

HashSet : HashSet extends AbstractSet and implements the Set interface.In a HashSet the elements are not stored in a sorted order.

TreeSet : TreeSet provides an information of the Set interface that uses a tree for storage.

Objects are stored in sorted,ascending order.Access and retrieval times are quite fast,which makes TreeSet an excellent choice when storing large amounts of sorted information that must be found quickly.

Iterator : An Iterator interface is used to cycle through the elements in a collection.

This is also done by a ListIterator interface.Iterator enables you to cycle through a collection,obtaining or removing elements.

ListIterator:

ListIterator extends Iterator to allow bidirectional traversal of a list.

Map : A Map interface is an object that stores associations between keys and values or key/value pairs.Given a key we can find its value.Both keys and values are objects.The keysmust be unique,but the values may be duplicated.Some may accept a null key and a null value.Map.Entry describes an element(key/value pair)in a map.This is an inner class of Map.

Enumeration : Enumeration interface defines the methods by which you can enumerate(obtain one at a time)the

elements in a collection of objects.This is superceded by Iterator.

Enumeration has two methods.

boolean hasMoreElements() Object nextElement()

Vector : Vector implements a dynamic array.It is similar to ArrayList, but Vector vector is synchronised.A Vector

extends AbstractList and implements the List interface.

Stack : Stack is a subclass of Vector that implements a Last-In,First-Out(LIFO) stack.

There are two opearation in Stack.Push n Pop operation.Push operation adds an object to the

stack n Pop operation deletes an object from stack.

Dictionary : It is an abstract class that represents a key/value pair like a Map.

HashTable : HashTable class is similar to a HashMap,but HashTable is synchronized.

HashTable stores key/value pairs in a hash table.When using a HashTable,you specify an object that is used as a key,and the value that you want linked to that key.

StringTokenizer:It is mainly used for parsing.To use a StringTokenizer,you specify an input string and a string that contains Delimiters.Delimiters are characters that seperate

tokens.Each character in the delimiters string is considered a valid delimiter.For example ",;:"sets the delimiters to

comma,semicolon,and colon.

The Collection Algorithms:

The collections framework

Defines several algorithms that can be applied to collections and maps.These algorithms are defined as static methods

within the Collections class.Several of the methods can throw a ClassCastException,which occurs when an attempt is made to compare incompatible types.

The Legacy classes and Interfaces:

Legacy classes and interfaces are sysnchronized.But none of the collection classes are synchronized.The legacy classes defined by java.util package are

Dictionary Hashtable Properties Stack Vector

There is only one legacy interface called Enumeration Random:

Random class is a generator of pseudorandom numbers.

Observable:

The Observable class is used to create sub classes that other parts of your program can observe.When an object of such a sub class undergoes a change, observing classes are notified.

Observing classes must implement the Observer interface, which defines the update() method.

****** E N D ******

Java.io

java.io is a package in Java to perform input and output operations.

Stream is an abstraction that either produces or consumes information.A stream is linked to a physical device by the java i/o system.

System class defines three predefined stream variables namely in,out,err

System.out refers to the standard output stream.By default, this is the console.System.err refers to the standard error stream,which is also the console by default.System.in refers to standard input,which is the keyboard by default.

Streams are two types.Byte streams and Character streams.

(i)Byte Streams :

Byte streams provide a convenient means for handling input and output of bytes. Byte streams are used when

reading or writing binary data.

Byte streams are defined by two classes namely InputStream and OutputStream

(ii)Character Streams :

Character streams provide a convenient means for handling input and output of characters.

Character streams are defined by two classes namely Reader and Writer.

Serialization:

Serialization is the process of writing the state of an object to a byte stream.This is useful when you want to save

the state of your program to a persistent storage area, such as a file.

Serialization is mainly needed to implement Remote Method Invocation (RMI)

Serializable :

A Serializable is an interface.This interface has no variables or methods. It is also called as Marker interface.

A Serialazable interface is used to make a class as serialized.If a class is serialazable,all of its subclasses are also serializable.

****** E N D ******

Networking:

A network is a set of computers and peripherals, which are physically connected together. Networking enables sharing of resources and communication. Internet is a network of networks.

Java applets can be downloaded from a website. Networking in java is possible through the use of java.net package.

Protocols: Communication between computers in a network or a different networks require a certain set of rules called protocol. Java networking is done using TCP/IP protocol.

Some of the different kinds of protocols available are HTTP, FTP, SMTP.

HTTP: hyper text transfer protocol enables interaction with the internet. It is the underlying protocol of www

FTP: file transfer protocol enables transfer of files between computers.

SMTP: SimpleMailTransferProtocol is used to send email between machines.

Client /Server: A computer which requests for some service from another computer is called client. The one that processes the request is called a server.

Internet Address: Every computer connected to a network has a unique IP address. An IP address is a 32- bit number which has four numbers separated by period. A sample IP address is given below.

80. 0 . 0 . 53

Domain Naming Service: it is very difficult to remember a set of numbers (IP address) to connect to the internet .The DomainNaming Service (DNS) is used to overcome this problem. It maps one particular IP address to a string of characters Eg: www.yahoo.com.

Port: It is a number given for a particular application running in a system.

For a system there will be only one connection to network, but there could be many applications running in the system. So, for communicating with each of these applications unique number is given. This number is known as a port number.

Each port is identified by a number from 0 to 65, 535.

Each port can be allocated to a particular service port number .7 is assigned for ECHO, 21 is assigned for FTP, 23 is for Telnet, 25 is for e-mail, 80 for HTTP and 1099 for RMI

registry.

Networking classes and interfaces: All these classes and interfaces are contained in java.net package.

DatagramPacket,DatagramSocket,InetAddress,Socket,

ServerSocket, URL, URLConnection etc., are some of the important classes in networking.

FileNameMap,SocketImplFactory, SocketOptions are important interfaces in this networking.

Whois: It is a simple directory service protocol. It was originally designed to keep track of administrators responsible for internet hosts and domains.

A Whois client connects to one of several central servers and requests directory information for a person or persons. It can usually give you a phone number and an

e-mail address.

SocketBasics:

A socket is a connection between two hosts. It can perform seven basic operations.

1.Connect to a remote machine.

2.Send data

3.Receive data

4.Close a connection 5.Bind a port

6.Listen for incoming data

7.Accept connections from remote machines on the bound port.

Java’s Socket class, which is used by both clients and servers, has methods that correspond to the first four of these operations. The last three are needed only by servers which wait for clients to connect to them. They are implemented by the ServerSocket class.

Socket class is used to perform client side TCP operations.

ServerSocket class contain every thing you need to write servers in Java.

URL: is called as Uniform Resource Locator and is a reference or an address to a resource on the internet.

A URL has two main components.

1. Protocol Identifier 2. Resource name.

Eg: The following is an example of a URL which addresses

the java web site hosted by SunMicroSystem . http://java.sun.com.

Protocol Identifier is http

Resource name is java.sun.com

URL Connection: it is a general purpose class for accessing the attributes of a remote resource. Once you make a connection to a remote server, you can use URLConnection to inspect the prosperities of the remote object before actually transporting it locally.

TCP (Transmission Control Protocol)

It is a connection based protocol that provides a reliable flow of data between two computer, this is analogous to making phone call. It provides point-to-point channel for application that require reliable communication HTTP, FTP, Telnet etc., are TCP protocols.

UDP (User Datagram protocol)

It is a connection less oriented protocol. It sends independent packets of data from one computer to another with no guarantees about arrival.This is analogous to sending

a letter through the postal service

DatagramPacket,DatagramSocket etc., are UDP type connection less protocols.

Differencec Between TCP n UDP

TCP UDP

It is a connection oriented protocol

It is a connectionless oriented protocol

It is similar to Telephone It is similar to sending letter from Post office

Overheads are high Overheads are low HTTP,FTP,SMTP etc…

are Connection oriented protocols

DatagramPacket,DatagramSocket etc…are Connectionless oriented protocols

It is more reliable Less reliable

Order guaranteed Order of deliver not guaranteed Delivery guaranteed Delivery not guaranteed

************ E N D **********

Applet

An Applet is a Java program which runs from a Java enabled web browser.

It also runs by using an appletviewer which is a tool from jdk(Java Development Kit)

Applet is a class in java.applet package.

Writing an Applet program:

We have to import two packages to write an Applet Program.Those packages are java.awt and java.applet

• There is no main() method in an Applet program.It has an init() method where the execution of the Applet begins.

• We have to write an <Applet> tag in order to see the output of an Applet Program.

• <Applet> tag contains three mandatory attributes namely code,width and height

• We can use an <Applet> tag in the same Java file where your Applet program is written by commenting it.We can also separately use this <Applet> tag in a html file.

Applet Architecture :

Applet is a window based program which runs by using a Java compatible browser or an appletviewer. It’s architecture is different from a console based application programs.

Applets are event driven.That is why we have to import java.awt package also to write Applet programming. All

Applets runs in a window. They have init() method instead of main() as in console based programs.

Life cycle of an Applet:

There are four stages in the life cycle of an Applet.These life cycle methods will be automatically called by the container or browser where the Applet is running.

The four life cycle methods are:

(i)init() (ii)start() (iii)stop() (iv)destroy() init():

This is the first method called when an Applet is

initialized.This method is called exactly once.During this method initialization of the Applet will generally be done.

start():

This is the next method called immediately called after the init() method.This method will be called one or more

number of times.

stop():

This method is called when an Applet moves from one page to another page.This method will also be called one or more number of times.

destroy():

This is the last method called by an Applet.This method is used to relieve the memory resources used by the

Applet.This method will also be called exactly once.

Applet Skeleton :

There are five methods defiend in the skeleton of an Applet. They are :

(i)init() (ii)start() (iii)stop() (iv)destroy() (v)paint()

The first four methods are also called life cycle methods of an Applet.

paint() method is called when an Applet’s window must be restored. In this method we can display any thing on the Applet window.

This method is called when each time Applet’s output is redrawn.

Note :

There are three interfaces in java.applet package.They are AppletContext, AudioClip and AppletStub

AppletContext :

AppletContext is an interface that lets you get information from the Applet’s execution environment.

The context of the currently executing Applet is obtained by

a call to the getAppletContext() method defined in Applet class.

showDocument()

Within an Applet,once you have obtained the Applet’s context,you can bring another document into view by calling showDocument() method. This method has no return values and throws no exception if it fails.

Writing a SimpleApplet Program:

/*Writing SimpleApplet Program which displays a message on the Applet window*/

import java.awt.*;

import java.applet.*;

/*<Applet code=”SimpleApplet” width=300 height=400></Applet>*/

public class SimpleApplet extends Applet {

public void paint(Graphics g) {

g.drawString(“Welcome to Applets”,100,200);

} }

Passing Parameters to Applets :

We can send parameters to Applet from a HTML file using

<Param> tag.A <Param> tag contains name and value as a pair and by giving name we can get the value associated with

that name.

getParameter() of Applet class is used to retrieve the value of a given name in the <Param> tag.

/*Program in which a HTML file passes parameters to an Applet and those values are displayed in the Applet Window.*/

import java.awt.*;

import java.applet.*;

/*<Applet code=”ParamApplet” width=400 height=600>

<param name=”Company1” value=”Infosys”></param>

</Applet> */

public class ParamApplet extends Applet {

Public void paint(Graphics g) {

String s=getParameter(“Company1”);

g.drawString(s,200,300);

} }

Explanation:

The output of the above program in an Applet window

is Infosys Note :

There are three interfaces in java.applet package.They are AppletContext, AudioClip and AppletStub

AppletContext :

AppletContext is an interface that lets you get information from the Applet’s execution environment.

The context of the currently executing Applet is obtained by a call to the getAppletContext() method defined in Applet class.

showDocument()

Within an Applet,once you have obtained the Applet’s context,you can bring another document into view by calling showDocument() method. This method has no return values and throws no exception if it fails.

*********** E N D *********

In document Core Java Notes (Page 31-50)

Related documents