What is a predefined package in Java?

You can create the .class files of all the Java classes and interfaces related to each other in one folder automatically by declaring them under same package. A package is nothing but a directory storing classes and interfaces of a particular concept.

Creating a package

You can create a package and add required classes/interfaces in it just by declaring the package on the top of the Class/Interface files using the keyword package as −


Package package_name;


Following Java program, demonstrates the declaration of a package in Java.

package com.tutorialspoint.samplePackage;
public class PackageExample{
   public void display(){
      System.out.println("Welcome to Tutorialspoint");

To compile this program (programs with packages), you need to use the –d option of the javac command. At this you need to specify the path where you need to create the package.

javac -d E:\ Sample.java

If you want the package in the current directory just use “.” instead of the path as −

javac -d . Sample.java

Predefined packages

Java provides various predefined classes and interfaces (API’s) organized under packages. These are known as predefined packages, following is the list of predefined packages in java −

  • java.lang − This package provides the language basics.
  • java.util − This packages provides classes and interfaces (API’s) related to collection frame work, events, data structure and other utility classes such as date.
  • java.io − This packages provides classes and interfaces for file operations, and other input and output operations.
  • java.math − This packages provides classes and interfaces for multiprecision arithmetics.
  • java.nio − This packages provides classes and interfaces the Non-blocking I/O framework for Java
  • java.net − This packages provides classes and interfaces related to networking.
  • java.security − This packages provides classes and interfaces such as key generation, encryption and decryption which belongs to security frame work.
  • java.sql − This packages provides classes and interfaces for accessing/manipulating the data stored in databases and data sources.
  • java.awt − This packages provides classes and interfaces to create GUI components in Java.
  • java.text − This packages provides classes and interfaces to handle text, dates, numbers, and messages.
  • java.rmi − Provides the RMI package.
  • java.time − The main API for dates, times, instants, and durations.
  • java.beans − The java.beans package contains classes and interfaces related to JavaBeans components.

All these packages are available in the rt.jar file in the bin folder of your JRE (Java Runtime Environment). Just like normal packages, to use a particular class you need to import its respective package.


In the following Java example, we are trying to read data from the keyboard using the Scanner class of the java.util package. Since it does not belong to the default package we are importing the required it using the import statement.

import java.util.Scanner;
class Student2{
   String name;
   int age;
   float percent;
   boolean isLocal;
   Student2(String name, int age, float percent, boolean isLocal){
      this.name = name;
      this.age = age;
      this.percent = percent;
      this.isLocal = isLocal;
   public void displayDetails(){
      System.out.println("Name: "+this.name);
      System.out.println("Age: "+this.age);
      System.out.println("Percent: "+this.percent);
      if(this.isLocal) {
         System.out.println("Nationality: Indian");
      } else {
         System.out.println("Nationality: Foreigner");
public class ReadData2 {
   public static void main(String args[]){
      //Instantiating the Scanner class
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter your name: ");
      String name = sc.next();
      System.out.println("Enter your age: ");
      int age = sc.nextInt();
      System.out.println("Enter your percent: ");
      float percent = sc.nextFloat();
      System.out.println("Are you local (enter true or false): ");
      boolean isLocal = sc.nextBoolean();
      Student2 std = new Student2(name, age, percent, isLocal);


Enter your name:
Enter your age:
Enter your percent:
Are you local (enter true or false):
Name: Krishna
Age: 26
Percent: 86.0
Nationality: Indian