Intro To Java API

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 27

Introduction to Java API

Core Java
Page:1

Copyrights 2009
BVU Amplify DITM

What is Java?
Java (with a capital J) is a high-level, third generation
programming language, like C, Fortran, Smalltalk, Perl,
and many others.
You can use Java to write computer applications that
crunch numbers, process words, play games, store data
or do any of the thousands of other things computer
software can do.
Compared to other programming languages, Java is most
similar to C. However although Java shares much of C's
syntax, it is not C. Knowing how to program in C or,
better yet, C++, will certainly help you to learn Java
more quickly, but you don't need to know C to learn
Java. Unlike C++ Java is not a superset of C.
Core Java
Page:2

Copyrights 2009
BVU Amplify DITM

What is Java
A Java compiler won't compile C code, and most large C
programs need to be changed substantially before they
can become Java programs.
What's most special about Java in relation to other
programming languages is that it lets you write special
programs called applets that can be downloaded from
the Internet and played safely within a web browser.
A Java applet cannot write to your hard disk without
your permission. It cannot write to arbitrary addresses
in memory and thereby introduce a virus into your
computer. It should not crash your system.
Core Java
Page:3

Copyrights 2009
BVU Amplify DITM

What Is the JavaTechnology?


Java technology is:
A programming language
A development environment
An application environment
A deployment environment
It is similar in syntax to C++.
It is used for developing both applets and applications.

Core Java
Page:4

Copyrights 2009
BVU Amplify DITM

Primary Goals of the Java Technology


Provides an easy-to-use language by:
Avoiding many pitfalls of other languages
Being object-oriented
Enabling users to create streamlined and clear code
Provides an interpreted environment for:
Improved speed of development
Code portability
Enables users to run more than one thread of activity
Loads classes dynamically; that is, at the time they are actually
needed
Supports changing programs dynamically during runtime by loading
classes from disparate sources
Furnishes better security
Core Java
Page:5

Copyrights 2009
BVU Amplify DITM

Primary Goals of the Java Technology


The following features fulfill these goals:
The Java Virtual Machine (JVM)1
Garbage collection
The Java Runtime Environment (JRE)
JVM tool interface

Core Java
Page:6

Copyrights 2009
BVU Amplify DITM

A Brief History
January 1996: first official release JDK 1.0
Web: applets, security, URL, networking
GUI: Abstract Windows Toolkit (AWT)

February 1997: JDK 1.1


Authentication: digital signatures, certificates
Distributed computing: RMI, object serialization, Java
IDL/CORBA

Database connectivity: JDBC


Component architecture: JavaBean
Core Java
Page:7

Copyrights 2009
BVU Amplify DITM

A Brief History (contd)


December 1998: Java 2 Platform (JDK 1.2):
Standard (J2SE), Enterprise (J2EE), and Micro (J2ME) Editions
JFC: Swing, Java2D, Java3D
Java Cryptography Extension (JCE)
Enterprise computing: enterprise JavaBean (EJB), servlets,
Java server page (JSP), Jini, XML
Java Multimedia Framework (JMF)
Embedded systems: KVM, JavaCard
Performance enhancement: JIT, HotSpot VM
Core Java
Page:8

Copyrights 2009
BVU Amplify DITM

A Brief History (contd)


May 2000: J2SDK v1.3
Enhancements in features, performance, and security.

May 2001: J2SDK v1.4 beta


XML, XSLT, JCE, etc. all included in J2SE

Numerous enahnacements: I/O, assertions, regular


expression, logging, etc.

2002(?): J2SDK v1.5


Generic types
Core Java
Page:9

Copyrights 2009
BVU Amplify DITM

Java is a Platform
Java (with a capital J) is a platform for application development.
A platform is a loosely defined computer industry buzzword that
typically means some combination of hardware and system software
that will mostly run all the same software.
Java solves the problem of platform-independence by using byte code.
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 byte code. Java byte code written in
hexadecimal, byte by byte, looks like this:
CA FE BA BE 00 03 00 2D 00 3E 08 00 3B 08 00 01 08 00 20 08

This looks a lot like machine language, but unlike machine language
Java byte code is exactly the same on every platform. This byte code
fragment means the same thing on a Solaris workstation as it does on a
Macintosh PowerBook.
Core Java

Page:10

Copyrights 2009
BVU Amplify DITM

Java is a Platform
Java programs that have been compiled into byte code still need an
interpreter to execute them on any given platform.

The interpreter reads the byte code and translates it into the native
language of the host machine on the fly.
The most common such interpreter is Sun's program java (with a little j).
Since the byte code is completely platform independent, only the
interpreter and a few native libraries need to be ported to get Java to run
on a new computer or operating system.
The rest of the runtime environment including the compiler and most of
the class libraries are written in Java. All these pieces, the javac
compiler, the java interpreter, the Java programming language, and more
are collectively referred to as Java.
Core Java
Page:11

Copyrights 2009
BVU Amplify DITM

Java is Simple
Java was designed to make it much easier to write bug free code.
Java has the bare bones functionality needed to implement its rich
feature set. It does not add lots of syntactic sugar or unnecessary
features. Despite its simplicity Java has considerably more functionality
than C, primarily because of the large class library.
Because Java is simple, it is easy to read and write. Obfuscated Java isn't
nearly as common as obfuscated C. There aren't a lot of special cases or
tricks that will confuse beginners.
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. The C library memory allocation functions malloc() and
free() are gone as are C++'s destructors.
Core Java
Page:12

Copyrights 2009
BVU Amplify DITM

Java is Simple
Java is an excellent teaching language, and an
excellent choice with which to learn programming.
The language is small so it's easy to become fluent.
The language is interpreted so the compile-run-link
cycle is much shorter.

Core Java
Page:13

Copyrights 2009
BVU Amplify DITM

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. These
fields and methods are closely tied to the object's real world
characteristics and behavior. When a program is run messages are passed
back and forth between objects. When an object receives a message it
responds accordingly as defined by its methods.
Object oriented programming is alleged to have a number of advantages
including:
Simpler, easier to read programs
More efficient reuse of code
Faster time to market
More robust, error-free code
Core Java
Page:14

Copyrights 2009
BVU Amplify DITM

Java is Object-Oriented
In practice object-oriented programs have been just as
slow, expensive and buggy as traditional non-objectoriented programs.
In large part this is because the most popular object-

oriented language is C++. C++ is a complex, difficult


language that shares all the obfuscation of C while
sharing none of C's efficiencies. It is possible in practice

to write clean, easy-to-read Java code.


Core Java
Page:15

Copyrights 2009
BVU Amplify DITM

Java is Platform Independent


Java was designed to not only be cross-platform in source form like C, but
also in compiled binary form.
Since this is frankly impossible across processor architectures Java is
compiled to an intermediate form called byte-code.
A Java program never really executes natively on the host machine.
Rather a special native program called the Java interpreter reads the
byte code and executes the corresponding native machine instructions.
Thus to port Java programs to a new platform all that is needed is to port
the interpreter and some of the library routines.
Even the compiler is written in Java. The byte codes are precisely
defined, and remain the same on all platforms.
Core Java
Page:16

Copyrights 2009
BVU Amplify DITM

Java is Platform Independent


The second important part of making Java cross-platform is the
elimination of undefined or architecture dependent constructs.
Integers are always four bytes long, and floating point variables
follow the IEEE 754 standard for computer arithmetic exactly.
You don't have to worry that the meaning of an integer is going
to change if you move from a Pentium to a PowerPC.
In Java everything is guaranteed.
However the virtual machine itself and some parts of the class
library must be written in native code.
These are not always as easy or as quick to port as pure Java
programs. This is why for example, there's not yet a version of
Java 1.2 for the Mac.
Core Java
Page:17

Copyrights 2009
BVU Amplify DITM

Java is High Performance


Java byte codes can be compiled on the fly to code that rivals C++ in
speed using a "just-in-time compiler." Several companies are also
working on native-machine-architecture compilers for Java. These will
produce executable code that does not require a separate interpreter,
and that is indistinguishable in speed from C++.
While you'll never get that last ounce of speed out of a Java program
that you might be able to wring from C or Fortran, the results will be
suitable for all but the most demanding applications.
It is certainly possible to write large programs in Java. The HotJava
browser, the Eclipse integrated development environment, the
LimeWire file sharing application, the jEdit text editor, the JBoss
application server, the Tomcat servlet container, the Xerces XML
parser, the Xalan XSLT processor, and the javac compiler are large
programs that are written entirely in Java.
Core Java
Page:18

Copyrights 2009
BVU Amplify DITM

Java is Multi-Threaded
Java is inherently multi-threaded. A single Java program can have many
different threads executing independently and continuously. Three Java
applets on the same page can run together with each getting equal time
from the CPU with very little extra effort on the part of the
programmer.
This makes Java very responsive to user input. It also helps to
contribute to Java's robustness and provides a mechanism whereby the
Java environment can ensure that a malicious applet doesn't steal all of
the host's CPU cycles.
Unfortunately multithreading is so tightly integrated with Java, that it
makes Java rather difficult to port to architectures like Windows 3.1 or
the PowerMac that don't natively support preemptive multi-threading.
There is a cost associated with multi-threading. Multi-threading is to
Java what pointer arithmetic is to C, that is, a source of devilishly hard
to find bugs. Nonetheless, in simple programs it's possible to leave
multi-threading alone and normally be OK.
Core Java
Page:19

Copyrights 2009
BVU Amplify DITM

Java is Dynamic(ly linked)

Java does not have an explicit link phase. Java source code is divided
into .java files, roughly one per each class in your program. The compiler
compiles these into .class files containing byte code. Each .java file
generally produces exactly one .class file.

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. If the file you're compiling
depends on other, non-compiled files the compiler will try to find them
and compile them as well. The compiler is quite smart, and can handle

circular dependencies as well as methods that are used before they're


declared. It also can determine whether a source code file has changed
since the last time it was compiled.
Core Java
Page:20

Copyrights 2009
BVU Amplify DITM

More importantly, classes that were unknown to a program


when it was compiled can still be loaded into it at runtime.

For example, a web browser can load applets of differing


classes that it's never seen before without recompilation.

Furthermore, Java .class files tend to be quite small, a few


kilobytes at most. It is not necessary to link in large runtime
libraries to produce a (non-native) executable. Instead the
necessary classes are loaded from the user's CLASSPATH.
Core Java
Page:21

Copyrights 2009
BVU Amplify DITM

Java is Garbage Collected


You do not need to explicitly allocate or deallocate memory in
Java. Memory is allocated as needed, both on the stack and the
heap, and reclaimed by the garbage collector when it is no longer
needed. There's no malloc(), free(), or destructor methods.
There are constructors and these do allocate memory on the heap,
but this is transparent to the programmer.
The exact algorithm used for garbage collection varies from one
virtual machine to the next. The most common approach in
modern VMs is generational garbage collection for short-lived
objects, followed by mark and sweep for longer lived objects. I
have never encountered a Java VM that used reference counting.

Core Java
Page:22

Copyrights 2009
BVU Amplify DITM

First Java Program


class HelloJava
{
public static void main (String args[])
{
System.out.println("Hello Java!");
}
}

Core Java
Page:23

Copyrights 2009
BVU Amplify DITM

First Java Program


To write the code you need a text editor. You can use any
text editor like Notepad, Brief, emacs or vi. Personally I use
BBEdit on the Mac and TextPad on Windows.
When you've chosen your text editor, type or copy the above
program into a new file. For now type it exactly as it appears
here. Like C and unlike Fortran, Java is case sensitive so
System.out.println is not the same as system.out.println.
CLASS is not the same as class, and so on.
Assuming that Java is properly installed on your system there
are three steps to creating a Java program:
writing the code
compiling the code
running the code
Core Java
Page:24

Copyrights 2009
BVU Amplify DITM

First Java Program


Under Windows, it's similar. You just do this in a DOS
shell.
C:> javac HelloWorld.java
C:> java HelloWorld
Hello World
C:>
Notice that you use the .java extension when
compiling a file, but you do not use the .class
extension when running a file.
For IDEs, consult your product documentation.
Core Java
Page:25

Copyrights 2009
BVU Amplify DITM

First Java Program


Under Windows you set the CLASSPATH environment variable
with a DOS command like

C:\> SET CLASSPATH=C:\JDK\JAVA\CLASSES;c:\java\lib\classes.zip


You can also add this to your autoexec.bat file (Windows ME and
earlier) or set it in the environment variables tab of the System
control panel (Windows NT and later) You should of course point
it at whichever directories actually contain your classes.
The CLASSPATH variable is also important when you run Java
applets, not just when you compile them. It tells the web
browser or applet viewer where it should look to find the
referenced .class files. If the CLASSPATH is set improperly, you'll
probably see messages like "Applet could not start."
Core Java
Page:26

Copyrights 2009
BVU Amplify DITM

If the CLASSPATH environment variable has not been set, and you do
not specify one on the command line, then Java sets the CLASSPATH to

the default:

Unix: .:$JAVA/classes:$JAVA/lib/classes.zip
Windows: .:$JAVA\classes:$JAVA\lib\classes.zip
Mac: ./$JAVA:classes/$JAVA:lib:classes.zip

Here . is the current directory and $JAVA is the main Java directory
where the different tools like javac were installed.
Core Java
Page:27

Copyrights 2009
BVU Amplify DITM

You might also like