Title: Introduction to JAVA
1Introduction to JAVA
- Vijayan Sugumaran
- School of Business Administration
- Oakland University
- Rochester, MI 48309
2Introduction to JAVA
- Ancestor to JAVA - Oak
- small, robust, architecture independent,
Object-Oriented, language to control interactive
TV. - didnt go anywhere
- refocused for Internet, Java was born.
- JAVA is both compiled and interpreted language.
UNIX
Compiled JAVA byte-code
JAVA Source Code
PC
MAC
3Java Introduction (contd.)
- JAVA byte-code (or J-code) is executed by JAVA
run-time interpreter (JAVA compliant Virtual
Machine) - The interpreter can be run as a separate
application or embedded in another software such
as a browser. - The fundamental unit of JAVA code is the class.
- Since JAVA interpreter runs compiled byte-code,
JAVA is relatively a fast interpreted language. - The run-time system can compile byte-code to
native machine code on the fly to optimize
performance. This is called Just in time. (one
sticky problem is the array-bounds checking)
4Java and Javascript
- Dont confuse JAVA with Javascript
- Javascript is an object-based scripting language
developed by Netscape - Lets you create active user interface and capture
and validate user inputs - Client-side replacement for cgi-script, client
side computing instead of server side - Java is full-featured programming language
- Javascript can interact with Java applets
5Java Characteristics
- Unlike C, Java doesnt allow programmer defined
operator overloading - Java doesnt have a preprocessor, so it doesnt
have macros, define statements, or conditional
source compilation - Java provides a well-defined package structure
for organizing class files - Supports only single inheritance class hierarchy,
but allows multiple inheritance of Interfaces - -Interface of a class--like an abstract class in
C which specifies the behavior of an object
without defining its implementation
6Java Characteristics (contd.)
- Statistically typed
- data types etched in stone for the compiler (C,
C) - Dynamically typed
- type checking performed during execution (lisp or
smalltalk) - Early binding
- binding method calls to definitions at compile
time (C, C) - Late binding
- locating definitions of methods dynamically at
run time - Java is statistically typed and late-binding
language - Java carries all data-type and method-signature
information with it from its source code to its
compiled byte-code form
7Java Characteristics (contd.)
- Base class in Java can evolve with out affecting
derived classes as long as it maintains a valid
form of its original structure. (in C, if the
base class changes, the derived classes should be
recompiled) - Java handles garbage collection
- Java uses references instead of pointers.
Cant do pointer arithmetic with references. A
references is a strongly typed handle for an
object. All objects are accessed through
references - Security manager controls access to system
resources like the file system, network parts,
windowing environment, etc.
8JAVA Security
System Resources
Security Manager
Class Loader
Verifier
Untrusted Source
JAVA Binary
9Safety Implementation
- Verifier guarantees the integrity of incoming
classes - Verifier
- reads byte-code makes sure it behaves properly
- it is a type of theorem prover - steps through
the byte-code and determines the behavior - Class Loader handles loading classes from the
network and protects basic system classes - Class Loaders and security managers are
implemented by applications that load applets
such as browser and applet viewer
10More about Class Loader
- Class Loader
- Java adds second layer of security with class
loader - It brings in Java binary classes that contain
byte-code into the interpreter - Classes loaded remain associated with class
loader - If a class references another class, the request
is served by its original class loader - Classes loaded from one location can be
restricted to interact with only those classed
loaded from that same location
11Security Manager
- Security Manager
- Application level security decisions
- Security manager is consulted every time the
application tries to access system resources - The security manager can be simple or complex as
the application warrants - The integrity of the security manager is based on
the protection afforded by the lower levels of
the Java security model
12Java Applets
- Applets
- Small embeddable application
- A Java applet is a compiled Java program,
consisting of classes just like any other Java
program - Applets are autonomous programs confined within
the walls of the browser or applet viewer. Can
interact with user and communicate with the host
over the network
13Error Handling
- The protection of references is one of the most
fundamental aspects of Java Security - Java code has to play by the rules--it cant peek
into places it shouldnt - Error Handling
- Exceptions allow separation of error-handling
code from normal code - Exception carries with it an object that contains
information about the situation that caused its
exception
14Multi Threading
- Thread
- flow of control within a program
- threads run in the same address space
- share instance variables, but not local variables
- Multi Threading
- Threads provide efficient multi processing and
distribution of tasks - Threads need to be synchronized--only one
synchronized method within the object may run at
a given time
15Java Packages
- Scalability
- Package--structure that groups classes into
functional units - Within a package, a class is publicly visible or
protected from outside access - Package promote reuse scalability
- Package names constructed in a hierarchical way
using dot-separated naming convention - e.g. Java.awt.event
16Sample Java Program
//Create a file called Welcome.java public class
Welcome public static void main(String
args) System.out.println(Hello
World, Welcome to Java!!) Compiling
and Executing (using JDK) compiling source code
gt javac Welcome.java (creates
Welcome.class - java bytecode) executing the
program gt java Welcome (JVM
executes the byte code)
17Sample Applet
//create a file called HelloWeb.java public class
HelloWeb extends java.applet.Applet public
void paint( java.awt.Graphics gc)
gc.drawString (Hello World, Welcome to Java,
125, 95) ? Compile the java source file
(using JDK) javac HelloWeb.java ? Create an
html file that embeds the applet using the
ltappletgt tag ? Load the html page into a java
supported browser
18Sample HTML Document
lthtmlgt ltheadgt lt/headgt ltbodygt ltapplet
codeHelloWeb width300 height200
lt/appletgt lt/bodygt lt/htmlgt ? Save it as
HelloWeb.html ? Open HelloWeb.html using the
browser