Download Introduction to Java

Document related concepts
no text concepts found
Transcript
1
is
high
level
programming language like
C++, Visual Basic, developed
by James Gosling and Patrick
Naughton at Sun Microsystems
in 1991.
2
You can use Java to write computer
applications of numbers, process
words, play games, store data or do
any of the thousands of other things
computer software can do. Java is
most similar to C and C++.
The special programs called Applets
that can be downloaded from the
Internet and played safely within a web
browser.
3
4
You
may
wonder
how
Sun’s
engineers were able to develop such
a powerful software development
environment and surpass all known
programming languages.
The origins of Java trace back to
1991, when Sun was investigating
consumer electronics products.
5
James Gosling, the father of Java,
was intent on building a low-cost,
hardware-independent
software
platform using C++. For a number of
technical
reasons,
C++
was
dropped, and a new language, called
Oak, was developed, based on C++,
but eliminating its shortcomings.
6
Oak was used to develop a small
electronics device called “Star 7”. By
1994 the Web emerged, Oak was
renamed Java, Java was used as
the basis for a Web browser, called
WebRunner.
WebRunner
was
successfully demonstrated, and the
Java/HotJava project took off.
7
HotJava,
Java,
and
the
Java
documentation and source code were
made available over the Web, as an
alpha
version,
in
early
1995.
Initially Java was hosted on SPARC
Solaris, and then on Windows NT.
In the summer of 1995, Java was
ported to Windows 95 and Linux.
8
In the fall of 1995 the Java Beta 1
version was released through Sun's
Web site, and Java support was
introduced
in
the
Netscape
2.0
browser.
In December 1995 the Java Beta 2
version was released, and JavaScript
was announced by Sun and Netscape.
9
Java's success became inevitable
when,
in
early
December,
both
Microsoft and IBM announced their
intention to license Java technology.
On January 23, 1996, Java 1.0 was
officially released and made available
for download over the Internet.
10
11
1- Java is a Platform
2- Java is Simple
3- Java is Object-Oriented
4- Java is Platform Independent
5- Java is Safe
12
6- Java is High Performance
7- Java is Multi-Threaded
8- Java is Dynamically linked
9- Java is Garbage Collected
10- Network Facilities
13
JAVA IS A PLATFORM
Java is a platform for application
development. A platform is a
loosely defined computer industry
that
typically
means
some
combination of hardware and
system software that will mostly
run all the same software.
14
Computer
programs
are
very
closely tied to the specific hardware
and operating system they run. A
Windows program will not run on a
computer that only runs DOS. A
Mac application can't run on a Unix
workstation and so on.
15
Java solves the problem of platformindependence by using BYTECODE.
The Java compiler does not produce
native
executable
code
for
a
particular machine like a C compiler
would. Instead it produces a special
format called BYTECODE.
16
The interpreter reads the
BYTECODE and translates it into
the native language of the host
machine. Since the BYTECODE is
completely platform independent,
compiler and most of the class
libraries are written in Java
Platform.
17
All these pieces, the javac
(Compiler), the java (Interpreter),
the Java Programming Language,
and more are collectively referred
to as Java Platform.
18
JAVA IS SIMPLE
Java was designed to make it much
easier to write bug free code. The
most
important
part
of
helping
programmers write bug-free code is
keeping the language simple.
19
About half of the bugs in C and
C++ programs are related to
Memory
Allocation
and
Deallocation.
Therefore
the
second important addition Java
makes to providing bug-free code is
Automatic memory allocation and
deallocation.
20
JAVA IS OBJECT-ORIENTED
In object-oriented programs data is
represented by objects. Objects
have two sections, fields (instance
variables) and methods. Fields tell
you what an object is. Methods tell
you what an object does.
21
Object oriented programming have
a number of advantages including
Simpler, easier to read programs,
More efficient reuse of code,
Faster time to market and More
powerful, error-free code.
Object-oriented language C++ is a
complex, difficult language.
22
JAVA IS PLATFORM
INDEPENDENT
Java is compiled to an intermediate
form called BYTECODE. A Java
program never really executes
natively on the host machine.
Rather a special native program
called the Java interpreter reads
the BYTECODE
23
The BYTECODES are precisely
defined and remain the same on all
platforms.
In Java everything is guaranteed.
However the Virtual Machine (VM)
itself and some parts of the class
library must be written in native
code.
24
JAVA IS SAFE
Most notably there are no Pointers
in Java. Java programs cannot
access optional addresses in
memory. All memory access is
handled behind the scenes by the
trusted
runtime
environment.
Furthermore Java performs strong
type checking.
25
JAVA IS DYNAMICALLY
LINKED
Java source code is divided into
.java files, roughly one per each
class in your program. The
compiler compiles these into .class
files containing BYTECODE. Each
.java file generally produces
exactly one .class file.
26
The compiler searches the current
directory and directories specified
in the CLASSPATH environment
variable to find other classes
explicitly referenced by name in
each source code file.
Furthermore, Java .class files tend
to be quite small, a few kilobytes at
most.
27
JAVA IS GARBAGE COLLECTED
You do not need to explicitly
allocate or deallocate memory in
Java. Memory is allocated and
reclaimed
by
the
Garbage
Collector when it is no longer
needed.
28
There are constructors and these
do allocate memory but this is
transparent to the programmer.
Most
Java
virtual
machines
(JVM) use an inefficient, mark
and sweep garbage collector.
29
NETWORK FACILITIES
Java has an extensive library of
routines for handling easily with TCP/IP
protocols like HTTP and FTP. This
makes creating network connections
much easier than in C or C++.
Java applications can open and access
objects across the net via URLs same
as local file access system.
30
JAVA vs C++
1. No Preprocessor #define in Java.
2. No Pointers in Java.
3. No Structures and Unions in Java.
4. No Multiple Inheritance in Java.
31
5. No need to make null terminated
characters array for Strings Java
have built in support for Strings.
6. No goto statement in Java.
7. No operator overloading in Java.
8. No Global Functions in Java.
9. No Global Variables in Java.
32
33
JAVA’s main strength is its
unique architecture. Java
provides
facilities
of
networking as well as at the
same time works on a wide
range of platforms.
34
Most
compilers
produce
machine code (understood
by the central processor).
Some produce intermediate
code (not understood by the
central processor).
35
But, Java compiler generates
system
architecture
independent BYTECODES
easy to translate, into lowlevel
language.
Java
BYTECODE executed by
interpreter.
36
BYTECODE
is
also
interpreted within a browser.
With most programming
languages,
you
either
COMPILE or INTERPRET a
program so that you can run
it on your computer.
37
The
Java
programming
language is unusual in that a
program is both compiled
and interpreted.
With the compiler, first you
translate a program into an
intermediate format called
38
JAVA
BYTECODES
the
platform-independent codes
interpreted by the interpreter
on the Java platform. The
interpreter parses and runs
each
Java
BYTECODE
instruction on the computer.
39
Compilation happens just once;
interpretation occurs each time the
program is executed.
40
The BYTECODES can be
executed by only a Java
Virtual
Machine
(JVM),
which
is
usually
implemented in software
rather than hardware.
41
You can think of Java
BYTECODES
as
the
machine code instructions
for
the
Java
Virtual
Machine.
42
Java
help
BYTECODES
make
(write once, run
anywhere)
possible.
43
It means that as long as a
computer has a Java virtual
machine (JVM), the same
program written in the Java
programming language can
run
on
Unix,
Linux,
Windows 2000 or on a
Solaris workstation.
44
45
To
execute
Java
BYTECODES, the VM uses
a CLASS LOADER to load
the BYTECODES from a
disk or a network. Each
class file is checked by a
BYTECODE VERIFIER
46
that ensures the class is
formatted correctly and will
not corrupt memory when it
is executed. The execution
unit of the VM is an
INTERPRETER
which
interpret the bytecode.
47
48
49
A Platform is the hardware
or software environment in
which a program runs.
We know about some of
the most popular platforms
like Windows 2000, Linux,
Solaris, and MacOS.
50
Java Platform Components
1- The Java Virtual
Machine (JVM)
2- The Java Application
Programming Interface
(Java API)
51
The JVM is the base for
the Java platform and is
ported
onto
various
hardware-based platforms.
52
The Java API is a large
collection of ready-made
software components that
provide
many
useful
capabilities,
such
as
graphical user interface
(GUI) widgets.
53
The Java API is grouped
into
libraries
classes
and
of
related
interfaces;
these libraries are known
as packages.
54
55
PHASE 1 (WRITE)
Program is created in
the editor(like Notpad)
and stored on disk with
an extension of .java
(filename.java).
56
PHASE 2 (COMPILE)
Compiler
creates
BYTECODES and stores
them on disk by using an
executable
program
(javac.exe).
57
The
compiled
BYTECODE file has an
extension of .class.
The
filename.java
produces another file
filename.class.
58
Syntax:
C:\> javac filename.java
59
PHASE 3 (LOAD)
Class
loader
puts
BYTECODES
into
memory before execution.
There are two types of
programs in Java
60
APPLICATIONS
A program that is stored
and executed from the
user’s local computer.
Applications are executed
using the Java Interpreter
with a utility “JAVA.EXE”.
61
APPLETS
A small program that is
stored
on
a
remote
computer (that is accessed
via World Wide Web
(WWW) Browser).
62
Applets are loaded from a
remote computer into the
browser of user’s local
machine, executed in local
browser and discarded
when execution completes.
63
Applets also can be viewed
by a utility called
“APPLETVIEWER.EXE”
64
Applets (.class) files are
embedded
into
HTML
pages using <APPLET>
Tag and that HTML page
can be viewed in the
browser or appletivewer.
65
Syntax for Applet Tag
<Applet code=“filename.class”
height=200
width=300>
</Applet>
66
Syntax for Application
C:\> java filename.class
67
Syntax for Applet
C:\> appletviewer filename.html
68
69
JDK is a complete set of
tools for java development,
testing,
documentation,
and execution of java
programs and applets. It
provides the followings
important programs.
70
javac
Java Compiler
java
Java Interpreter
javadoc
Documentation
Generator
appletviewer Applet viewer
71
When you install the latest
version of JDK which is
JDK1.6 on C:\ drive, All
these
programs
are
available in the following
path.
C:\program files\jdk1.6.0_14\bin
72