Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Class Libraries Chapter 1 1 Source Intro to Java Programming Y. Daniel Liang."— Presentation transcript:

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

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

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

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 4 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. 5 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. 6 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. 7 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. 8 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. 9 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.*; 10 Source Intro to Java Programming Y. Daniel Liang

11 Packages Packages 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. 11 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. 12 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 13 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 14 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 1.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. 15 Source Intro to Java Programming Y. Daniel Liang


Download ppt "Class Libraries Chapter 1 1 Source Intro to Java Programming Y. Daniel Liang."

Similar presentations


Ads by Google