class libraries chapter 1 1 source intro to java programming y. daniel liang

15
Class Libraries Chapter 1 1 Source Intro to Java Programming Y. Daniel Liang

Upload: stephanie-phelps

Post on 05-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Class Libraries Chapter 1 1 Source Intro to Java Programming Y. Daniel Liang

Class LibrariesChapter 1

1Source Intro to Java Programming Y. Daniel Liang

Page 2: Class Libraries Chapter 1 1 Source Intro to Java Programming Y. Daniel Liang

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.

Page 3: Class Libraries Chapter 1 1 Source Intro to Java Programming Y. Daniel Liang

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 Javaprogramming language.

The complete Java language specification can be found at java.sun.com/docs/books/jls.

Page 4: Class Libraries Chapter 1 1 Source Intro to Java Programming Y. Daniel Liang

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 APIsGoogle java api or

API – Java2 Application Programmers Interfacehttp://java.sun.com/j2se/1.6/docs/api/overview-summary.html

4Source Intro to Java Programming Y. Daniel Liang

Page 5: Class Libraries Chapter 1 1 Source Intro to Java Programming Y. Daniel Liang

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.

5Source Intro to Java Programming Y. Daniel Liang

Page 6: Class Libraries Chapter 1 1 Source Intro to Java Programming Y. Daniel Liang

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. 6Source Intro to Java Programming Y. Daniel

Liang

Page 7: Class Libraries Chapter 1 1 Source Intro to Java Programming Y. Daniel Liang

Java Application Programming Interface (API) cont’d.

java.awtcontains 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.eventcontains classes for handling events in event-driven graphical user interface programming.

7Source Intro to Java Programming Y. Daniel Liang

Page 8: Class Libraries Chapter 1 1 Source Intro to Java Programming Y. Daniel Liang

Java Application Programming Interface (API) cont’d.

java.awt.swing contains the lightweight graphical user interface components .

java.iocontains classes for inputs and output streams and files.

java.utilcontains many utilities, such as date, calendar, locale, system properties, sets, lists, vectors, hashing, and stacks. 8Source Intro to Java Programming Y. Daniel

Liang

Page 9: Class Libraries Chapter 1 1 Source Intro to Java Programming Y. Daniel Liang

Java Application Programming Interface (API) cont’d.

java.textcontains classes for formatting information, such as date and time, in a number of formatting styles based on language, country, and culture.

java.netcontains classes for supporting network communications.

9Source Intro to Java Programming Y. Daniel Liang

Page 10: Class Libraries Chapter 1 1 Source Intro to Java Programming Y. Daniel Liang

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.classor import package.*;

10Source Intro to Java Programming Y. Daniel Liang

Page 11: Class Libraries Chapter 1 1 Source Intro to Java Programming Y. Daniel Liang

PackagesPackages are used to group classes and interfaces. There are three reasons for using packages.1. 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. 11Source Intro to Java Programming Y. Daniel

Liang

Page 12: Class Libraries Chapter 1 1 Source Intro to Java Programming Y. Daniel Liang

Package-Naming ConventionsPackages are hierarchical, and you can have packageswithin 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. 12Source Intro to Java Programming Y. Daniel

Liang

Page 13: Class Libraries Chapter 1 1 Source Intro to Java Programming Y. Daniel Liang

Putting Classes into PackagesEvery class in Java belongs to a package. The class is added tothe package when it is compiled. All the classes that were used so farwas 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 publicclass. Must put the package statement in each class:

package edu.mcc.java.student; Address.java Student.java StudentGradeBook.java

13Source Intro to Java Programming Y. Daniel Liang

Page 14: Class Libraries Chapter 1 1 Source Intro to Java Programming Y. Daniel Liang

Putting Classes into Packages Cont.

A class in the package must be declared as public in order tobe accessed by other programs.

Since the directory for the package is not automaticallycreated 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

14Source Intro to Java Programming Y. Daniel Liang

Page 15: Class Libraries Chapter 1 1 Source Intro to Java Programming Y. Daniel Liang

Using Classes/Interfaces from PackagesThere are two ways to use classes/interface from a package1. Use the fully qualified name of the class/interface. For example, the

fully qualified name for Date is java.util.Date

2. 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.15Source Intro to Java Programming Y. Daniel

Liang