Transcript 081208

Final Review
James Atlas
August 12, 2008
“Family Feud” style
• Each team gets a chance to pick an answer
• The team with the highest ranked answer
gets to try to guess the rest
• 2 wrong answers and the other team can
then “steal” the category
• Each category winner gets candy!
• Must be able to explain a little about the
answer! And there are bonus questions…
August 12,2008
James Atlas - CISC370
2
First, a list of topics
• http://www.cis.udel.edu/~atlas/cisc370/finalprep.html
• On the exam:















Java basics
Syntax, primitive types, control flow, naming conventions
Basic OOP syntax, object-oriented design
OOP in Java: Inheritance, Interfaces, Polymorphism
Advanced classes: inner classes and anonymous classes
Packages
Exceptions
Java Input/Output
Serialization/Cloning
Java Collections framework
The Swing toolkit
Network programming
Multithreaded programming
Software design patterns
XML
August 12,2008
James Atlas - CISC370
3
August 12,2008
James Atlas - CISC370
4
What is Java?
int
50 Object-oriented
float
30 Cross-platform
long
15 Large Library of Tools
Programming Language (Sun)
boolean
5
August 12,2008
James Atlas - CISC370
5
Name some components of the Java
Virtual Machine
int
35 Bytecode Interpreter
float
30 Garbage Collector
long
25 Dynamic Compiler
boolean
10 Versions (native code for each platform)
August 12,2008
James Atlas - CISC370
6
Name a Java primitive type
int
20 int
float
17 float
long
15 long
boolean
15 boolean
double
10 double
byte
10 byte
short
7
short
char
6
char
August 12,2008
James Atlas - CISC370
7
Name a method/field access modifier
int
50 public
float
25 private
long
20 protected
boolean
5
package-private
August 12,2008
James Atlas - CISC370
8
Name a different keyworded
method/field modifier
int
35 static
float
30 final
long
20 synchronized
boolean
10 volatile
double
5
transient
August 12,2008
James Atlas - CISC370
9
How do you control a loop in Java?
int
50 for
float
25 while
long
10 do-while
boolean
10 break
double
5
continue
August 12,2008
James Atlas - CISC370
10
What Java constructs provide the
basis for Object-oriented principles?
int
50 Classes
float
25 Interfaces
long
20 Abstract Classes
boolean
5
java.lang.Object/Instances
August 12,2008
James Atlas - CISC370
11
BONUS: Every variable is passed-byvalue in Java, how does this work?
August 12,2008
James Atlas - CISC370
12
What components/properties make
up a class definition?
int
25 Fields/variables
float
20 Constructors
long
15 Methods
boolean
15 Package
double
15 Imports
byte
10 Object hierarchy
August 12,2008
James Atlas - CISC370
13
BONUS: What is overloading and how
is it handled in Java?
August 12,2008
James Atlas - CISC370
14
Name some common Interfaces
related to the Java Collections
Framework
int
35 List
float
25 Map
long
20 Set
boolean
10 Collection
double
5
Comparable
byte
5
Queue
August 12,2008
James Atlas - CISC370
15
What keywords are used in Java
Exception handling?
int
40 catch
float
30 try
long
20 throws
boolean
10 finally
August 12,2008
James Atlas - CISC370
16
Name a Java I/O class or interface
int
30 InputStream
float
28 OutputStream
long
17 FileInputStream/Out
boolean
15 SocketInputStream/Out
double
6
DataInputStream/Out
byte
4
ObjectInputStream/Out
August 12,2008
James Atlas - CISC370
17
BONUS: What design pattern does
Java I/O follow?
August 12,2008
James Atlas - CISC370
18
Name a way/reason to use an innerclass
int
30 Hidden from other classes
float
26 Convenient
long
24 Implement interfaces on-the-fly
boolean
20 Access the surrounding implementation
August 12,2008
James Atlas - CISC370
19
Name a general software pattern
(only ones we talked about in class)
int
35 Factory
float
30 Decorator
long
25 MVC
boolean
6
Command
double
4
Observer/Event
August 12,2008
James Atlas - CISC370
20
Name a component of the Java Swing
framework
int
30 Frame
float
25 Container
long
15 Layout
boolean
10 Button
double
10 Panel
byte
6
Menu
short
4
Event
August 12,2008
James Atlas - CISC370
21
Name a component of the Java
Network Programming model
int
60 Socket
float
25 ServerSocket
long
15 Stream
boolean
5
InetAddress
double
5
DatagramPacket
August 12,2008
James Atlas - CISC370
22
Name an activity that a Java program
can perform on XML
int
35 parse/read
float
30 generate/write
long
25 transform
boolean
10 validate
August 12,2008
James Atlas - CISC370
23
BONUS: Explain the difference
between the SAX and DOM
programming models
August 12,2008
James Atlas - CISC370
24
The End!
August 12,2008
James Atlas - CISC370
25
Object Destructors
• In C++ (and many other OOP languages),
classes have explicit destructor methods that
run when an object is no longer used.
• Java does not support destructors, as it
provides automatic garbage collection
 Watches/waits until there are no references to
an object
 Reclaims the memory allocated for the object
that is no longer used
August 12,2008
James Atlas - CISC370
26
finalize()
• Java supports a method named finalize().
• method is called before the garbage collector
sweeps away the object and reclaims the
memory
• This method should not be used for
reclaiming any resources
 the timing when this method is called is not
deterministic or consistent
 only know it will run sometime before garbage
collection
August 12,2008
James Atlas - CISC370
27
Summary of Inheritance
• Place common operations & fields in the
superclass.
 Remove repetitive code by modeling the “is-a”
hierarchy
 Move “common denominator” code up the
inheritance chain
• Protected fields are generally not a good
idea.
• Don’t use inheritance unless all inherited
methods make sense
• Use polymorphism.
August 12,2008
James Atlas - CISC370
28
Abstract Classes
• Some methods defined, others not defined
• Classes in which not all methods are
implemented are abstract classes.
 public abstract class Animal
• Blank methods are labeled with the abstract
keyword also
 public abstract void feed();
August 12,2008
James Atlas - CISC370
29
Abstract Classes
• An abstract class cannot be instantiated
• Subclass of an abstract class can only be
instantiated if it overrides each of the
abstract methods of its superclass and
provides implementation
 If subclass does not override all abstract
methods, it is also abstract
• Use abstract classes when you have a partial
implementation
August 12,2008
James Atlas - CISC370
30
Methods: An Alternate Ending
• If a method throws an exception
 it does not return anything
 execution does not resume immediately
following the method call (as it would if the
method returns a normal value)
• JVM’s exception-handling mechanism
searches for an exception handler
 Exception handler: error recovery code
• runs to deal with a particular error condition.
August 12,2008
James Atlas - CISC370
31
Streams
• Java handles input/output using streams
input stream: an object from which we can
read a sequence of bytes
Abstract class: InputStream
August 12,2008
James Atlas - CISC370
32
Streams
• Java handles input/output using streams
output stream: an object to which we can write
a sequence of bytes
Abstract class: OutputStream
August 12,2008
James Atlas - CISC370
33
Object Serialization
• Serialization: process of converting an object
to ordered data, to operate with streams
• To allow a class of objects to be written and read
with Object[Output/Input]Stream
 the class must implement the Serializable interface
• Serializable interface contains no methods
 “Marker interface”
 used to tag a class as able to be serialized
 refers to the class’s ability to be converted into a single
byte stream, which is then saved
• All classes are inherently serializable
 But you have to mark them as Serializable
August 12,2008
James Atlas - CISC370
34
Object Serialization
• When an object is written to a stream, it is written
•
as a sequence of bytes.
Stores, in order
 fingerprinting information that describes the class
 instance fields and their values
 a more complete class description.
• The ability to convert an object from its “object”
state to a byte stream state is what makes the
direct writing and reading of objects possible
August 12,2008
James Atlas - CISC370
35
Java-memory model (JMM)
August 12,2008
James Atlas - CISC370
36
Java-memory model Rules
• Atomicity
 access/update to anything except long/double
• Visibility
 changes not always guaranteed visible
•
• synchronization does
• volatile variables
• Thread.join()
Ordering
 instructions in-thread are as-if-serial
 out-of-thread no guarantee
August 12,2008
James Atlas - CISC370
37
Thread Synchronization
• In many multithreaded applications, more
than one thread will need to access the
same data
Can turn into a problem when two threads have
access to the same object and then both modify
the state of the object
Depending on the order of data access, the
object can become corrupted
Known as a race condition
August 12,2008
James Atlas - CISC370
38
Synchronized Methods
• A method or block of code can be marked
as atomic
Using keyword synchronized in method’s
return type
After the section of code starts, the code is
guaranteed to complete before another thread
can enter another synchronized section
belonging to that same object
August 12,2008
James Atlas - CISC370
39
Synchronization Diagram
August 12,2008
James Atlas - CISC370
40
Wait/Notify Mechanism Diagram
August 12,2008
James Atlas - CISC370
41