Professional Documents
Culture Documents
Java offers
• Platform independence
• Security
• Multi threading
• Automatic garbage collection.
Java Virtual Machine (JVM).
Java technology is based on the concept of a single Java virtual machine (JVM)
-- a translator between the language and the underlying software and
hardware.
We can develop and run Java on any device equipped with Java Virtual
Machine (JVM).
Simple Java Program
public class Example
{
public static void main (String args[])
{
System.out.println ("This is a simple Java program");
}
}
Variable
Variable serves as place holders in memory for data. Different type of
variables are used to store data in different format.
e.g.: int type can store integer values.
Data types
• byte • float
• short • double
• int • Boolean
• long • char
Derived Data types
• class
• array
Arrays
An array is a container object that holds a fixed number of values of a single type.
The length of an array is established when the array is created.
Each item in an array is called an element, and each element is accessed by its
numerical index.
Arrays
Declaration
Syntax Eg:
<data_type> []<array_name> int []marks;
<data_type> <array_name>[] int marks[];
Initializing at the time of declaration
Eg:
int marks[]={40,45,34};
Arrays
Operators
• Unary Operators
• Assignment Operators
• Arithmetic Operators
• Equality and Relational Operators
• Conditional Operators
• Type Comparison Operator
Operators
• Unary Operators
+ Unary Plus Operator(indicates +ve)
- Unary Minus
++ Increment Operator
-- Decrement Operator
! Logical Complement Operator
Operators
• Assignment Operators
= Simple Assignment Operator
• Arithmetic Operators
+ Additive Operator (used for String Concatenation)
- Substraction Operator
* Multiplication Operator
/ Division Operator
% Modulus Operator
Operators
• Equality and Relational Operators
== Equal to
!= Not Equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
Operators
• Conditional Operators
&& Conditional AND
|| Conditional OR
?: Ternary Operator
Control Statements
Statements that control the flow of a program .
• if-else
• switch
• break
• continue
Loops
Loops are programming constructs which enables
repeated execution of a specific code.
• while
• do-while
• for
Classes and Objects
•The finally block always executes when the try block exits.
•It allows the programmer to avoid having cleanup code accidentally bypassed
by a return, continue, or break.
Throw statement
throw someThrowableObject;
• All methods use the throw statement to throw an exception. The throw
statement requires a single argument: a throwable object.
•Throwable objects are instances of any subclass of the Throwable class.
Throwable Class
and Its Subclasses
Error class:- When a dynamic linking failure or other hard failure in the Java virtual
machine occurs, the virtual machine throws an Error.
Simple programs typically do not catch or throw Errors.
Exception Class:-An Exception indicates that a problem occurred, but it is not a serious
system problem.
The Java platform defines the many descendants of the Exception class.
These descendants indicate various types of exceptions that can occur.
Throws statement
modifiers> <return type> <method name>(<parameter list>) throws <list of Throwables>
{
/* Java statements */
}
•Introduction to Streams
1.Node Streams
2.Filter Streams
3.Byte Streams
4.Character Streams
Introduction to Streams
•A Stream is a sequence of bytes travelling from a source to a destination over a
communication path.
•When a stream of data is being sent, it is said to be written and when a stream
of data is being received it is said to be read.
•Data in a stream can flow in one direction only. So we use separate streams for
reading and writing data.
•Java offers
1.java.io.InputStream class to read bytes of data from a source(keyboard).
2.java.io.OutputStream class to write bytes of data to a destination.
Node Streams
Input and Output streams that read from or write to a specific location such as
a disk file or memory are called a Node Stream.
Eg: FileInputStream, FileOutputStream.
Filter Streams
Filter streams are used to read data from one stream and write it to another.
Eg: PrintStream
Byte Streams
Byte streams work on eight bits of data.
Character Streams
Streams that work on 16-bit Unicode characters are called character streams.
Eg:InputStreamReader,OutputStreamReader
Java Database Connectivity
JDBC is Java application programming interface that allows the Java programmers to
access database management system from Java code.
Java applications need to communicate with a database to perform tasks such as
to
•Store and update data in a database.
•Retrieve the data stored in a database and present it in a proper format to users.
Java Database Connectivity
Sun Microsystems has introduced JDBC API as a part of JDK to enable Java
Applications to communicate with a database. JDBC API takes care of converting Java
commands to generic SQL queries.
JDBC API submits queries to the JDBC Driver which in turn converts queries to a form
that a particular DBMS/RDBMS can understand.
Java Applets
Applets are web based programs that can be downloaded and can
execute on a computer. Applets run in a java enabled browser such
as Internet Explorer, Netscape navigator etc. These are said to be
java enabled because they have a built in java platform(JVM and
Java API).
Life cycle of a Java Applets
Life cycle of an applets is implemented using the following methods.
init() Method
• is called the first time when an applet is loaded in the memory.
•Using this can initialize variables and add components.
start()Method
• is called immediately after the init() and every time an applet receives focus.
•use this method to restart a process.
stop() Method
•is called every time an applet loses its focus.
•can use this method to reset variables and stop the threads that are running.
destroy() Method
•Is called when a user moves to another page.
•Can use this method to perform clean up operations like closing a file.
Java AWT(Abstract Window Toolkit)
A class library is provided by the Java programming language which is
known as Abstract Window Toolkit (AWT). A common set of tools is
provided by the AWT for graphical user interface design.
1. Flow Layout
2.Grid Layout
3.Border Layout
4.Card Layout
5.Grid Bag Layout
6.Box Layout
Event Driven Programming
The program waits for a user to perform an action. Each action that the user
performs on the GUI, translates to a call to a function that handles the event. This
approach is called event-driven programming.
There are many types of events that are generated by your AWT application. These
events are used to make the application more effective and efficient. Generally, there
are twelve types of event are used in Java AWT.
The Delegation Event Model
A source generates an event and sends it to one or more listeners. The listener simply
waits until it receives an event. Once received the listener processes the event and
then returns.
Event
An event is an object that describes a state change in a source.
Components of an Event
An event object comprises of three components.
1.Event object
An event object contains the information related to the event such as
category of the event, component that generated the event, and the time when the
event occurred.
2.Event Source
3.Event Handler
Event Listeners
A listener is an object that is notified when an event occurs. It has two major
requirements.
• It must be registered with one or more sources
• It must implement methods to receive and process the notification
The methods that receive and process events are defined in a set of interfaces found
in java.awt.event
Event Classes
Java.util.EventObject
Java.awt.AWTEvent
Item Event Adjustment Event Component Event Action Event Text Event
Focus Event Container Event Input Event Paint Event Window Event
Adapter classes help us in avoiding the implementation of the empty method bodies.
Generally an adapter class is there for each listener interface having more than one
method. Event package provides seven adapter classes.
Adapter classes
Adapter classes help us in avoiding the implementation of the empty method bodies.
Generally an adapter class is there for each listener interface having more than one
method. Event package provides seven adapter classes.