You are on page 1of 14

,QWURGXFWLRQWR-DYD3URJUDPPLQJ

((

Part 1: Introduction
Dr Kun Yang
Email:
Email: kunyang@essex.ac.uk
Web: http://privatewww.essex.ac.uk/~kunyang
http://privatewww.essex.ac.uk/~kunyang
Office:
Office: 1NW.4.16

EE905-intro

6\OODEXV
Aims: to provide a first step assistance to students towards excellent Java
programming with emphasis on most fundamental concepts and
mechanisms provided by Java language. GUI design and more advanced
threading mechanism are also covered.
Learning Outcomes: Understand the basic concepts of OOP (Objectoriented Programming); Write, compile and run simple Java program
using command line based Sun JDK; Know the difference of classes and
objects; Extend classes; Understand the difference between class and
interface; Use basic control and data structure; Use exceptions to handle
potential error; Program multi-thread Java programs; documentation
comments; Understand the difference between Java application and Java
applets; Use event model within GUI applications; Debug Java programs;
Know basic principles to develop more complex Java applications
Method: Lecture presentations in association with Software Lab
(EE982/5).
EE905-intro-2

*HQHUDO,QIRUPDWLRQ
Consisting of nine 2-hour lectures
Weeks 2-7, Weeks 9-11
Friday 3pm-5pm

The material listed here is just outline of the lecture. Please


refer to the reference books for language details.
Contents in connection with EE982/5 (MSc Software Lab)

EE905-intro-3

0DLQ5HIHUHQFH0DWHULDOV
Ken Arnold, James Cosling, David Holmes. The Java Programming
Language (Third Edition), Addison Wesley Press. (esp. for part 2)
Cay Horstmann, Gary Cornell. Core Java 2 Fundamentals, Prentice
Hall. (this is where most example programs are from)
Deitel & Deitel. Java: How to Program (5th Edition), Prentice Hall.
Suns Java Tutorials: excellent for self-paced learning

Online: http://java.sun.com/docs/books/tutorial/
Book: The Java Tutorial: A Short Course on the Basics (3rd Edition,
AW Java Series) by Mary Campione, Kathy Walrath, Addison-Wesley
(ISBN: 0201703939)

EE905-intro-4

6WUXFWXUHRIWKH/HFWXUH

Introduction (2 hours)

Basic of Java Language (6 hours)

GUI Design, Event Handling and Java Applets


(6 hours)
Thread (2 hours)

A Briefing on Advanced Java Application


Development (2 hours)

EE905-intro-5

3DUW5RDGPDS
Knowing Java Program a Bit
Object-Oriented Programming (OOP) & Features of Java
Language
Java Development & Execution Environment
Java in Action! (live demo)
Linux, its file system and some basic commands (for
reference)

EE905-intro-6

*HWWLQJ6WDUWHG
In the Java Programming language, programs are built from classes.

From a class definition, you can create any number of objects that are
known as instances of that class.
A class contains members, the primary kinds being fields and methods.
Fields are data variables belonging either to the class itself or to
objects of the class; they make up the state of the object or class.

Methods are collections of statements that operate on the fields to


manipulate the state. Statements define the behaviour of the classes;
they can assign values to fields and other variables, evaluate arithmetic
expressions, invoke methods and control the flow of execution.

EE905-intro-7

+HOOR:RUOG3URJUDP
A simple Java program: Hello World
/* HelloWorld.java */
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}

Typing in the Java Program

Running the Java compiler: javac HelloWorld.java


Running Java on the class file: java HelloWorld

Note: each Java class (or interface) must be in a file on its own, with
the file name identical to the class name (plus .java )
EE905-intro-8

+HOOR:RUOG3URJUDP$QDO\VLV
Weve had a small program that prints Hello, World!, but what does it
mean?

The program declares a class called HelloWorld with a single member:


a method called main. Class members appear between curly braces { and
} following the class name.
The main method is a special method: it is executed when you run the
class as an application. When run, a main method can create objects,
evaluate expressions, invoke other methods, and do anything else needed
to define an applications behaviour.
public: anyone can invoke it (in this case the Java virtual machine)
static: meaning the method belongs to the class rather than a
particular instance of the class
Return type: void (no return)

EE905-intro-9

+HOOR:RUOG3URJUDP$QDO\VLV FRQW
Following the method name is the parameter list for the method a
sequence of zero or more pairs of types and names.
The name of a method together with its parameter list constitute the
signature of the method.

The signature together with any modifiers (such as public and static)
and exception throws list (covered later in this lecture) forms the method
header.
A method declaration consists of the method header followed by the
method body a block of statements appearing between curly braces.
In this example, main contains a single statement that invokes the
println method.

EE905-intro-10

OOP (Object Oriented Programming) &


Features of Java Language

EE905-intro-11

,QWURGXFWLRQWR2EMHFW2ULHQWDWLRQ 22  
Object orientation is a natural way of thinking about the world and a
natural way of writing computer programs.

Everywhere you look into the real world you see objects: people,
animals, plants, cars, planes, buildings, computers and so on. Humans
think in terms of objects and use abstraction.
All these objects have something in common: they all have attributes
like size, shape, colour and weight, and they all exhibit behaviours.
Object-oriented Design (OOD) models software in terms similar to
those used by humans to describe real-world objects.

OOD takes advantage of class relationships, where objects of a certain


class (e.g., a class of vehicles) have the same characteristics. It takes
advantage of inheritance relationships where new classes of objects are
derived by absorbing characteristics of existing classes and adding
unique characteristics of their own.
EE905-intro-12

,QWURGXFWLRQWR2EMHFW2ULHQWDWLRQ 22  
OOD encapsulates attributes and operations (behaviours) into objects;
the attributes and operations of an object are intimately tied together.
Objects have the property of information hiding.

Languages such as Java are object oriented (OO) and programming in


such a language is called OOP. Whereas languages such as C are
procedural where programming tends to be action-oriented (in C, the
unit of programming is function.)
We can instantiate many objects from one class.

Classes can also have relationships with other classes. These


relationships are called associations.
When software is packaged as classes, these classes can be reused in
future software systems. Reuse is critical to OOP
EE905-intro-13

2230DLQ7HUPLQRORJLHV
Abstraction : "An abstraction denotes the essential characteristics of an
object that distinguish it from all other kinds of objects and thus
provide crisply defined conceptual boundaries, relative to the
perspective of the viewer."
Encapsulation : "Encapsulation is the process of compartmentalizing
the elements of an abstraction that constitute its structure and
behaviour; encapsulation serves to separate the contractual interface of
an abstraction and its implementation."
Modularity : "Modularity is the property of a system that has been
decomposed into a set of cohesive and loosely coupled modules."
Hierarchy : classes ( is-a ) and object ( has-a ) hierarchies
"Hierarchy is a ranking or ordering of abstractions."

EE905-intro-14

-DYD)HDWXUHV6LPSOH5REXVW 6HFXUH
Simple

Many of the language complexities removed (e.g. no pointers!)


Java libraries can be complex though!

Robust

Compile-time checking
Run-time checking: class loader, verification, security manager
Simplification of the language cf. C++: e.g. no pointers, no need to locate
dynamic memory

Secure

Designed for use in distributed environments


Untrusted Java applets can be downloaded on the Web and run in a secure
environment ("sandbox")
Most secure programming language to date
Java team has "zero tolerance" for security bugs
But see e.g. Java Security FAQ at

http://java.sun.com/sfaq/index.html

EE905-intro-15

-DYD$UFKLWHFWXUH1HXWUDODQG3RUWDEOH
Architecture Neutral

Java source code is compiled into bytecode


Bytecode can be interpreted on any platform with the Java runtime system
(Java virtual machine)
Using JIT (just-in-time) compiler, bytecode can be converted on the fly
to native platform machine code for faster execution

Portable

Unlike C/C++ there are no implementation-dependent language features


For example, integers (int) are all 32 bits, irrespective of machine word
size
Characters & strings use Unicode
Graphics & user interface components platform independent
(less so with AWT, more so with Swing)

EE905-intro-16

-DYD,QWHUSUHWHG0XOWLWKUHDGHG '\QDPLF
Interpreted

Development benefits from rapid compile/link times in principle


In practice, JDK not that fast!
Commercial Java IDEs better

Multithreaded

Java provides language-level support for multithreading


Language provides sophisticated monitor and condition-locking primitives
Java libraries are thread-safe

Dynamic

Classes are linked in only as needed


They can be obtained when required from disk or even across the network
Reflection API allows full insight into structure and behaviour of objects
Many uses e.g. Java GUI builders, smart debuggers, pluggable components,
object databases, etc.
EE905-intro-17

3HUIRUPDQFH,VVXHVRI-DYD
Interpreted bytecode is necessarily slower than compiled C++

Java can access native compiled code if required (but then no longer
portable)
JIT (just-in-time) compilers can provide 10- to 20-times speed-up,
approaching that of compiled code
Native Java compilers available
Java Whitepaper:
http://java.sun.com/doc/language_environment

EE905-intro-18

Java Development & Execution


Environment

EE905-intro-19

-DYD3URJUDP'HYHORSPHQWDQG([HFXWLRQ3KDVHV
Phase 1

Editor

Disk

Program is created in an editor and stored


on disk in a file ending with .java

Phase 2

Compiler

Disk

Compiler creates bytecodes and stores them


on disk in a file ending with .class

Phase 3

Class
Loader
Disk

memory Class loader reads .class files


containing bytecodes from disk and puts
them into memory

Phase 4

Bytecode
Verifier

memory

Phase 5

Interpreter

memory

Bytecode verifier confirms that all


bytecodes are valid and do not vilate
Javas security restrictions.
Interpreter reads bytecodes and
translates them into a language
understandable by computer.

EE905-intro-20

10

6XQ-6'.
Java 2 SDK, Standard Edition, version 1.4
Networking Support, including IPv6
XML Processing
Java Bean
.
Enterprise Features

RMI-IIOP
Java IDL CORBA
JDBC (Java Database Connectivity)
Java Naming and Directory Interface (JNDI)
EE905-intro-21

-DYD9LUWXDO0DFKLQH -90
Java is designed to maximize portability.

Java source code is compiled into Java bytecodes, which are designed
to be run on a Java virtual machine (JVM).
JVM can be implemented in silicon, i.e., on a special-purpose chip
without affecting the portability of the bytecodes.
JVM provides a runtime system that

Provides access to the virtual machine itself and


To the outside world (such as output stream System.out).

JVM checks security-sensitive operations.

When loaded into a virtual machine classes are checked by a verifier to


ensure that bytecodes are properly formed and meet the security
requirement.
EE905-intro-22

)LUVWLPSUHVVLRQ-DYDLQ$FWLRQ
Some demos from Sun J2SD 1.4.0
Race3d: a 3d racing game for Java
http://www.brackeen.com/home/race3d/

Monkey Puzzle : a puzzle/skill game


http://www.gamesdomain.co.uk/GamesArena/monkey/index.html

Java-enabled chips for pervasive computing!

EE905-intro-23

+RZWR/HDUQ-DYD
Follow this tutorial

Buy a book (or two)

Read a book (or two)

Read Java code - theres plenty of example code available (book CDs,
download, even the Java library source)
Use the Java API documentation online:
http://java.sun.com/products/jdk/1.4/docs/api/index.html
Loads of practice!

EE905-intro-24

12

Linux Essentials

EE905-intro-25

%DVLF2SHUDWLRQV
Logging in/Logging out
Desktop environment
Terminal windows
Browser windows
Start menu
Toolbar

Multiple desktops

Getting Help (man)

Changing password ( Dont use passwd )

Use the Computing Service web page instead


EE905-intro-26

13

8VLQJWKH)LOH6\VWHP
List the contents of a directory ( ls )
Change working directory ( cd )

Display the pathname of the current working directory ( pwd )


Make a directory ( mkdir )
Copy files ( cp )

Move or rename files ( mv )


Remove files ( rm )

Remove a directory ( rmdir )

Change access permissions ( chmod )

EE905-intro-27

5HDGLQJ(GLWLQJ)LOHV
Output complete files to terminal ( cat )
Browse through a text file ( more )
Or use a text editor ( emacs )

Control ( C-x ) and meta ( M-x ) keys


emacs tutorial ( C-h t )
Running tcsh under emacs ( M-x shell )
Compiling using emacs ( M-x compile )
Debugging using emacs ( M-x jdb )

End of Part 1
EE905-intro-28

14

You might also like