1 / 27

Java Packages and Libraries

Java Packages and Libraries. M Taimoor Khan taimoorkhan@ciit-attock.edu.pk. Content. What is a class library? What is a package? Java class libraries, what do they provide? The root class : java.lang.object. The Java class library. Thousands of classes Tens of thousands of methods

will
Download Presentation

Java Packages and Libraries

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Packages and Libraries M Taimoor Khan taimoorkhan@ciit-attock.edu.pk

  2. Content • What is a class library? • What is a package? • Java class libraries, what do they provide? • The root class : java.lang.object

  3. 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 • http://java.sun.com/javase/reference/api.jsp

  4. Working with the library You should: • know some important classes by name • know how to find out about other classes • http://java.sun.com/javase/6/docs/api/ Remember: • We only need to know the interface, not the implementation The public face of the class

  5. 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?

  6. 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

  7. Interface vs implementation The documentation includes • the name of the class;

  8. Interface vs implementation The documentation includes • a general description of the class;

  9. Interface vs implementation The documentation includes • a list of constructors

  10. Interface vs implementation The documentation includes • a list of methods

  11. Interface vs implementation The documentation includes • return values and parameters for constructors and methods

  12. Interface vs implementation The documentation does not include • private fields (most fields are private) • private methods • the implementation (source code) for each method

  13. 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. • http://java.sun.com/docs/books/tutorial/java/package/usepkgs.html • “The Java compiler automatically imports” • java.lang package • the package of the current file source code

  14. 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.*; Some of the Packages listed in the API Packages are NOT hierarchical Using “import java.awt.*;” does not import java.awt.color.*;

  15. Using Random • The library class Random can be used to generate random numbers import java.util.Random; ... Random randomGenerator = new Random(); ... int index1 = randomGenerator.nextInt(); int index2 = randomGenerator.nextInt(100);

  16. Generating random responses public Responder() { randomGenerator = new Random(); responses = new ArrayList<String>(); fillResponses(); } public String generateResponse() { int index = randomGenerator.nextInt(responses.size()); return responses.get(index); } public void fillResponses() { ... }

  17. Java Packages • An overview can be found at this link • http://java.sun.com/javase/6/docs/api/overview-summary.html

  18. Java Packages • Inside each package you will find • Interfaces • Classes • Enumerations • Exceptions

  19. java.lang • “Provides classes that are fundamental to the design of the Java programming language.” • Boolean, Byte, Double, Integer, Long, String • Math • System • Object

  20. java.util • “Contains the collections framework…date and time facilities,… string tokenizer.. random-number generator..” • ArrayList<E>, LinkedList<E>, Stack<E> • Calendar, Date • Formatter • Scanner • StringTokenizer • Random • Iterator<E>

  21. java.io • “Provides for system input and output through data streams, serialization and the file system.” • Console • File • FileReader • BufferedInputStream • StreamTokenizer

  22. GUI libraries • java.awt • java.awt.Event • javax.imageio • javax.swing • http://java.sun.com/docs/books/tutorial/uiswing/

  23. All classes are Objects

  24. Packages

  25. Reading & Exercises • P136 – p147 • Collections • http://java.sun.com/docs/books/tutorial/collections/index.html • Swing • http://java.sun.com/docs/books/tutorial/ui/index.html • Classes and Objects • http://java.sun.com/docs/books/tutorial/java/javaOO/index.html • Generics • http://java.sun.com/docs/books/tutorial/java/generics/index.htm

  26. References • http://java.sun.com/javase/reference/api.jsp • http://java.sun.com/docs/books/tutorial/java/package/managingfiles.html • http://en.wikipedia.org/wiki/Classpath_(Java) • http://condor.depaul.edu/~glancast/224class/docs/lecJun16.html

More Related