Download Introduction to Java 2 Programming

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

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

Document related concepts
no text concepts found
Introduction to Java 2
Lecture 10
API Review; Where Next
• Reviewing the Java API
– Some packages we’ve seen
– And some new ones…
• Coding and Design Tips
– Tips for good Java programs
• Where Next?
– Books, Websites, Tutorials
• The real core of the Java API
– Automatically imported by all Java classes
• System and Runtime classes
– Access to the virtual machine
– System properties
– Standard input, output, error
• The Number classes
– Boolean, Integer, Float, Double, Long, Short, etc
– Provide OO version of primitive types
– Provide conversion functions (e.g. to/from String)
• The Class and Object classes
– Object is the root of the Java inheritance tree
– Class is an OO description of a Java class
• Large collection utility classes, and several subpackages
• Collection classes, very flexible data structures
– Implementations of Set, List and Map interfaces
– Arrays and Collections classes, for sorting and
searching collections
• Calendar and Date classes for providing date
• Other miscellaneous classes
– Currency, StringTokenizer, etc
• java.util.logging
– Provides access to a complete logging system, for recording
application progress, error messages, etc
– Very flexible (different log levels, different log formats, different
log destinations)
•, java.util.jar
– Classes for working with zip and jar files.
– Built in compression libraries
• java.util.regex
– “Regular Expressions”
– Pattern matching system adopted from Perl
– Very flexible way to process text strings (e.g. to extract substrings,
find data, etc)
• The Java I/O classes
• InputStreams and OutputStreams
– Reading and writing data
• Readers and Writers
– Reading and writing text (according to a character set)
• File
– OO representation of the file system (both files and
• Socket
– Standard Berkeley socket implementation for
connecting to remote service
– ServerSocket class for writing servers
– Describes a remote website, and gives access to its data
via a Stream
• HTTPURLConnection
– Provides more detailed access to a remote website
– E.g. follow redirects, write to remote website, etc
– (but not as flexible as a true HTTP library)
• “Remote Method Invocation”
• A standard way to call methods on objects on a
remote machine
– No need to deal with networking!
• A Registry lists all the useful objects on the remote
– Client gets reference to object from a registry, and then
uses it as normal
• Remote objects have to implement an special
– All methods throw RemoteException
• Very powerful
• Java database access layer
– Known as JDBC (“Java DataBase Connectivity”)
• Allows a Java application to
– ..get a Connection to a database
– …create and execute Statements to query the database
– …and then process the ResultSet that is returned
• All major database vendors have implemented a
Java “JDBC Driver”
– The core API describes the basic interfaces
– The vendor implements that for their database system
• “Abstract Windowing Toolkit”
– The original way to build GUI applications in Java
• Limited capabilities
– Few components
– API poorly designed (particularly event handling)
– Not entirely portable (not pure Java)
• Still used for some applications, but has been
• Java Foundation Classes (“Swing”)
– Replacement for AWT (but does share some common
• Much more flexible
100% Java (therefore truly portable)
More components, more sophisticated
Pluggable “Look and Feel” support
Better graphics support
Support for Drag and Drop, etc
• Some downsides, though
– Can be slower than AWT
– Memory hungry
– Large and complex API (to meet all requirements)
• Reviewing the Java API
– Some packages we’ve seen
– And some new ones…
• Coding and Design Tips
– Tips for good Java programs
• Where Next?
– Books, Websites, Tutorials
Java Coding Tips
• Never re-invent the wheel!
• Never write a new class, if an existing one
can be extended in some way
– Either through inheritance (or wrapping)
• Look for third-party APIs
– E.g.
• Only then write something yourself
Java Coding Tips
• Make good use of polymorphism
– Use base class references where possible
• Allows you to change the concrete implementation
• Make good use of inheritance
– Add functionality by extending. Don’t rewrite original
– But don’t overuse it, aggregation is often better
• Never break encapsulation
– Practice “defensive programming”
– Don’t return objects you don’t want changed, copy
them instead.
Java Coding Tips
• Use Collections and Iterators, not arrays and for
– More functionality, easier to work with
• Spend time thinking about your object model
– …but don’t worry about getting it right first time
• Learn from others
– Read other peoples code, see how they’ve tackled
similar problems
Java Coding Tips
• Best tip of all:
• Get to know the core Java API
– Read through the Javadoc
– Try building simple test classes to manipulate
the objects
Where Next?
• The Java Tutorial
– Covers all the core functionality very thoroughly
– Divided into “trails” so easy to dip into to learn about
specific functionality
– (Some bits aren’t as up to date as they might be,
• “Thinking in Java” (Bruce Eckel)
– Excellent OO introduction to Java programming
• “Design Patterns” (Gang of Four)
– Recipes for designing OO applications
• “Refactoring” (Martin Fowler)
– Improving the design of existing OO code