Java Packages and Libraries M Taimoor Khan
Content What is a class library? What is a package? Java class libraries, what do they provide? The root class : java.lang.object 2
The Java class library Thousands of classes Tens of thousands of methods Many useful classes that make life much easier A Java programmer must be able to work with the libraries o 3
Working with the library You should: know some important classes by name know how to find out about other classes o Remember: We only need to know the interface, not the implementation 4 The public face of the class
The exit condition String input = reader.getInput(); if(input.startsWith("bye")) { finished = true; } Where does ‘startsWith’ come from? What is it? What does it do? How can we find out? 5
Reading class documentation Documentation of the Java libraries in HTML format; Readable in a web browser Class API: Application Programmers’ Interface Interface description for all library classes 6
Interface vs implementation The documentation includes the name of the class; 7
Interface vs implementation The documentation includes a general description of the class; 8
Interface vs implementation The documentation includes a list of constructors 9
Interface vs implementation The documentation includes a list of methods 10
Interface vs implementation The documentation includes return values and parameters for constructors and methods 11
Interface vs implementation The documentation does not include private fields (most fields are private) private methods the implementation (source code) for each method 12
Using library classes Classes from the library must be imported using an import statement (except classes from java.lang). They can then be used like classes from the current project. gs.html gs.html 13 “The Java compiler automatically imports” java.lang package the package of the current file source code
Packages and import Classes are organised in packages. Single classes may be imported: import java.util.ArrayList; Whole packages can be imported: import java.util.*; 14 Some of the Packages listed in the API Packages are NOT hierarchical Using “ import java.awt.*;” does not import java.awt.color.*;
Using Random The library class Random can be used to generate random numbers 15 import java.util.Random;... Random randomGenerator = new Random();... int index1 = randomGenerator.nextInt(); int index2 = randomGenerator.nextInt(100);
Generating random responses 16 public Responder() { randomGenerator = new Random(); responses = new ArrayList (); fillResponses(); } public String generateResponse() { int index = randomGenerator.nextInt(responses.size()); return responses.get(index); } public void fillResponses() {... }
Java Packages An overview can be found at this link summary.html summary.html 17
Java Packages Inside each package you will find o Interfaces o Classes o Enumerations o Exceptions 18
java.lang “Provides classes that are fundamental to the design of the Java programming language.” Boolean, Byte, Double, Integer, Long, String String Math System Object 19
java.util “Contains the collections framework…date and time facilities,… string tokenizer.. random-number generator..” ArrayList, LinkedList, Stack ArrayList LinkedList Stack Calendar, Date Calendar Date Formatter Scanner StringTokenizer Random Iterator 20
java.io “Provides for system input and output through data streams, serialization and the file system.” Console File FileReader BufferedInputStream StreamTokenizer 21
GUI libraries java.awt o java.awt.Event javax.imageio javax.swing o
All classes are Objects 23
Packages 24
25
Reading & Exercises P136 – p147 Collections o Swing o Classes and Objects o ex.html ex.html Generics o ex.htm ex.htm 26
References agingfiles.html agingfiles.html html html 27