Final Review James Atlas August 12, 2008
James Atlas - CISC3702 “Family Feud” style Each team gets a chance to pick an answer Each team gets a chance to pick an answer The team with the highest ranked answer gets to try to guess the rest 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 2 wrong answers and the other team can then “steal” the category Each category winner gets candy! Each category winner gets candy! Must be able to explain a little about the answer! And there are bonus questions… Must be able to explain a little about the answer! And there are bonus questions…
August 12,2008James Atlas - CISC3703 First, a list of topics On the exam: 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,2008James Atlas - CISC3704
August 12,2008James Atlas - CISC3705 What is Java? Programming Language (Sun) 5 Large Library of Tools Cross-platform Object-oriented boolean long float int
August 12,2008James Atlas - CISC3706 Name some components of the Java Virtual Machine Versions (native code for each platform) Dynamic Compiler Garbage Collector Bytecode Interpreter boolean long float int
August 12,2008James Atlas - CISC3707 Name a Java primitive type char 6 short 7 byte double boolean long float int char short byte double boolean long float int
August 12,2008James Atlas - CISC3708 Name a method/field access modifier package-private 5 protected private public boolean long float int
August 12,2008James Atlas - CISC3709 Name a different keyworded method/field modifier transient 5 volatile synchronized final static double boolean long float int
August 12,2008James Atlas - CISC37010 How do you control a loop in Java? continue 5 break do-while while for double boolean long float int
August 12,2008James Atlas - CISC37011 What Java constructs provide the basis for Object-oriented principles? java.lang.Object/Instances 5 Abstract Classes Interfaces Classes boolean long float int
August 12,2008James Atlas - CISC37012 BONUS: Every variable is passed-by- value in Java, how does this work?
August 12,2008James Atlas - CISC37013 What components/properties make up a class definition? Object hierarchy Imports Package Methods Constructors Fields/variables byte double boolean long float int
August 12,2008James Atlas - CISC37014 BONUS: What is overloading and how is it handled in Java?
August 12,2008James Atlas - CISC37015 Name some common Interfaces related to the Java Collections Framework Queue 5 Comparable 5 Collection Set Map List byte double boolean long float int
August 12,2008James Atlas - CISC37016 What keywords are used in Java Exception handling? finally throws try catch boolean long float int
August 12,2008James Atlas - CISC37017 Name a Java I/O class or interface ObjectInputStream/Out 4 DataInputStream/Out 6 SocketInputStream/Out FileInputStream/Out OutputStream InputStream byte double boolean long float int
August 12,2008James Atlas - CISC37018 BONUS: What design pattern does Java I/O follow?
August 12,2008James Atlas - CISC37019 Name a way/reason to use an inner- class Access the surrounding implementation Implement interfaces on-the-fly Convenient Hidden from other classes boolean long float int
August 12,2008James Atlas - CISC37020 Name a general software pattern (only ones we talked about in class) Observer/Event 4 Command 6 MVC Decorator Factory double boolean long float int
August 12,2008James Atlas - CISC37021 Name a component of the Java Swing framework Event 4 Menu 6 Panel Button Layout Container Frame short byte double boolean long float int
August 12,2008James Atlas - CISC37022 Name a component of the Java Network Programming model DatagramPacket 5 InetAddress 5 Stream ServerSocket Socket double boolean long float int
August 12,2008James Atlas - CISC37023 Name an activity that a Java program can perform on XML validate transform generate/write parse/read boolean long float int
August 12,2008James Atlas - CISC37024 BONUS: Explain the difference between the SAX and DOM programming models
August 12,2008James Atlas - CISC37025 The End!
August 12,2008James Atlas - CISC37026 Object Destructors In C++ (and many other OOP languages), classes have explicit destructor methods that run when an object is no longer used. 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 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,2008James Atlas - CISC37027 finalize() Java supports a method named finalize(). Java supports a method named finalize(). method is called before the garbage collector sweeps away the object and reclaims the memory 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 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,2008James Atlas - CISC37028 Summary of Inheritance Place common operations & fields in the superclass. 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. Protected fields are generally not a good idea. Don’t use inheritance unless all inherited methods make sense Don’t use inheritance unless all inherited methods make sense Use polymorphism. Use polymorphism.
August 12,2008James Atlas - CISC37029 Abstract Classes Some methods defined, others not defined Some methods defined, others not defined Classes in which not all methods are implemented are abstract classes. Classes in which not all methods are implemented are abstract classes. public abstract class Animal Blank methods are labeled with the abstract keyword also Blank methods are labeled with the abstract keyword also public abstract void feed();
August 12,2008James Atlas - CISC37030 Abstract Classes An abstract class cannot be instantiated 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 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 Use abstract classes when you have a partial implementation
August 12,2008James Atlas - CISC37031 Methods: An Alternate Ending If a method throws an exception 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 searches for an JVM’s exception-handling mechanism searches for an exception handler Exception handler: error recovery code runs to deal with a particular error condition. runs to deal with a particular error condition.
August 12,2008James Atlas - CISC37032 Streams Java handles input/output using 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,2008James Atlas - CISC37033 Streams Java handles input/output using 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,2008James Atlas - CISC37034 Object Serialization Serialization: process of converting an object to ordered data, to operate with streams 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 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 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 All classes are inherently serializable But you have to mark them as Serializable
August 12,2008James Atlas - CISC37035 Object Serialization When an object is written to a stream, it is written as a sequence of bytes. When an object is written to a stream, it is written as a sequence of bytes. Stores, in order 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 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,2008James Atlas - CISC37036 Java-memory model (JMM)
August 12,2008James Atlas - CISC37037 Java-memory model Rules Atomicity Atomicity access/update to anything except long/double Visibility Visibility changes not always guaranteed visible synchronization does synchronization does volatile variables volatile variables Thread.join() Thread.join() Ordering Ordering instructions in-thread are as-if-serial out-of-thread no guarantee
August 12,2008James Atlas - CISC37038 Thread Synchronization In many multithreaded applications, more than one thread will need to access the same data 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,2008James Atlas - CISC37039 Synchronized Methods A method or block of code can be marked as atomic 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,2008James Atlas - CISC37040 Synchronization Diagram
August 12,2008James Atlas - CISC37041 Wait/Notify Mechanism Diagram