Open In App

Built-in Packages in Java

Last Updated : 30 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, Built-in packages are predefined libraries provided by the Java Development Kit (JDK) that contain a wide range of classes and interfaces to perform common tasks such as input/output operations, data structures, networking, database connectivity and more. These packages help developers avoid writing code from scratch by offering ready-to-use functionalities and are organized under the java and javax namespaces.

Hierarchical Structure of Java Packages

The image below demonstrates the hierarchical structure of Java packages and their related class files.

Java Built-In Packages

Examples of Built-in Packages

  • java.sql: Provides the classes for accessing and processing data stored in a database. Classes like Connection, DriverManager, PreparedStatement, ResultSet, Statement, etc. are part of this package.
  • java.lang: Contains classes and interfaces that are fundamental to the design of the Java programming language. Classes like String, StringBuffer, System, Math, Integer, etc. are part of this package.
  • java.util: Contains the collections framework, some internationalization support classes, properties, random number generation classes. Classes like ArrayList, LinkedList, HashMap, Calendar, Date, Time Zone, etc. are part of this package.
  • java.net: Provides classes for implementing networking applications. Classes like Authenticator, HTTP Cookie, Socket, URL, URLConnection, URLEncoder, URLDecoder, etc. are part of this package.
  • java.io: Provides classes for system input/output operations. Classes like BufferedReader, BufferedWriter, File, InputStream, OutputStream, PrintStream, Serializable, etc. are part of this package.
  • java.awt: Contains classes for creating user interfaces and for painting graphics and images. Classes like Button, Color, Event, Font, Graphics, Image, etc. are part of this package.

Now, we are going to discuss each one with particula example in detail.

1. java.sql Package

This package provides APIs for database operations using JDBC (Java Database Connectivity).

Example:

Java
import java.sql.*;

public class Geeks {
   
   static final String URL = "jdbc:mysql://localhost/Geeksforgeeks";
   static final String USER = "user";
   static final String PASSWORD = "123";
   static final String QUERY = "SELECT ID, NAME, ADDRESS FROM STUDENTS";
  
   public static void main(String[] args) {
      // Open a connection
      try(
         Connection con = DriverManager.getConnection(URL, USER, PASSWORD);
         Statement stm = con.createStatement();
         ResultSet rs = stm.executeQuery(QUERY);) {
         
        while (rs.next()) {
            // Retrieving data from column name
            System.out.print("ID: " + rs.getInt("id"));
            System.out.print(",NAME: " + rs.getString("name"));
            System.out.println(",ADDRESS: " + rs.getString("address"));
         }
      } catch (SQLException e) {
         e.printStackTrace();
      } 
   }
}

Output

ID: 1, NAME: Harry, ADDRESS: India
ID: 2, NAME: Jhon, ADDRESS: USA
ID: 3, NAME: Kim, ADDRESS: USA
ID: 4, NAME: Bob, ADDRESS: India

Explanation:

  • We can see that first, we imported the package name java.sql.*,
  • In the main method, we initialize the string variable name URL, USER, PASSWORD, QUERY.
  • And then use DriverManager to get a connection to the database and finally, we use the executeQuery method to execute the query of selecting the data from the database. 

2. java.lang Package

This package contains the core classes that are fundamental to the Java language. This package is automatically imported to each program, which means we can directly use classes of this package in our program.

Let's have an example of the Math class of lang package which provides various functions or methods for mathematical operation.

Example:

Java
public class Geeks {
   public static void main(String[] args) {
      int a = 100, b = 200;
      int maxi = Math.max(a, b);
      System.out.printf("Maximum is = %d%n", maxi); 
   }
}

Output
Maximum is = 200

Explanation:

In the above-mentioned example,

  • We use the lang package and Math class to find the maximum of two-digit using the "max" function.
  • First, we created a three integer variable named "a" and "b" and maxi. where a = 100 and b = 200 and maxi will store the maximum value from a or b.
  • After assigning values to the variable we used the max function to find the maximum and stored it in maxi.
  • So, in desired output, we can see a maximum of two numbers and that is 200.  

3. java.util Package

This package provides utility classes such as collections, date/time and array manipulation tools. The most common class in this package is Arrays which is generally used by programmers in various cases. We can say that it is the most useful package for java because it helps to achieve different types of requirements easily by using pre-written classes.

Let's see the example of Arrays class.

Example:

Java
import java.util.Arrays;

public class Geeks {
   public static void main(String[] args) {
      int[] arr = { 40, 30, 20, 70, 80 };
      Arrays.sort(arr);
      System.out.println("Sorted Array is = " + Arrays.toString(arr));
   }
}

Output:

Sorted Array is = [20, 30, 40, 70, 80]

Explanation:

  • We used java.util package and Arrays class to sort the integer array.
  • First, we created an integer type array that consists of elements 40,30,20,70,80 and sort the array using the "sort" function of the Arrays class.
  • After sorting an array it prints the sorted array. 

4. java.io Package

This package provides classes and an interface for handling the system(input/output). Using these classes programmer can take the input from the user and do operations on that and then display the output to the user. Other than this we can also perform file handling read or write using classes of this package.

Example:

Java
import java.io.Console;

public class Geeks {
   public static void main(String[] args) {
      Console cons = System.console();
      if (cons != null) {
         System.out.println("Enter your favorite color:");
         String colour = cons.readLine();
         System.out.println("Favorite colour is " + colour);
      } else {
         System.out.println("Console not available.");
      }
   }
}

Output

Enter your favorite color
RED
Favorite color is RED

Explanation: In the above-mentioned example,

  • We use the java.io package and Console class to take input from users to perform some task or operations on it and then finally display the result on the console.
  • As code said first we display the message "Enter your favorite color" which asks a user to enter color.
  • Then user system read that line using cons.readLine() and stored it in string type variable then finally display that line.

5. Java.net Package

This package contains the classes and interfaces for networking like Socket class, ServerSocket class, URLConnection class, DatagramSocket, MulticastSocket, etc. Java supports the two network protocols such as TCP (Transmission control protocol) and UDP (User Datagram Protocol which is a connection-less protocol).

Example:

Java
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Geeks {
   public static void main(String[] args) throws IOException {
      int port_no = 4567;
      DatagramSocket ds = new DatagramSocket(port_no);
      byte[] receive = new byte[65535];

      DatagramPacket DpReceive;
      while (true) {
         DpReceive = new DatagramPacket(receive, receive.length);
         ds.receive(DpReceive);
         System.out.println("DATA:- " + data(receive)); 
         receive = new byte[65535];
      }
   }
   public static String data(byte[] a) {
      if (a == null) return null;
      StringBuilder ret = new StringBuilder();
      int i = 0;
      while (a[i] != 0) {
         ret.append((char) a[i]);
         i++;
      }
      return ret.toString();
   }
}

Output:

DATA:- //whatever to get from server 

Explanation:

  • Here we have implemented a UDP socket receiver that receives data from the server-side through port no.
  • First, we created a socket using the DatagramSocket method to listen on port no 4567, second, we created a datagram packet to receive the data in byte buffer and then finally after getting the data we printed it.

6. Java.awt Package

This package is the main package of the abstract window kit. it includes the classes for graphics, containing the java 2D graphics and it also defines the graphical user interface (GUI) framework for java. this package had several heavy GUI objects which come under java.swing package.

Example:

Java
import java.awt.*;

public class Geeks {
   AWTExample() {
      Frame fr1 = new Frame();
      Label la = new Label("Welcome to Java Graphics - GEEKSFORGEEKS");
      fr1.add(la);
      fr1.setSize(300, 200);  
      fr1.setVisible(true);
   }

   public static void main(String[] args) {
      new AWTExample(); 
   }
}

Output:

Explanation: Here, we created a frame of size 200 x 200 by using an awt package and then print a message "Welcome to the Java graphics GEEKSFORGEEKS. 


Article Tags :
Practice Tags :

Similar Reads