Lecture 2.1 - Java Packages
Lecture 2.1 - Java Packages
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
1
Java Packages & API
A java package is a group of similar types
of classes, interfaces and sub-packages.
Think of it as a folder in a file directory.
We use packages to avoid name conflicts,
and to write a better maintainable code.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
2
Package Categories
Built-in Packages (packages from the Java
API)
User-defined Packages (create your own
packages)
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
3
Built-in Packages
The Java API is a library of prewritten
classes, that are free to use, included in the
Java Development Environment.
The library contains components for
managing input, database programming,
and much more.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
4
Java API
Java API is divided into
– Packages
Contains classes and other packages
– Classes
Contains methods
– methods
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
5
Structure of Java API
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
6
Import keyword
To use a class or a package from the library,
you need to use the import keyword:
There are many built-in packages such as
java, lang, awt, javax, swing, net, io, util,
sql etc.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
7
Importing
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
8
Syntax
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
9
Import a Class
If you find a class you want to use, for
example, the Scanner class, which is used to
get user input, write the following code:
Example
– import java.util.Scanner;
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
10
Scanner class
In the example above, java.util is a package,
while Scanner is a class of the java.util
package.
To use the Scanner class, create an object of
the class and use any of the available
methods found in the Scanner class
documentation.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
11
In our example, we will use the nextLine()
method, which is used to read a complete
line:
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
12
Example
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
13
Import a Package
To import a whole package, end the
sentence with an asterisk sign (*). The
following example will import ALL the
classes in the java.util package:
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
14
Other Packages
Java
Javax
etc
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
15