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

Slides:



Advertisements
Similar presentations
1 Packages: Putting Classes Together. 2 Introduction The main feature of OOP is its ability to support the reuse of code: Extending the classes (via inheritance)
Advertisements

Java Packages CSci 1130 Intro to Computer Programming with Java Instructor Tatyana Volk.
Importing methods in the Java library. Previously discussed Method = a collection of statements that performs a complex (useful) task A method is identified.
Packages Sudhir Talasila Preeti Navale. Introduction Packages are nothing more than the way we organize files into different directories according to.
PACKAGES. PACKAGES IN JAVA A package is a collection of related classes and interfaces in Java Packages help in logical grouping of classes and interfaces.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 1 Introducing Java.
Chapter 3 - Java Programming With Supplied Classes1 Chapter 3 Java Programming With Supplied Classes.
Java Programming Chapter 3 More about classes. Why?  To make classes easier to find and to use  to avoid naming conflicts  to control access  programmers.
1 Lecture 2 Java Packages  What are Java packages?  Why do wee need packages?  How to create a Java package?  Some examples.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
1 Chapter 3 Programs and Packages. 2 Java Virtual Machine (JVM) Java programs execute on the JVM. The JVM is a virtual rather than a physical machine,
Unit 051 Packages What is a Package? Why use Packages? Creating a Package Naming a Package Using Package Members Managing Source and Class Files Visibility.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
Java Software Solutions Lewis and Loftus Chapter 2 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Software Concepts -- Introduction.
PACKAGES Mimi OpkinsCECS277. What We’ll Cover  Packages and Import Statements  The Package java.lang  Package Names and Directories  The Default Package.
Access Control Problem A primary consideration in object- oriented design is to “separate the things that change from the things that stay.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Packages.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
Java Packages and Libraries M Taimoor Khan
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Method Abstraction You can.
Packages. Package 1.Avoid naming conflicts. Put classes into packages so that they can be referenced through package names. 2.Distribute software conveniently.
JAVA PROGRAMMING BASICS CHAPTER 2. History of Java Begin with project Green in 1991 founded by Patrick Noughton, Mike Sheridan and James Gosling who worked.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
成都信息工程学院 计算机系 1 Java 程序设计 Java Programming Fall, 2010.
1 JAVA API & Packages Starring: Java Documentation Co-Starring: BlueJ IDE.
23-October-2002cse JavaIntro © 2002 University of Washington1 Intro to Java CSE 413, Autumn 2002 Programming Languages
Using methods in the Java library. Previously discussed Method = a collection of statements that performs a complex (useful) task A method is identified.
School of Computer Science & Information Technology G6DICP - Lecture 14 Class Libraries.
9.1 Java Packages A collection of classes Allows classes to be grouped arbitrarily Hierarchical structure independent of inheritance Classes can.
Application Programming Interfaces. Java comes with a bunch of classes that are already written. Java comes with a bunch of classes that are already written.
JAVA INTRODUCTION. What is Java? 1. Java is a Pure Object – Oriented language 2. Java is developing by existing languages like C and C++. How Java Differs.
Java Software Solutions Lewis and Loftus Chapter 2 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Software Concepts -- Introduction.
Creating a Java Application and Applet
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
1 Chapter 6 Programming with Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive.
Classes, Interfaces and Packages
Packages. The main feature of OOP is its ability to support the reuse of code: –Extending the classes –Extending interfaces The features in basic form.
1 Wrapper Classes  Sometimes we want to use a primitive type as an object, so there are wrapper classes that will help us.  In particular, we need to.
CSI 3125, Preliminaries, page 1 Packages. CSI 3125, Preliminaries, page 2 Packages Packages are containers for classes Collection of classes Packages.
Programming in Java (COP 2250) Lecture 7 Chengyong Yang Fall, 2005.
Methods. Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
April 20, 1998CS102-02Lecture 4-1 A Method to the Madness CS Lecture 4-1 Java's Work Horses.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Today Javadoc. Packages and static import. Viewing API source code. Upcoming Topics: –protected access modifier –Using the debugger in Eclipse –JUnit testing.
1 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used as a type to declare an object reference.
Java Packages  What are Java packages?  Why do we need packages?  How to create a Java package?  Some examples.  Coming Next: Program Documentation.
1 Stack and Heap. 2 “Stack” Stack – a linear data structure in which items are added and removed in last-in, first-out order. Definition: context – the.
Java Packages - allow you to group classes as a collection Standard Java library has packages: java.lang and java.util … Also hierarchical: java and javax.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Variables in Java A variable holds either
Topic: Packages Course : JAVA PROGRAMMING Paper Code: ETCS-307
Inheritance-Basics.
Classes, Libraries & Packages
Java Basics Packages.
Packages Written part of final exam Alex Rudniy
Applying OO Concepts Using Java
PACKAGES.
Building a program (Java libraries) - an example
Joel Adams and Jeremy Frens Calvin College
Using java libraries CGS3416 spring 2019.
Chap 1. Getting Started Objectives
CMPE212 – Reminders Assignment 2 due today, 7pm.
Chap 4. Programming Fundamentals
Presentation transcript:

Class Libraries Chapter 1 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.

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 summary.html 4 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. 5 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. 6 Source Intro to Java Programming Y. Daniel Liang

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

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

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

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

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

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

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

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

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