JAVA

Java Packages


What is package?

  • In java, a package organize number of classes, interfaces and sub-package of same type into a particular group.
  • A package is essentially a directory that contains a collection of Java classes and interfaces, along with sub-packages if needed.

Types:

These are the packages that are part of the Java Standard Edition (SE) and are automatically available for use.

It already developed by java people and integrated with JDK  for use.

Some commonly used built-in packages include:

  • java.lang:  It is a default package which provides fundamental classes that are automatically imported into every Java program. 

    Examples:  Include Object, String, and basic data types like int, float etc…

  • java.util: Contains utility classes and data structures like ArrayList, HashMap, and Date.
  • java.io: Deals with input and output operations, including classes for file handling.
  • java.net: Provides classes for networking operations.
  • java.awt and javax.swing: Packages for creating graphical user interfaces (GUIs).

 

Example: Let's create a simple example using a built-in package. We'll use the java.util package, specifically the ArrayList class, to create a basic program.

import java.util.ArrayList;

public class BuiltInPackageExample {
    public static void main(String[] args) {
        // Create an ArrayList of strings
        ArrayList<String> fruits = new ArrayList<>();

        // Add elements to the ArrayList
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Orange");

        // Display the elements in the ArrayList
        System.out.println("Fruits in the ArrayList:");
        for (String fruit : fruits) {
            System.out.println(fruit);
        }
    }
}

 

Note:

1.  We import the ArrayList class from the java.util package.
2.  We create an ArrayList named fruits to store strings.
3.  We add three fruits to the ArrayList.
4.  Finally, we use a loop to iterate through the ArrayList and print the fruits.

 

To compile and run this code:

  • Save the code in a file named BuiltInPackageExample.java.
  • Open a terminal or command prompt.
  • Navigate to the directory containing BuiltInPackageExample.java.
  • Compile the code using the following command:
javac BuiltInPackageExample.java
  • Run the application:
java BuiltInPackageExample

 

Output:

Fruits in the ArrayList:
Apple
Banana
Orange

These are packages created by Java developers (like- us) to organize their own classes and interfaces.

User-defined packages help manage and structure code within a project. 

A user-defined package can also contain subpackages.

To create a user-defined package, you use the package statement at the beginning of your Java source file.

Syntax:

package com.example.myproject;

class MyClass {
    // class members and methods
}

 

Example: Let's create a simple example of a user-defined package with a couple of classes. We'll create a package named com.example.myproject with two classes: Calculator and App

Calculator.java (inside the directory com/example/myproject):

package com.example.myproject;

public class Calculator {
    public static int add(int a, int b) {
        return a + b;
    }

    public static int subtract(int a, int b) {
        return a - b;
    }
}

App.java (outside the com/example/myproject directory):

import com.example.myproject.Calculator;

public class App {
    public static void main(String[] args) {
        int resultAdd = Calculator.add(5, 3);
        int resultSubtract = Calculator.subtract(10, 4);

        System.out.println("Addition Result: " + resultAdd);
        System.out.println("Subtraction Result: " + resultSubtract);
    }
}

 

To compile and run this code:

1.  Save the Calculator.java file inside the directory com/example/myproject.

2.  Save the App.java file in the same directory where you saved com.

3.  Open a terminal or command prompt.

4.  Navigate to the directory containing com and App.java.

5.  Compile the code using the following commands:

javac com/example/myproject/Calculator.java
javac App.java

6.  Run the application:

java App

 

Output:

Addition Result: 8
Subtraction Result: 6