Download Programming in Java - UCL Computer Science

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Resource management (computing) wikipedia , lookup

Library (computing) wikipedia , lookup

Smalltalk wikipedia , lookup

Pwn2Own wikipedia , lookup

Design Patterns wikipedia , lookup

Go (programming language) wikipedia , lookup

C syntax wikipedia , lookup

Object-oriented programming wikipedia , lookup

Class (computer programming) wikipedia , lookup

Java syntax wikipedia , lookup

Name mangling wikipedia , lookup

Scala (programming language) wikipedia , lookup

C++ wikipedia , lookup

C Sharp syntax wikipedia , lookup

Java (programming language) wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Java performance wikipedia , lookup

Transcript
C340 Concurrency:
Programming in Java
Wolfgang Emmerich
Mark Levene
© Wolfgang Emmerich, 1997
1
What is Java?
n
n OO Programming Language
n
n C/C++ control statements & expressions
n
n Automatic garbage collection
n
n Single inheritance + interfaces
n
n Concurrent Threads + Monitors
n
n Standard Packages
••
••
••
••
Graphical
Graphical User
User Interface
Interface support.
support.
Network
Network support.
support.
Multi-media
Multi-media support
support -- animation,
animation, sound
sound
ADTs
ADTs -- Vector,
Vector, Hashtable,
Hashtable, Dictionary
Dictionary etc
etc
© Wolfgang Emmerich, 1997
2
Why is Java interesting?
n
n Portability
•• Java
Java programs
programs are
are compiled
compiled into
into bytecode
bytecode
and
and are
are executed
executed by
by interpreter
interpreter
•• Efficiency
Efficiency through
through JIT
JIT bytecode
bytecode compilers
compilers
•• Java
Java Virtual
Virtual Machine
Machine implementations
implementations availavailable
able for
for all
all common
common platforms
platforms (CPU
(CPU ++ OS)
OS)
•• Package
Package java.awt:
java.awt: portable
portable interface
interface to
to
window
window systems
systems e.g
e.g X11,
X11, Windows,
Windows, Mac
Mac OS.
OS.
n
n Accessibility
•• Compiled
Compiled Java
Java programs
programs can
can be
be transmitted
transmitted
to
to and
and executed
executed on
on remote
remote computers.
computers.
•• Web
Web browsers
browsers execute
execute Java
Java code
code -- Applets.
Applets.
© Wolfgang Emmerich, 1997
3
What is an Applet?
Java
Java class
class derived
derived from
from application
application window
window
toolkit
toolkit (AWT)
(AWT) class
class Applet
Applet
n
n Executable
Executable by
by aa Web
Web browser.
browser.
n
n
Browser
HTTP request
applet code
Demo
© Wolfgang Emmerich, 1997
HTTP
server
Java
.class
files
4
A simple Applet - Hello World
import java.awt.*;
//windows toolkit
import java.applet.*;
//applet support
public class HelloWorld extends Applet {
private Font f1;
public HelloWorld() {
f1 = new Font(“Helvetica”,Font.BOLD,36);
}
public void paint(Graphics g) {
g.setFont(f1);
g.drawString(“Hello World”,25,50);
}
}
© Wolfgang Emmerich, 1997
5
Hello World - Class diagram
Applet
init()
start()
stop()
HelloWorld
init()
paint()
© Wolfgang Emmerich, 1997
6
Embedding Applets in HTML
<HTML>
<HEAD>
<TITLE> A simple program </TITLE></HEAD>
<BODY>
<H1 ALIGN=CENTER>Hello World</H1>
<CENTER><P>
<APPLET CODE=”Hello World.class"
WIDTH=250
HEIGHT=150>
</APPLET>
</P></CENTER>
</BODY>
</HTML>
© Wolfgang Emmerich, 1997
7
How does Java differ from C/C++?
n
n Primitive Datatypes
•• boolean
boolean,,char
char,,byte
byte,,short
short,,int
int,,long
long,,float
float,,
double
double..
•• boolean
boolean is
is aa 11 bit
bit value
value ((true
true,, false
false))
•• char
char is
is aa 16
16 bit
bit Unicode
Unicode character
character
•• constants
constants are
are declared
declared like:
like:
public
public final
final static
static PI
PI == 3.14159;
3.14159;
n
n Derived Types
classes
classes &
& arrays
arrays
Instances
Instances of
of derived
derived types
types handled
handled by
by
reference
reference
•• primitive
primitive types
types handled
handled by
by value.
value.
••
••
© Wolfgang Emmerich, 1997
8
Java Classes
n
n Single inheritance hierarchy
n
n Rooted in class Object.
class Counter extends Object{
private int count;
Counter(int i)
{count = i;}
void increment()
{ ++count; }
void decrement()
{ --count; }
int value()
{ return count;}
}
© Wolfgang Emmerich, 1997
9
Java Interfaces
Do not contain any method code
n Similar to C++ classes with pure virtual
member functions (int a()=0;)
n Implemented by other classes that declare
implements relationship
n Used to implement callbacks
n
© Wolfgang Emmerich, 1997
10
Objects & Object References
n
n Declaring objects:
Counter a;
Counter b;
n
n Creating objects:
a=new Counter(0);
b=new Counter(1);
n Method invocation:
a.increment();
b.decrement();
© Wolfgang Emmerich, 1997
a:Counter
count = 0
b:Counter
count = 1
11
Objects & Object References (cont’d)
n
n Identity vs. equality:
(a = = b)
// identity test
(a.equals(b)) // equality test
n
n Automatic garbage collection:
a = b;
//
a = null; //
//
//
© Wolfgang Emmerich, 1997
if a is last ref
garbage collection
deletes object after
these assignments
12
Arrays
n
n Arrays are created similarly to objects:
int table[]= new table[128];
int lookup[] = {1,2,4,8,16,32};
n
n Arrays have a length field:
for(int
for(int ii =0;
=0; i<table.length;
i<table.length; i++)
i++)
table[i]
table[i] *=
*= 2;
2; //
// double
double
© Wolfgang Emmerich, 1997
13
Strings
n
n Strings are not null terminated arrays of
characters:
String a = “Hello”;
String b = “World”;
String message = a + “, ” + b;
n
n A String is immutable
n
n Use StringBuffer to modify the
contents of a String
© Wolfgang Emmerich, 1997
14
Further Information
n
n Java in a Nutshell by David Flanagan
O’Reilly & Associates Inc. 1996
n
n The Java Tutorial by Mary Campione and
Kathy Walrath Addison-Wesley, 1996.
http://www.javasoft.com/
n
n Java API Documentation
http://www.javasoft.com/
n
n Concurrency Course using Java
http://www-dse.doc.ic.ac.uk/~jnm
© Wolfgang Emmerich, 1997
15
Summary
n
n Advantages of Java
n
n Applets vs. Applications
n
n Differences between Java and C++
n
n Classes and Inheritance in Java
n
n Interfaces in Java
n
n Objects and Object References
n
n Arrays
n
n Strings
n
n Next Lecture: Concurrency in Java
© Wolfgang Emmerich, 1997
16