Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
The Java Language
Topics of this Course
Introduction to Java
The Java Language
Object Oriented Programming in Java
Exceptions Handling
Threads
Applet
GUI
Networking
Chapter 1
Introduction to Java
Bibliography
History
What is Java?
Environments
Security
Memory Managment
Java Syntax
Bibliography
The Java Programming Language –
Ken Arnold,James Gosling
The Java Tutorial - Third Edition
Mary Campione & Kathy Walrath
Addison Wesley
Online :
http://java.sun.com/docs/books/tutorial/index.html
History
1991 - language for consumer devices
– small, tight,efficient
– OOP
– C++ ruled out
– named Oak, name collision, became
Java
History
1994 Browsers - static pages of html.
Gosling realized that Java could provide
an architecture neutral browser with
downloadable and executable content.
J. Gosling, J. Payne, P. Naughton build
HOTJAVA
Demonstration of technology at
SunWorld May 23, 1995 was “Hit” of
show.
Netscape releases Java interpreter in
2.0
Versions
1.0 initial release
1.1
– major changes to fix event handling
problems in 1.0
– support new and needed
methodologies (read that as beans)
1.2 , 1.3(AKA Java 2)
mostly expanding capabilities.
What is Java ?
An Object-oriented Programming
Language
A Virtual Machine Architecture (JVM)
Platform independent and secure
(portable bytecodes)
A feature for dynamic WEB pages
A program library
Development tools
Virtual Machine
Java is both compiled and interpreted
language
Java source turned into simple binary
instructions
C/C++ source is refined to native
instructions for a particular processor
Java compiled into a universal format instructions for a virtual machine.
Compiled Java byte-code (J-code) is
executed by Java run-time interpreter in
a safe, virtual environment .
Executes stack-based instruction set,
manages a storage heap, creates and
manipulates primitive data types.
Executes in accordance with a strictly
defined open specification that can be
implemented by anyone on any
platform.
Java Interpreter
Java interpreter is lightweight and
small.
Written in C/C++
Interpreter can be run as
– separate, self-standing, application
– embedded in another piece of
software, such as Web browser
Java code is implicitly portable
Source
code
Java Runtime
Byte
Code
Java
Runtime
UNIX
PC
Macintosh
...
Environments
javac SomeName.java
– a class named SomeName is defined in this
file.
java SomeName
– instantiates the SomeName.class and starts
the static public void main(String[] x) {}
method.
appletviewer File.html
– file contains an applet. The browser loads
the applet, calls the init(), then start()
methods. If applet becomes invisible the
stop() method is called. After a time the
destroy() method is called.
Environments
package means directory.
package mechanism is a compile time
(javac) visibility issue, not a #include,
and does not mean the interpreter
(java) will find the classes it needs.
work in a directory and be sure your
interpreter looks for classes in your
current directory. CLASSPATH and
PATH variables are sometimes required
to fix visibility issues.
Applet vs. Application
Application - normal Java program
Applet - program to be executed by a
WEB browser
Applets
• typically loaded from some URL on
the WEB before execution
• new thing which makes Java
popular (dynamic pages)
“Just in Time” Compilation
Interpreted languages slow
Java is considered to be a fast
interpreted language - interpreter
executes compiled byte-code.
Software implementations of the runtime system can optimize performance
by compiling byte-code to native
machine code on the fly.
Security
Java provides several layers of
protection from dangerously flawed
code, viruses and Trojan horses.
Java virtual machine architecture
assesses the safety of the code before
it’s run.
These features provide foundation for
high-level security policies.
Security
is essential when running applets over
the net
three layers of security
–
language
– bytecode (verified before execution)
– runtime (security manager to prevent
unallowed IO and net operations)
Language Security
No direct memory addressing allowed
No pointer arithmetic
Automatic storage release with a
garbage collector.
All casts checked at runtime.
All array accesses checked at
runtime.
Memory Managment
The most important differences
between Java and C/C++ involve how
Java manages memory.
Java
– eliminates add hoc pointers
– adds garbage collection
– adds true arrays
These features eliminate many
problems related to safety, portability
and optimization.
Memory Managment
Explicit memory allocation and
deallocation are considered the largest
source of programming errors in C/C++
Java maintains objects in memory as
well as tracks all references to those
objects. When an object is no longer in
use, Java removes it from memory.
Garbage collector runs in the
background. This means that most
garbage collection is done between
events (mouse clicks, keyboard hits,…)
Memory Managment
Java does not have pointers as we know
them in C/C++. However, Java provides
references - safe pointer.
A reference is a strongly-typed handle
for an object. All objects, except
primitive numeric types, are accessed
through references.
References can be used for building
linked lists, trees and other data
structures.
Memory Managment
Pointer arithmetic can’t be done using
references.
References are passed by value.
References can’t reference an object
through more than a single level of
indirection.
Protection of references is one of the
fundamental aspects of Java security.
This means that references can’t
examine memory locations that should
not be examined.
Java Syntax
Java does not allow programmerdefined operator overloading as we
know it in C++, for instance.
The string concatenation operator + is
the only system-defined, overloaded
operator in Java.
All methods in Java are like C++ virtual
methods, so overridden methods are
dynamically selected at run-time.
Java Syntax
Java does not have a preprocessor, so it
doesn’t have
– macros
– #define statements
– conditional source compilation
Other languages need these constructs
in order to address various systemdependent issues.
Another use for conditional compilation
is for debugging purposes.
Java Syntax
Debugging code in Java can be included
directly in Java source code by making it
conditional on a constant (static vs.
final) variable. Java compiler takes care
of this at compile time - it simply
removes it when it determines that it will
not be called.
Java also provides a well-defined
package structure for organizing class
files. The compiler works with compiled
Java classes (all the info is there).
Arrays
Arrays in Java are first-class citizens.
Arrays...
– can be dynamically allocated and
assigned like other objects
– know their own size and type
True arrays eliminate the need for
pointer arithmetic.
Classes
Fundamental unit of Java
Class is an application component that
holds executable code and data
Java classes are distributed in a
universal binary format that contains
Java byte-code and other class
information.
Classes can be maintained discretely
and stored in files or archives (local or
net)
Classes are located and loaded
Error Handling
Java was first written for the networked
devices and embedded systems. This is
why Java provides robustness and
intelligent error management.
Java has powerful exception-handling
mechanism. Allows us to separate errorhandling code from normal code cleaner&readable code.
MultiThreading
Today’s apps require parallelism
(unrelated tasks must execute, if possible,
at the same time)
Threads provide efficient multiprocessing
and distribution of tasks.
Java makes threads easy to use - support
built into the language
Synchronization of threads
Java’s support of synchronization is
based “lock and key system for accessing
resources”.