Packages in Java

Java Package Introduction:

In Java, a package is a way of organizing classes and interfaces into a single namespace, making it easier to manage and maintain large codebases. It also helps in avoiding naming conflicts by providing a unique namespace for the classes and interfaces within a package. Packages are an essential part of Java's modular approach and are widely used in Java development.

In Java, there are two types of packages:

1) In-build package

2) User-defined package

In-build package:In Java, there are several built-in packages that come with the standard Java Development Kit (JDK). These packages contain classes and utilities that provide various functionalities, making it easier to develop Java applications. Some of the In-build packages of java are  java.lang , java.util , java.io , java.awt .

java.lang package:This package is automatically imported in all Java programs and contains fundamental classes that are essential for every Java application.

public class Main {
    public static void main(String[] args) {
        // Using classes from java.lang package without explicit import
        String message = "Hello, World!";
        System.out.println(message);

        int num = Integer.parseInt("42");
        System.out.println(num);
    }
}
java.util package: This package contains utility classes for data structures, collections, date and time, random number generation, and more.
 import java.util.ArrayList;
import java.util.HashMap;

public class Main {
    public static void main(String[] args) {
        // Using ArrayList from java.util package
        ArrayList names = new ArrayList<>();
        names.add("Alice");
        names.add("Bob");
        names.add("Charlie");
        System.out.println(names);

        // Using HashMap from java.util package
        HashMap ages = new HashMap<>();
        ages.put("Alice", 30);
        ages.put("Bob", 25);
        ages.put("Charlie", 35);
        System.out.println(ages);
    }
}
java.io package: This package provides classes for input and output operations, such as reading from and writing to files.
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        // Using FileWriter from java.io package to write to a file
        try {
            FileWriter writer = new FileWriter("output.txt");
            writer.write("Hello, File!");
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
java.awt package: This package provides classes for creating graphical user interfaces (GUI) for Java applications.
import java.awt.Frame;
import java.awt.Label;

public class Main {
    public static void main(String[] args) {
        // Using classes from java.awt package to create a simple GUI window
        Frame frame = new Frame("My Window");
        Label label = new Label("Hello, GUI!");
        frame.add(label);
        frame.setSize(300, 100);
        frame.setVisible(true);
    }
}
User Defined Package: In Java, we can create our own user-defined packages to organize classes and interfaces into a custom namespace. User-defined packages follow the same principles as built-in packages but allow us to group related classes and interfaces based on our application's requirements. 

Creating User Defined Package and use the package takes this following steps :

Create the Directory Structure: In your project directory, create a directory structure that matches the package name you want to use. Each level of the package name corresponds to a subdirectory. 
 myapp
└── com
    └── example
        └── myapp
Add Java files to the package: Create MyClass.java in the myapp/com/example/myapp directory.
package com.example.myapp;

public class MyClass {
    public void sayHello() {
        System.out.println("Hello from com.example.myapp.MyClass!");
    }
}
Compile the java files: Open a terminal or command prompt, navigate to the myapp directory, and run the following command:
javac com/example/myapp/MyClass.java
Import and use the package: Create a new Java file Main.java in the myapp directory.
import com.example.myapp.MyClass;

public class Main {
    public static void main(String[] args) {
        MyClass myObject = new MyClass();
        myObject.sayHello();
    }
}

Next Post Previous Post