1 / 15

Class Libraries Chapter 1

Learn about the Java language specification, class libraries, and APIs that define the standard for Java programming. Explore the Java API documentation and understand how classes and packages are organized.

ssilva
Download Presentation

Class Libraries Chapter 1

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. Class LibrariesChapter 1 Source Intro to Java Programming Y. Daniel Liang

  2. The Java Language Specification and API Computer languages have strict rules for usages. If you do not follow the rules when writing a program. the computer will be unable to understand it. The Java language specification and Java API define the Java standard. Source Intro to Java Programming Y. Daniel Liang

  3. The Java Language Specification The Java language specification is a technical definition of the language that includes the syntax and semantics of the Java programming language. The complete Java language specification can be found at java.sun.com/docs/books/jls. Source Intro to Java Programming Y. Daniel Liang

  4. A Class library is a set of classes that support the development of programs. The classes in a class library are often related by inheritance. To access the Java APIs Google java api or API – Java2 Application Programmers Interface http://java.sun.com/j2se/1.6/docs/api/overview-summary.html Source Intro to Java Programming Y. Daniel Liang

  5. Java Application Programming Interface (API) Java comes with a rich set of classes and methods that can be used to build applications. The classes and methods of the Java library are listed in the API documentation. The API is still expanding. At the Sun Java (java.sun.com) websit, you can view and download the latest version of the Java API These classes are grouped into packages that provide a convenient way to organize them. You can put the related classes that you develop into packages and distribute them to other people. Think of packages as libraries to be shared by many users. Source Intro to Java Programming Y. Daniel Liang

  6. Java Application Programming Interface (API) consists of numerous classes and interfaces grouped into fifteen core packages, such as, java.lang, java.awt, java.event, java.swing, java.applet, java.util, java.io, java.net. java.lang contains core Java classes (e.g., System, Math, Object, String, StringBuffer, Number, Character, Boolean, Byte, Short, Integer, Long, Float, and Double. This package is implicitly imported to every Java program. These classes are the most frequently used of all classes in the API. Source Intro to Java Programming Y. Daniel Liang

  7. Java Application Programming Interface (API) cont’d. java.awt contains classes for drawing geometrical figures, managing component layout, and creating peer-based (so-called heavyweight) components, such as windows, frames, panels, menus, buttons, fonts, lists, and many others. java.awt.event contains classes for handling events in event-driven graphical user interface programming. Source Intro to Java Programming Y. Daniel Liang

  8. Java Application Programming Interface (API) cont’d. java.awt.swing contains the lightweight graphical user interface components . java.io contains classes for inputs and output streams and files. java.util contains many utilities, such as date, calendar, locale, system properties, sets, lists, vectors, hashing, and stacks. Source Intro to Java Programming Y. Daniel Liang

  9. Java Application Programming Interface (API) cont’d. java.text contains classes for formatting information, such as date and time, in a number of formatting styles based on language, country, and culture. java.net contains classes for supporting network communications. Source Intro to Java Programming Y. Daniel Liang

  10. The import Statement When you want to use a class from the Java API in a program, you can use its fully qualified name including the package name every time it is referenced . java.awt.Graphics Better: import package.class or import package.*; Source Intro to Java Programming Y. Daniel Liang

  11. Packages are used to group classes and interfaces. There are three reasons for using packages. To avoid naming conflicts. When you develop reusable classes and interfaces to be shared by other programmers, naming conflicts often occur. To prevent this, put your classes and interfaces into packages so that they can be referenced through package names. 2. To distribute software. Packages group related classes and interfaces so that they can be easily distributed. 3. To protect classes. Packages provide protection so that the protected members of the classes are accessible to the classes in the same package, but not to external classes. Packages Source Intro to Java Programming Y. Daniel Liang

  12. Package-Naming Conventions Packages are hierarchical, and you can have packages within packages. Example: java.lang.Math indicates that Math is a class in the package lang and that lang is a package in the package java. Must choose a unique name because your package may be used on the Internet: package edu.mcc.java.student; // By conventions package names are // lowercase This must be the first non comment and non blank statement in each of the classes in the package. Source Intro to Java Programming Y. Daniel Liang

  13. Putting Classes into Packages Every class in Java belongs to a package. The class is added to the package when it is compiled. All the classes that were used so far was placed in the current directory (a default package) when the source programs were compiled. package edu.mcc.java.student; // package names are lowercase To place the following three classes in the same package. Must create separate source files for them because one file can have only one public class. Must put the package statement in each class: package edu.mcc.java.student; Address.java Student.java StudentGradeBook.java Source Intro to Java Programming Y. Daniel Liang

  14. Putting Classes into Packages Cont. A class in the package must be declared as public in order to be accessed by other programs. Since the directory for the package is not automatically created using the javac compiler in JDK, you have to manually create the directory before compiling the program. Next, place the .java file in the package directory and then compile it using the javac command. If the .java file is not in the package directory, use the following command with the –d option to specify the destination of the .class file. javac Address.java –d f:\714 Lab6\ edu\mcc\java\student Source Intro to Java Programming Y. Daniel Liang

  15. Using Classes/Interfaces from Packages There are two ways to use classes/interface from a package Use the fully qualified name of the class/interface. For example, the fully qualified name for Date is java.util.Date To use the import statement: To import all classes in the java.util package use: import java.util.*; To import all classes in the edu.rit.java.student.* package use: import edu.mcc.java.student.*; Note only one asterisk(*) can be used in the import statement. Source Intro to Java Programming Y. Daniel Liang

More Related