OOPJ Unit 3 Material - Part 2
OOPJ Unit 3 Material - Part 2
Packages
Package Definition
A package is a group of related classes. A package is used to restrict access to a class and to create
namespaces. If all reasonable class names are already used, then we can create a new package (new
namespace) and reuse the class names again. For example a package mypackage1 can contain a
class named MyClass and another package mypackage2 can also contain a class named MyClass.
ClassA.java
package mypackage;
public class ClassA
{
public void methodA()
{
System.out.println("This is methodA in Class A");
}
}
ClassB.java
package mypackage;
package pack1.pack2.pack3;
Remember that packages are normal folders in the file system. So, pack3 is a sub folder in pack2 and
pack2 is a sub folder in pack1.
If no package is specified, by default all the classes are placed in a default package. That is why no
errors are shown even if we don’t use a package statement.
By default Java compiler and JVM searches the current working directory (option 1) for
specified package(s) like mypackage. Let’s assume that our package mypackage is stored at following
location:
D:\packages\mypackage
Then we can set the CLASSPATH (option 2) environment variable (in command prompt) to the
location of the package as shown below:
The dot (.) before the path specifies the current working directory and multiple paths can be
separated using semi-colon (;).
We can also use -classpath or -cp options (option 3) with javac and java commands as shown below:
javac -classpath .;D:\packages Driver.java
or
javac -cp .;D:\packages Driver.java
In the above example, Driver.java is our main program which utilizes the classes in the package
mypackage.
There are multiple ways in which we can import or use a package. They are as follows:
1. Importing all the classes in a package.
2. Importing only necessary classes in a package.
3. Specifying the fully qualified name of the class.
First way is to import all the classes in a package using the import statement. The import statement
is placed after the package statement if any and before all class declarations. We can import all the
classes in a package as shown below:
import mypackage.*;
* in the above line denotes all classes within the package mypackage. Now you are free to directly
use all the classes within that package. A program which demonstrates importing all classes in a
package is given below:
1. java.lang.Object class
Object class is present in java.lang package. Every class in Java is directly or indirectly derived from
the Object class. If a Class does not extend any other class then it is direct child class of Object and if extends
Example:
Illustrates toString( ) method
class Demo
{
public String toString( ) //this method is overriden
{
Return “My demo object created”;
}
public static void main(String args[])
{
System.out.println(new Demo());
}
}
2. Java Wrapper classes
A Wrapper class is a class whose object wraps or contains primitive data types. When we create an
object to a wrapper class, it contains a field and in this field, we can store primitive data types. In
other words, we can wrap a primitive value into a wrapper class object.
The eight classes of the java.lang package are known as wrapper classes in Java. The list of eight
wrapper classes are given above:
Autoboxing
The automatic conversion of primitive data type into its corresponding wrapper class is known as
autoboxing, for example, byte to Byte, char to Character, int to Integer, long to Long, float to Float,
boolean to Boolean, double to Double, and short to Short.
Since Java 5, we do not need to use the valueOf() method of wrapper classes to convert the
primitive into objects.
//Java program to convert primitive into objects -Autoboxing example of int to Integer
public class WrapperExample1
{
public static void main(String args[])
{
//Converting int into Integer
int a=20;
Integer i=Integer.valueOf(a);//converting int into Integer explicitly
Integer j=a;//autoboxing, now compiler will write Integer.valueOf(a) internally
System.out.println(a+" "+i+" "+j);
}
}
Unboxing
The automatic conversion of wrapper type into its corresponding primitive type is known as unboxing.
It is the reverse process of autoboxing. Since Java 5, we do not need to use the intValue() method of
wrapper classes to convert the wrapper type into primitives.