Introduction to JAVA

cavalcadejewelSoftware and s/w Development

Nov 18, 2013 (3 years and 8 months ago)

78 views

Introduction to JAVA

Vijayan Sugumaran

School of Business Administration

Oakland University

Rochester, MI 48309

Introduction to JAVA


Ancestor to JAVA
-

Oak


small, robust, architecture independent, Object
-
Oriented, language to control interactive TV.


didn’t go anywhere


refocused for Internet, Java was born.


JAVA is both compiled and interpreted
language.

JAVA

Source

Code

Compiled

JAVA

byte
-
code

UNIX

PC

MAC

Java 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)

Java and Javascript


Don’t 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



Java Characteristics


Unlike C++, Java doesn’t allow programmer
defined operator overloading


Java doesn’t have a preprocessor, so it doesn’t
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

Java 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

Java 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. Can’t
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.

JAVA Security


System

Resources


Security Manager


Class Loader


Verifier


Untrusted

Source


JAVA Binary

Safety 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

More 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

Security 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

Java 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


Error Handling


The protection of references is one of the most
fundamental aspects of Java Security


Java code has to play by the rules
--
it can’t peek
into places it shouldn’t


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

Multi 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

Java 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

Sample 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:


> javac Welcome.java (creates Welcome.class
-

java bytecode)

executing the program:


> java Welcome (JVM executes the byte code)

Sample 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 <applet> tag




Load the html page into a java supported browser

Sample HTML Document

<html>

<head>

</head>

<body>


<applet code=HelloWeb width=300 height=200 </applet>

</body>

</html>





Save it as HelloWeb.html




Open HelloWeb.html using the browser