[go: up one dir, main page]

0% found this document useful (0 votes)
8 views14 pages

Final Unit-IV

Uploaded by

blahhwhocares69
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views14 pages

Final Unit-IV

Uploaded by

blahhwhocares69
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 14

Unit-IV Packages

Contents:-

Defining a Package

 Finding packages and CLASSPATH

Important Packages

Access protection

 importing packages

Wrapper Classes, Boxing and Unboxing conversions


1
Packages:
Package:
A package is a container of classes and interfaces. A package represents a directory that
contains related group of classes and interfaces.
For example, when we write statements like:
import java.io.*;
Here we are importing classes of java.io package. Here, java is a directory name and io is

another sub directory within it. The ‘*’ represents all the classes and interfaces of that io
sub directory.

 We can create our own packages called user-defined packages or extend the available
packages. User-defined packages can also be imported into other classes and used exactly in
the same way as the Built-in packages. Packages provide reusability.

 General form for creating a package: package packagename; e.g.: package pack;

 The first statement in the program must be package statement while creating a package

2
Packages:
Package:

 While creating a package except instance variables, declare all the members and the class
itself as public then only the public members are available outside the package to other
programs.

Advantage of Java Package


 Java package is used to categorize the classes and interfaces so that they can be easily
maintained.
 Java package provides access protection.
 Java package removes naming collision.

 Note: Sequence of the program must be package then import then class.

3
Packages:
Package:
Another way to run this program by -classpath switch of java:
 The -classpath switch can be used with javac and java tool.

 Program 1: Write a program to create a package pack with Addition class.

 Code-1: Compile: - javac -d . Addition.java

The –d option tells the Java compiler to create a separate directory and place the .class file in
that directory (package). The (.) dot after –d indicates that the package should be created in
the current directory. So, package pack with Addition class is ready.

Program 2: Write a program to use the Addition class of package pack.


Code-2:

4
Packages:
Package:
 Program 2: Write a program to use the Addition class & Subtraction class of package pack.

 Code-3:

 Program 4: Write a program to access all the classes in the package pack.
To import all the classes and interfaces in a class using import pack.*;

 Code-4:

Note : In this case, please be sure that any of the Addition.java and Subtraction.java
programs will not exist in the current directory. Delete them from the current directory as
they cause confusion for the Java compiler. The compiler looks for byte code in Addition.java
and Subtraction.java files and there it gets no byte code and hence it flags some errors.

5
Packages:
Creating sub Package:

 We can create sub package in a package in the format:


package packagename.subpackagename;
e.g.: package pack1.pack2;

Here, we are creating pack2 subpackage which is created inside pack1 package. To use the
classes and interfaces of pack2, we can write import statement as:
import pack1.pack2;

 Program 5: Program to show how to create a subpackage in package.

 Compile: - javac –d . Sample.java

 Code-5:

6
Packages:

Access Specifier: Specifies the scope of the data members, class and methods.
private members of the class are available within the class only.

public members of the class are available anywhere.

default members of the class are available within the class, outside the class and in its sub

class of same package. It is not available outside the package.


protected members of the class are available within the class, outside the class and in its sub

class of same package and also available to subclasses in different package also.

7
Packages:

 Program 6: Write a program to create class A with different access specifiers.

 Code-6

 Program 7: Write a program for creating class B in the same package.

 Code-7

 Program 8: Write a program for creating class C of another package.

 Code-8

8
Wrapper Class:

The variable of normal data type are send to any method are sent by Value. Hence, No two
methods in a memory can refer same instance of normal variable in memory. But sometimes it
becomes necessary that we want multiple methods to refer the same instance of variable in
memory. For this purpose we need to convert a variable of normal data type to object, this is
done by wrapper class. Wrapper class wraps the normal data type and convert them to object.

Wrapper class in java provides the mechanism to convert primitive into object and object
into primitive.

Primitive Type Wrapper Class


boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double
9
Wrapper Class:

•Autoboxing and Unboxing feature converts primitive into object and object into
primitive automatically. The automatic conversion of primitive into object is known as
Autoboxing and vice-versa Unboxing.

Hierarchy :

10
Wrapper Class:

Number Class:

Methods of Number class:


byte byteValue();

short shortValue();

int intValue();

long longValue();

float floatValue();

double doubleValue();

11
Wrapper Class:

Constructor of Integer Class:


Integer( int i)

Integer(String str) throws NumberFormatException

Method :
int intValue();

Constructor of Character class:


Chracter( char ch)

Method
char charValue();

Constructor of Boolean class:


Boolean( Boolean b)

Boolean( string str)

Method
boolean booleanValue();

12
Wrapper Class:

Static Methods of 'Integer' class:--


i) parseInt():-- static int parseInt(String str)
ii) toBinaryString():-- String toBinaryString(int i)
iii) toOctalString():-- String toOctalString(int i)
iv) toHexString():-- String toHexString(int i)

Static Methods of 'Character' class:--


i)isDigit() :-- boolean isDigit(char ch)
ii)isLetter():-- boolean isLetter(char ch)
iii)isUpperCase():-- boolean isUpperCase(char ch)
iv)isLowerCase():-- boolean isLowerCase(char ch)

Code-9:
Code-10:

13
Wrapper Class:
Built-in Packages in Java:

These packages consist of a large number of classes which are a part of Java API.\
Some of the commonly used built-in packages are:
1) java.lang: Contains language support classes(e.g classed which defines primitive data
types, math operations). This package is automatically imported.

2) java.io: Contains classed for supporting input / output operations.

3) java.util: Contains utility classes which implement data structures like Linked List,
Dictionary and support ; for Date / Time operations.

4) java. Applet: Contains classes for creating Applets.

5) java.awt: Contain classes for implementing the components for graphical user
interfaces (like button , ;menus etc).

6) java.net: Contain classes for supporting networking operations.


14

You might also like