Package in java is like a container that contains different classes. Java provides several in-built packages and also allows us to create own packages.

In built Packages of Java:
java.lang package bundles the fundamental classes. No need to import this package explicitly. String and Exception classes are found here.
Java.io package contains classes for input,output and file operations.
Java.util package contains classes related to various utilities. Calender, Date, Dictionary classes are available here. It should be imported explicitly.
Java.awt package contains classes related to various window elements like dialog boxes, buttons etc.
java.applet package useful in creating applets
java.net package is to be imported when we want to write program related to networking.

Package is a collection of related classes and interfaces providing access protection and namespace management. Java programmer creates packages to partition classes for managing program. The package statement is used to define space to store classes.

The package statement should be the first line in the source file. If package line is not used, then the class file will be created in the default package.

//example program for Packages
package newpack;
class Demo
{
int k,num;
public void show(int a,int b)
{
System.out.println(“The numbers are:”+a+” “+b);
System.out.println(“The sum is:”+(a+b));
}
}
class packageDemo
{
public static void main(String args[])
{
Demo d=new Demo();
d.show(20,30);
}
}

Steps to create the above program:
1. create a new folder NEWPACK under your working folder
2. save the above program as packageDemo.java in newpack folder
3. compile it and check whether the class files are created under newpack folder
4. come back to your working folder and run the program as java <packname>.<class filename>

If there are multiple classes in a single source file, only one class may be public and it must share the name of source file. Only public package members are accessible from outside the package.
//Another way of creating above package-first file in newpack folder
package newpack;
public class Demo
{
int k,num;
public void show(int a,int b)
{
System.out.println(“The numbers are:”+a+” “+b);
System.out.println(“The sum is:”+(a+b));
}
}

//Second file uses Demo class from new pack-this file to be stored in your working folder
import newpack.*;
public class packageDemo
{
public static void main(String args[])
{
Demo d=new Demo();
d.show(20,30);
}
}

//third file uses Demo class from newpack- this file to be stored in your working folder
import newpack.*;
public class usepackDemo
{
public void main(String args[])
{
Demo d2=new Demo();
d2.show(40,60);
}
}