Java
Java
1. Application programs.
2. Applets.
Application programs are programs which are similar to programs created using
any other language. But it is the "Applets" that makes Java, a special language. Applets
are Java programs that can be run using a web browser and hence an Applet executes on
the client side of a web connection.
Features of Java:
Architecture-Neutral
Distributed
Multithreaded
Secure
Dynamic
1. Java is a portable language(i.e) java programs in the form of binary code can be run on
variety of architectures to produce a unique output.
The feature that enables this, is the output of the java compiler. As, usual it is not the
platform specific .EXE file, but it is the .CLASS file which contains byte codes. Byte
codes are highly optimized set of intermediate instructions designed to be executed by the
Java Runtime Environment. In fact, each and every Java program is compiled by the Java
compiler and a corresponding .CLASS file is created. Then the .CLASS file is
interpretted by an interpretter to generate the required output.
5. In Java, memory allocation for an object is done dynamically at the runtime. This
enables an object to be tied more closely with the methods it
operates upon, thus, enabling a call to the method which matches the type of the call,
dynamically, at the runtime. No, static memory allocation for objects is possible in Java.
APPLET
What is applet
--------------
Applets are java programs that are integrated into web pages. When a web page
containing an applet is displayed by a web browser, the applet is loaded and
executed. The applet's output is displayed within a subset of the browser's display
area.
1. init()
---------
The init() method is the first method to be called. This is where you should
initialize variables. This method is called only once during the life time of your
applet.
2.start()
---------
The start() method is called after init(). It is also called to restart an applet
after it has been stopped. start() is called each time an applet's HTML document
is displayed on the page.
3.paint()
---------
The paint() method is called each time your applet's output must be
redrawn. paint() is called when the applet begins execution.The paint method has
one parameter of type Graphics.This parameter will contain the graphics
context,which describes the graphics environment in which the applet is
running.
1.stop()
--------
The stop() method is called when a web browser leaves the HTML
document containing the applet (ex-when it goes to another page).
2.destroy()
-----------
The destroy() method is called when the environment determines that your
applet needs to be removed completely from memory.The stop() method is always
called before destroy().
setBackground()
---------------
void setBackground(Color newColor)
setForeground()
---------------
void setForeground(Color newColor)
Used to set the foreground color ie, the color in which text is shown.
Methods to obtain the current settings for the background and fore-ground
colors are
Color getBackground()
Color getForeground()
DOCUMENTBASE
------------
Java will allow the applet to load data from the directory that held the HTML
file that started the applet called DOCUMENTBASE.
CODE BASE
---------
The directory that the applet's class file was loaded from called CODE BASE.
1.Swing
2.Pluggable look and feel
3. java2D
4. Drag & Drop
5. Accessibility
packages
1. javax.swing
2. javax.swing.event
3. javax.swing.plaf
4. javax.swing.table
5. javax.swing.text
6. javax.swing.tree
7. javax.swing.undo
8. javax.swing.plaf.basic
9. javax.swing.plaf.multi
10. javax.swing.border
Advantages
3. MVC Architecture
Model, View Controller Architecture.
View - The properties that are set to the component.
Controller - The action that can be registered with the component.
Model - The appearance of the componant such as metal,motif or windows
These three will be running in a seperate thread in Swing architecture. View &
controller are combined in the component.
4. Keystroke handling
5. Action object
- Same component can referred to same action. For eg. a toolbar object and a
menu object can referred to same action.
6. Nested Container
Except containers such as JApplet,JFrame,JDialog & JWindow all other
components are lightweight component.
Heavyweight component are component that takes the appearance of the peer
objects. Lightweight components have its won appearance.
All lightweight component are placed over heavy weight component.
All awt component are heavy weight component.
A Container will be created from the heavy weight component inorder to add the
swing component. ContentPane class is used to create the container. ContentPane -
super class - LayerPane - super class - JRootPane
All swing components can be added only on the content pane.
8. Component Borders
9. Customized Dialogs
messagedialogs,confirms dialogs,inputdialogs...
Object
|
Component
|
Container
- Window
-JWindow
-Frame
-JFrame
-Dialog
-JDialog
Constructor:
It is a method which has the same name as the class name and is executed
automatically, when an instance for the class is created. A constructor can take
arguments.But, a constructor will not return any value.
Properties of a constructor:
1. A constructor has the same name as the class name.
2. A constructor will not return any value.
3. A constructor can be overloaded.
4. A constructor need not be explicitly called.
Types
=======
1. Default constructor.
It is the constructor that is called by default (i.e) when no other constructors are
defined in the class. It is the constructor that would be executed first.
2. Parametrized constructor.
Network :
A network is nothing but a connection of autonomous
computers and peripherals.Network enables the sharing of resources
and communication. Internet is a network of networks.Network in java
is possible through the use of java.net package.
Protocols
Communication in a network or a different network requires
certain set of rules called protocols.In java networking is done using
TCP/IP protocol(connection-oriented) and UDP protocol(conntion-less).
Some other protocols are,
TCP/IP :
Tcp/ip Sockets are the most reliable ,bi-directional ,stream
protocols.The server has to wait for the client's request.The server uses the ServerSocket
class and the client uses the Socker class to connect to the server.
The getInputStream method is used to receive the input from the
port and getOutputStream method is used to send the data to the port .
Socket
Socket is a one which act as medium of transfer of data in a
network in which TCP/IP act as protocol and IPaddress forms the
address of the Socket.
Client/Server
Computer which,requests for some service from another
computer is called as client.The one that processes the request is called as server.
InternetAddress
Every computer connected to a network has a unique
IP(internet protocol) address.It is 32 bit number which has four
numbers separated by periods. A sample IPaddress is 80.0.0.54
Datagram: -
DatagramPackets
Constructors in this class are
1)DatagramPacket(byte data[],int size);
2)DatagramPacket(byte data[],int size,InetAddress i ,int port);
DatagramSocket
Constructor used in this class are(Socket exceptio must be caught)
1)DatagramSocket s=new DatagramSocket();
2)DatagramSocket s=new DatagramSocket(int port);
C++ JAVA
1. Supports the usage of pointers No pointers support.
2. uses header files uses
packages.
3. implicit type conversion No implicit type conversion
that leads to imprecision.
4. supports operator overloading No support for
operator overloading.
5. memory allocation can be memory allocation can only
static or dynamic. be dynamic.
6. hasn't got a built-in support has got a built-in suppor
for multithreading. for multithreading.
7. supports multiple interheritance. supports
interfaces.
8. platform - dependent. Platform independent.
9. supports friend funs. no support for friend fun.
Inheritance:
Note:
The class that exists, is known as "base class or parent class" and the class whose
object gets the properties of the "base class" is known as "derived class or child class."
1. Single:
It is the type of inheritance where there is a single base class and a single
derived class.
2. Multilevel:
3. Hierarchical:
It is the type of inheritance where a base class has a minimum of two derived
classes (i.e) the properties of a single class are acquired by a minimum of two classes.
Interfaces:
Java does not supports mulitiple inheritance. To avoid this drawback, it supports
the concept of interfaces. An interface is an abstract collection of methods which can be
implemented by individual classes. An interface itself has no definition and should not
contain definition for the methods contained in it. A variable declared within a interface
becomes a final variable, (i.e) its value cannot be changed within the class that inherits
the interface.An interface is similar to an abstract class and no objects of an interface can
be created. But, a single class can inherit any no. of interfaces, which is not possible in
the case of an abstract class.
When a class inherits the properties of another class, the keyword "extends" is
used, when a class inherits the properties of an interface, the keyword "implements" is
used and when an interface inherits the properties of another interface, the keyword
"extends" is used.
Syntax:
where,
access_specifier, is either "public" or has the default value and
interface_name, is any valid identifier.
It is a java specific API that allows the users to connect to databases. The
medium for interaction is
the standard SQL(Structured Query Language). The classes and interfaces for a database
connectivity is contained in the "java.sql" package. The important classes and interfaces
of this api are,
Connection(interface)
DriverManager(class)
Statement(interface)
PreparedStatement(interface)
CallableStatement(interface)
ResultSet(interface)
Types(class)
DatabaseMetaData(interface)
ResultSetMetaData(interface)
Type 1 drivers use a bridge techology to connect a Java Client to an ODBC database
service. Sun's JdbcOdbc bridge is the most common type 1 driver.
These drivers are implemented using native code.
Type 2 drivers wrap a thin layer of Java around Database specific native code libraries.
For Oracle databases, the native code libraries might be based on the OCI(Oracle Call
Interface) libraries, which were originally designed for C/C++ programmers. Because
Type 2 drivers are implemented using native code, in some cases they have better
performance than their all_Java counterparts. However they add some risk, because a
defect in a driver's native code section can crash the entire server.
Type 4 drivers are the most common type of drivers used is real-time applications.
Written entirely in Java, Type 4 drivers understand database specific networking
protocols and can access the database directly without any additional software.
// Exception
/*Exceptions are errorenous events that occur during the execution of the program. They
can be ArithmeticException(occurs when a value is divided by zero etc),
NumberFormatException(occurs when a string is given as input for an interger variable),
NegativeArraySizeException(occurs when an array is creted with a negative size) etc.
In Java, the runtime exceptions can be handled using exception
handlingblocks and keywords("try","catch","throws","throw","finally"). */
Packages:
Syntax:
package <package_name>;
class <class name>
{
body of the class
}
interface <interface name>
{
body of the interface
}
import statement:
The "import" statement is used to reference the java packages in our programs.
There are three forms of import statements,
The java compiler and the java interpretter searches for the files with ".class"
extension in the path specified in the "classpath" environment variable. The current
working directory is, by default, included to the "classpath" environment variable. Hence
it is possible to compile and execute the programs from the current directory.
1. create a directory which has the same name as the package name.
2. include the "package" keyword as the first statement in the program along with the
package name(the name of the directory).
3. write the class declaration and implementation and save the file as class_name.java
within the directory.
4. compile using java compiler.
Overloading:
Overridding:
Streams:
File
marksupported()-True/false
mark()
reset()
File:
getName();
getPath();
getAbsolutePath()
isAbsolute();
isDirectory();
isFile();
isReadOnly()
Swing:
It is the major improvements in the JDK between versions 1.1 and 1.2.It is also
one of the key APIs of the Java foundations Classes(JFC).
The Swing provides a large number of useful GUI controls that originated with
Netscape's Internet Foundation classes(IFC).
The JComponenent class of javax.swing is the top level class of the swing
component hierarchy.It is a subclass of the java.awt.Container class.
AbstractButton:
The top-level button class.it is subclassed by JButton(traditional and enhanced
GUI buttons),JToggleButton(checkboxes and radio button) and JMenuItem(menus and
menu items).
JComboBox:
A combination of atext field and drop-down list.
JInternalFrame:
A frame that supports all standard frame operations and can be used as an internal
GUI compenent.The JDesktopIcon inner class is used to implement an iconified form of
an internal frame.
JLabel:
A label that may contain text,an image,or both.
JLayerPane:
A panel that support several layers.It is used to manage layered frames as a
desktop.
JList:
A list component that can be tailered in a variety of ways.
JMenuBar:
A menu bar that can be added to any container.It displays a pop-up menu of menu
items.
AWT
Swing
PLAF(Plugable Look and Feel)
2d
DND -> Drag and drop support
Accesibilty
Features of swing:
Synchronization
InterprocessCommunication
Exchange of data or message among threads.
wait() - make the thread to wait for data.
notify() - to notify the waiting thread,
notifyAll() - to notify all the waiting threads.
SERVLET:
Servlets are small programs that execute on the server side of a web
connection.Applets dynamically extend the fuctionality of a web browser,Servlets
dynamically extend the functionality of a web server.
Java Servlet Development Kit(JSDK): It contains the class libraries that you will
need to create servlets.A utility known as the servletrunner is also included,which enables
you to test some servlets that is created.
It allows seperate process to read data from the HTTP request and write data to
the HTTP response.A variety of different languages were used to build the programming
for CGI,including C,C++ and PERL(Practical Extraction
Reporting Language).
Interfaces:
//steps
set classpath=%classpath%;c:\javawebserver2.0\lib\servlet.jar
javac filename.java
url:
http://localhost:8080/servlet/filename -- servlet
In RMI the application is divided into objects. The objects communicate with each other
through an interface. This interface is used to access the remote object and its methods.
RMI passes objects by their true type, as a result the behaviour of those objects is not
changed when they are sent to another virtual machine. To develop the distribted
application using RMI we have to follow the steps given below,
1. Application Layer:
2. Proxy Layer:
The stub and skeleton file created using the RMI compiler which
represent the client and server side of a remote object, froms the proxy layer.The stub
acts on the client side of the application and the skel acts on the
server side of the application. The main function of proxy layer is marshaling and
unmarshaling the parameters.
Process of converting input parameters to the a format that can be transmitted
across the network is called marshaling.
Stub:
The stub is client side proxy of the remote object. It has three primary
responsibilities,
2. It works with JVM and RMI system on client machine to serialize any
arguments to a remote method call and sends this information to server machine.
3. The stub receives any result from the remote method and returns it to
the client.
Skeleton:
The skeleton is a server side proxy of the remote object. It has thre
primary responsibilities,
3. It receives any return value from this method call and works with the
JVM and RMI system on server machine to serialize this return
value and sends the information back to the client.
4. Transport Layer:
RMI registry:
javac <remote_interface_name>.java
javac <implementation of remote interface>.java
rmic <implementation of remote interface>
javac <client application>.java
javac <server application>.java