Download Chapter 2

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Chapter 2
The Java Overview
1
Prepared By E.Musa Alyaman
Chapter 2 Outline
•
•
•
What is Java?
The Java Programming Language
The Java Platform
–
–
•
•
The Java API
Applications of Java Network Programming
–
–
–
–
–
•
•
2
The Java Virtual Machine
Java Runtime Environment
Network clients
Games
Software agents
Web Applications
Distributed Systems
Java Language Issues
1. Exception Handling in Java
2. What are Exceptions
3. Types of Exceptions
4. Handling exceptions
5. Causes of exceptions
Chapter 2 Highlights
Prepared By E.Musa Alyaman
What is Java?
•
The name Java is applied to a variety of technologies
created by Sun Microsystems
•
There are three main components of Java:
1. The Java Programming Language – a programming
language used to write software for the Java platform
2. The Java platform – a range of runtime environments that
supports execution of software written in Java
3. The Java API – a rich, fully featured class library that
provides graphical user interface, data storage, data
processing, I/O and networking support
3
Prepared By E.Musa Alyaman
The Java Programming Language
• Properties of the Java language
•
•
•
•
•
•
•
4
Object orientation
Simplicity
Automatic garbage collection
Portability
Multi-threaded programming
Security
Internet awareness
Prepared By E.Musa Alyaman
The Java Platform
• Third-generation language
• The source code instructions written in
Java must be compiled to a form that the
computer is capable to understand
• Most languages would be compiled to
machine native code, hence running on a
specific CPU architecture.
• Problem with distribution of the program
5
Prepared By E.Musa Alyaman
The Java Platform
• The Java platform takes a different approach.
• Instead of creating machine code for particular
pieces of hardware, Java source code is
compiled to run on a single CPU
• Java machine code or bytecode is executed by a
special software that mimics a CPU chip capable
of understanding bytecode
• The software is called Java Virtual Machine
(JVM)
6
Prepared By E.Musa Alyaman
The Java Virtual Machine
• An emulation of hardware device
• Write Once, Run Anywhere (WORA)
• JVM allows Java to be portable across
different type of OS
• Flexibility vs performance issues
• However, advancement of CPU
performance “covers” Java weakness
7
Prepared By E.Musa Alyaman
Java Runtime Environment
• JVM is not a software application that can itself
be run
• Usually, the JVM is hosted within a Java runtime
environment (JRE)
• JRE will also include the core classes from the
Java API and other supporting files.
• J2SE, J2EE, J2ME
8
Prepared By E.Musa Alyaman
The Java API
• The API provides a rich suite of classes and components that
allows Java to do real work, such as:
• Reading from and writing to files on the local hard drive
• Creating graphical user interfaces with menus, buttons,
text fields and drop-down lists
• Drawing pictures from graphical primitives such as lines,
circles, squares and ellipses
• Assessing network resources such as Web sites or
network servers
• Storing data in data structures such as linked lists and
arrays
• Manipulating and processing data such as text and
numbers
• Retrieving information from databases or modifying
records
9
Prepared By E.Musa Alyaman
Java Networking Considerations
• “Ideal” language for network programming
• However, Java does not provide low-level
access to Internet Protocols
• Java imposes severe security restrictions
on Java applets
10
Prepared By E.Musa Alyaman
Network clients
• A common use for Java is to create
network clients such as:
• Mail readers
• Remote file transfer application
• Browser
11
Prepared By E.Musa Alyaman
Games
• One major application of network
communication is the multiplayer games
that run over a LAN or online games which
run over the Internet
12
Prepared By E.Musa Alyaman
Software agents
• A software that acts on the behalf of one
or more users, to perform specific
commands and tasks or to fulfill a set of
goals
• Examples:
– An agent to sort through email message
– An agent that searches for information on the Web
– An agent that monitors a source of information for
changes relating to the interest of a user
13
Prepared By E.Musa Alyaman
Web Applications
• One of the most important areas for Java
network programming
• Applets
• Server-side Java
14
Prepared By E.Musa Alyaman
Distributed Systems
• Used to solve very complex and large
problems
• Resources may be distributed across an
organization
• Remote Method Invocation (RMI) and
Common Object Request Broker
Architecture (CORBA) make this possible
15
Prepared By E.Musa Alyaman
Java Language Issues
•
16
In network programming using Java,
there are some issues that we need to be
aware of
1. Exception Handling in Java
2. What are Exceptions
3. Types of Exceptions
4. Handling exceptions
5. Causes of exceptions
Prepared By E.Musa Alyaman
Exception Handling in Java
A mechanism for dealing with errors that
occur in software at runtime. For example,
while attempting to read from a file, an
application may be unable to proceed
because the file is missing (occur at
runtime).
17
Prepared By E.Musa Alyaman
What are Exceptions
• Unusual conditions that occur at runtime and are
represented as objects
• Track information about errors condition, making it
possible to diagnose the cause of the problem or at least
to provide clues as to why it occurred.
• “throw” the exception and pass it to the calling method.
• The calling method may choose to handle the error
condition and “catch” the exception or it may throw the
exception to its calling method.
• Method will generate the exception that indicates the
type of exception will be thrown.
• At some point in the code, the exceptions need to be
caught and dealt with accordingly
18
Prepared By E.Musa Alyaman
Types of Exceptions
• The types of exception vary depending on
the classes that are being used
• Compilation Error examples:
• AWTError – serious error occurs in the Abstract
Windowing Toolkit
• NoClassDefFoundError – thrown when the JVM is
unable to locate the class definition file (.class) for
a class
• OutOfMemoryError – occurs when JVM can no
longer allocate memory to objects
19
Prepared By E.Musa Alyaman
Types of Exceptions
• Runtime error examples:
– NoSuchElementException – thrown when an
attempt is made to access the next element of
an enumeration but all elements have been
exhausted
– NullPointerException – thrown when an
attempt to reference an object has been made
but the reference was null
– SecurityException – thrown by the current
security manager when an attempt to access
a resource, object or method has been made
but not permitted
20
Prepared By E.Musa Alyaman
Handling Exceptions
• Java provides three statements for
handling exceptions:
1. try
2. catch
3. finally
21
Prepared By E.Musa Alyaman
try statement
– The try statement indicates a block of code
that can generate exception
//code outside of try block should not throw an exception
try {
// do something that could generate an exception…
}
//handle exception…
22
Prepared By E.Musa Alyaman
catch statement
The catch statement is used to catch exceptions
thrown within a try block of code.
//try block can generate exceptions
try {
//generate an exception
}
catch (SocketException se)
{
System.err.println(“Socket error reading from host: “ + se);
System.exit(2);
}
catch (Exception e)
{
System.err.println(“Error:” + e);
System.exit(1);
}
23
Prepared By E.Musa Alyaman
finally statement
The finally statement is a generic catchall for cleaning
up after a try block.
//try block can generate exceptions
try {
//generate an exception
}
catch (SomeException some)
{
//handle some exception
}
finally
{
//clean up after try block, regardless of any
//exceptions that are thrown
}
24
Prepared By E.Musa Alyaman
Causes of exceptions
• In networking, the most common cause of
exceptions is related to the state of the
network connection:
– Loss of connection due to congestion
– Server/host behind firewall that block external
requests
• Other may be due to security measures
imposed by the browser or by Java
security policy or security manager.
25
Prepared By E.Musa Alyaman
Development Tools
• Java SDK version 6 (the latest)
• IDE
– NetBeans
– Eclipse
– JCreator
– TextPad
26
Prepared By E.Musa Alyaman
Chapter Highlights
• You have learned
– The history of Java, design goals and
properties of the Java language
– About compiled bytecode, the Java Virtual
Machine and Java Runtime Environment
– The core Java API and some Java extensions
– Some issue and consideration related to Java
– Example of applications
– Exception handling
– Development tools
27
Prepared By E.Musa Alyaman