Vous êtes sur la page 1sur 30

Java Packages

Objectives
• In this lesson, you will learn about:
• Packages in Java
• User-defined packages
• Exploring the java.lang package
• Exploring the java.util package

©NIIT Packages and Streams Lesson 1A / Slide 1 of 30


Java Packages

Packages in Java
• A package is a set of classes that are stored in a directory, which has the same
name as the package name.
• Packages enable you to organize the class files provided by Java.
• Java packages are classified into two categories:
• Java API packages
• Java user-defined packages

©NIIT Packages and Streams Lesson 1A / Slide 2 of 30


Java Packages

Packages in Java (Contd.)


• The following table lists a few built-in Java packages:

Java Package Description


Name

java.lang Provides various classes, such as Object,


System, and Class.

java.util Provides various classes that support


collection or groups of objects, such as hash
tables, String parsing, and system properties.

java.io Defines two streams, InputStream and


OutputStream that determine the flow of
bytes from a source to destination.

©NIIT Packages and Streams Lesson 1A / Slide 3 of 30


Java Packages

Packages in Java (Contd.)


• The various built-in packages of Java are: (Contd.)

Java Package Description


Name

java.awt Provides classes to implement graphical user


interface, such as creating buttons, check boxes,
text boxes, menus, and list boxes.

java.net Provides classes that support network


programming, such as Socket, ServerSocket, and
DatagramSocket.

java.applet Provides the Applet class that provides methods to


display images, play audio files, and obtain
information about the applet environment. Some of
these methods are play(), getImage(),
getAppletInfo(), and getAudioClip().

©NIIT Packages and Streams Lesson 1A / Slide 4 of 30


Java Packages

Packages in Java (Contd.)


• The hierarchy of the Java API packages:

©NIIT Packages and Streams Lesson 1A / Slide 5 of 30


Java Packages

User–Defined Packages
• When you write a Java program, you create many classes. You can organize
these classes by creating your own packages.
• The packages that you create are called user-defined packages.
• A user-defined package contains one or more classes that can be imported in a
Java program.
• Creating a user-defined package
• The following syntax shows how to create a user-defined package:
package <package_name>
// Class definition
public class <classname1>
{
// Body of the class.
}
public class <classname2>
{
// Body of the class.}
©NIIT Packages and Streams Lesson 1A / Slide 6 of 30
Java Packages

User–Defined Packages (Contd.)


• To create a user-defined package, perform the following steps:
• Create a source file containing the package definition
• Create a folder having the same name as package name and save
the source file within the folder.
• Compile the source file.
• Importing a user-defined package
• You can include a user-defined package using the import statement.
• The following syntax shows how to implement the user-defined
package, empDetails from the app directory in a program:
import app.empDetails.Employee;
public class Director extends Employee
{
// Body of the class.
}

©NIIT Packages and Streams Lesson 1A / Slide 7 of 30


Java Packages

User-Defined Packages (Contd.)


• The CLASSPATH variable points to the directories in which the
classes that are available to import are present.
• CLASSPATH enables you to put the class files in various directories
and notifies the JDK tools of the region of these classes.

©NIIT Packages and Streams Lesson 1A / Slide 8 of 30


Java Packages

Exploring java.lang Package


• The java.lang package provides various classes and interfaces that are
fundamental to Java programming.
• The java.lang package contains various classes that represent primitive data
types, such as int, char, long, and double.
• Classes provided by the java.lang package:

Class Description

Object All the classes in the java.lang package are


subclasses of the Object class and inherit its
methods. The toString() method creates a
String representation of the value of an object of
the Object class. This method is automatically
called when you display an Object using the
println() method.

©NIIT Packages and Streams Lesson 1A / Slide 9 of 30


Java Packages

Exploring java.lang Package (Contd.)


Class Description

Class Supports runtime processing of the class


information of an object. The Class class also
supports the toString() method.

System Provides a standard interface to input, output, and


error devices, such as keyboard and Visual Display
Unit (VDU). The System class has the variables, in,
out, and err represent input, output, and error
respectively.
Wrapper Represents the primitive data types, such as int,
char, and double, as objects. In addition, the
Wrapper class provides methods to convert strings
to various data types.

©NIIT Packages and Streams Lesson 1A / Slide 10 of 30


Java Packages

Exploring java.lang Package (Contd.)


Class Description

Integer Provides methods for converting an int object to a


String object.

Math Provides various numeric constants and methods


for statistics, logarithms, exponents, and
trigonometry.

String Supports operations on Strings and characters.

©NIIT Packages and Streams Lesson 1A / Slide 11 of 30


Java Packages

Exploring java.lang Package (Contd.)


• The Math Class
• The java.lang.Math class provides various methods for performing
numeric operations, such as calculating the square root of a number,
absolute value of a number, maximum and minimum values, random,
and trigonometric functions such as sin(), cos(), and tan().

©NIIT Packages and Streams Lesson 1A / Slide 12 of 30


Java Packages

Exploring java.lang Package (Contd.)

Method Description Syntax

abs() Accepts a parameter of data type, such as public static double


int, long, float, or double and returns the abs(double x)
absolute value of that parameter. public static float
  abs(float x)
  public static int
abs(int x)
ceil() Returns the smallest double value of the  
public static double
next higher integer. ceil(double x)
 
 
floor() Returns the largest double value of the public static double
next smaller integer. floor(double x)

©NIIT Packages and Streams Lesson 1A / Slide 13 of 30


Java Packages

Exploring java.lang Package (Contd.)

Method Description Syntax

max() Accepts two arguments and returns the public static float
larger of the two values of the arguments. max(float x, float y)
The two arguments should be of same data public static double
type such as float, double, int, or long. max(double x, double y)
  public static int
  max(int x, int y)
min() Accepts two parameters and returns the  public static float
smaller of the two values of the min(float x, float y)
arguments. The two parameters should be public static double
of same data type such as float, double, min(double x, double y)
int, or long. public static int
  min(int x, int y)
   
©NIIT Packages and Streams Lesson 1A / Slide 14 of 30
Java Packages

Exploring java.lang Package (Contd.)


Method Description Syntax

random() Accepts no argument and returns a public static double


positive, double, value randomly random()
generated, greater than or equal to 0.0
and less than 1.0.

round() Accepts one parameter of the double or public static long


float data type and returns the integer
value closest to the parameter. round(double
x)

sin() Accepts an angle in radian as parameter public static


public static double
int
and returns the trigonometric sine of the round(float x)
sin(double x)
angle.

©NIIT Packages and Streams Lesson 1A / Slide 15 of 30


Java Packages

Exploring java.lang Package (Contd.)

Method Description Syntax

cos() Accepts an angle in radian as public static


parameter and returns the double cos(double
trigonometric cosine of the angle. x)

tan() Accepts an angle in radian as public static


parameter and returns the double tan(double
trigonometric tangent of the angle. x)

sqrt() Accepts one parameter of data type public static


double and returns the positive double sqrt(double
square root of that parameter. x)

©NIIT Packages and Streams Lesson 1A / Slide 16 of 30


Java Packages

Exploring java.lang Package (Contd.)


• The String Class
• Methods of the String class:

Method Description Syntax

length() Returns the length of a public int length()


String object.

charAt() Returns the character at the public char charAt(int


specified index, which index)
ranges from 0 to the second
last element.
getChars() Copies characters from a public void getChars(int
source String object into the srcBegin, int srcEnd,
destination character array. char[] dstn, int dstnBegin)

©NIIT Packages and Streams Lesson 1A / Slide 17 of 30


Java Packages

Exploring java.lang Package (Contd.)


Method Description Syntax

equals() Accepts one argument and public Boolean equals(Object


compares the current String obj)
object with an object of the
Object class. The result is true if
the argument is not null.
CompareTo() Compares the current String public int compareTo(String
object with another String. If the String2)
strings are same, the return
value is 0 else the return value
is non-zero.
startsWith( Tests whether a String object public Boolean
) starts with the specified prefix startsWith(String prefix, int
beginning at the specified index offset)
or not. public Boolean
startsWith(String prefix)
©NIIT Packages and Streams Lesson 1A / Slide 18 of 30
Java Packages

Exploring java.lang Package (Contd.)


Method Description Syntax

subString( Returns a substring of a string. The public String subString(int


) substring begins with the character beginIndex)
at the specified index and extends
to the end of the main string.

concat() Concatenates the specified String public String concat(String


to the end of the String object. str)

toUpperCas Converts all the characters in the public String toUpperCase()


e() String object to upper case by
using the default locale.
toLowerCas Converts all the characters in the public String toLowerCase()
e() calling String object to lower case
by using the default locale.
©NIIT Packages and Streams Lesson 1A / Slide 19 of 30
Java Packages

Exploring java.lang Package (Contd.)


• The Wrapper Class

Wrapper Class Objects Description Syntax

doubleValue() Returns the value of an public double


object as the primitive data doubleValue()
type double.
floatValue() Returns the value of an public float
object as the primitive data floatValue()
type float.
intValue() Returns the value of an public int
object as the primitive data intValue()
type int.

©NIIT Packages and Streams Lesson 1A / Slide 20 of 30


Java Packages

Exploring java.lang Package


(Contd.)
Wrapper Class Description Syntax

toString() Returns a String •public String


representation of an object toString()
of the data types byte, int, •public static
long, float, double, or String
short. toString(byte
val)
 
toHexString() Creates a String public static
representation of the String
specified argument as an toHexString(int
unsigned integer in val)
hexadecimal form.  

©NIIT Packages and Streams Lesson 1A / Slide 21 of 30


Java Packages

Exploring java.util Package


• The java.util package provides various utility classes and interfaces that
support date/calendar operations, String manipulation, parsing, and basic
event processing.
•Various classes of the java.util package:
Class Description

BitSet Creates a set of bits that grows as required.

Date Encapsulates date and time information.

Hashtable Implements a hash table, which maps keys to values.

Random Generates a stream of random numbers.

©NIIT Packages and Streams Lesson 1A / Slide 22 of 30


Java Packages

Exploring java.util Package (Contd.)

Class Description

StringToken Provides method to break strings into tokens.


izer

Arrays Provides various methods to perform various operations on arrays,


such as searching and sorting.

Calendar Provides support for date conversion and can be extended to provide
conversion for specific calendar systems.

GregorianC Provides the standard calendar used worldwide. It is a subclass of the


alendar Calendar class.

©NIIT Packages and Streams Lesson 1A / Slide 23 of 30


Java Packages

Exploring java.util Package (Contd.)


• Few interfaces of the java.util package:

Interface Description

Map Provides an object that maps keys to values.

Enumeration Generates a series of elements.

List An ordered list of elements, which may contain


duplicate objects.

Set A group of objects with no duplication.

©NIIT Packages and Streams Lesson 1A / Slide 24 of 30


Java Packages

Exploring java.util Package (Contd.)


• The Collection Interface
• A collection is an object that contains a group of objects within it.
• These objects are called the elements of the collection. The elements
of a collection descend from a common parent type.
• Collections have an advantage over arrays that collections can grow to
any size unlike arrays.
• The following constructors are used for the collection interface:
• CollectionName(): Creates an empty collection. A void
constructor does not accept any argument.
• CollectionName(Collection coll): Creates a new collection
that accepts a collection as an argument and returns the
collection containing the same elements as the collection
argument.

©NIIT Packages and Streams Lesson 1A / Slide 25 of 30


Java Packages

Exploring java.util Package (Contd.)


The following figure shows the hierarchy of the collection interface:

©NIIT Packages and Streams Lesson 1A / Slide 26 of 30


Java Packages

Exploring java.util Package (Contd.)


• The various sub interfaces of the Collection interface are :
•List
•Set
•SortedSet
• Legacy Classes and Interfaces
• The classes of the java.util package were updated to support the concept
of collection framework.
• These classes are referred to as the legacy classes.
• The various legacy classes defined by the java.util package are:
• Vector
• Stack
• Hashtable
• Properties
• The interface defined by the java.util package is Enumeration interface.

©NIIT Packages and Streams Lesson 1A / Slide 27 of 30


Java Packages

Summary
In this lesson, you learned:

• Packages enable you to organize the class files provide by Java.


• The various built-in packages of the Java programming language are:
• java.lang
• java.util
• java.io
• java.applet
• java.awt
• java.net
• The packages created by users are called user-defined packages.
• The import statement with the package name enables you to inform the
compiler about the region of classes.
• The java.lang package provides a number of classes and interfaces that are
fundamental to Java programming.

©NIIT Packages and Streams Lesson 1A / Slide 28 of 30


Java Packages

Summary (Contd.)
• Some of the classes defined in the java.lang package are :
• The various built-in packages of the Java programming language are:
• Object
• Class
• System
• Wrapper
• Character
• Integer
• Math
• String
• The java.util package provides various utility classes and interfaces that
support date/calendar operations, String manipulation, parsing, and basic
event processing

©NIIT Packages and Streams Lesson 1A / Slide 29 of 30


Java Packages

Summary (Contd.)
• Some of the classes defined in the java.util package are :
• BitSet
• Date
• Calendar
• GregorianCalendar
• Random
• StringTokenizer
• Arrays
• The various interfaces defined in the java.util package are:
• Collection
• List
• Set
• SortedSet      

©NIIT Packages and Streams Lesson 1A / Slide 30 of 30

Vous aimerez peut-être aussi