Built-in Packages in Java
Last Updated :
04 Jun, 2025
In Java, Packages are used to avoid naming conflicts and to control the access of classes, interfaces, sub-classes, etc. A package can be defined as a group of similar types of classes, sub-classes, interfaces, or enumerations, etc. Using packages makes it easier to locate or find the related classes and packages provide a good structure or outline of the project with a huge amount of classes and files.
Types of Packages in Java
Packages are divided into two parts, which are listed below:
- Built-in packages: These are the predefined packages provided by the JDK (Java Development Kit). They are present in the .jar files and include packages such as java.lang, java.util, and java.io
- User-defined packages: These packages are created by us to organize our own classes and interfaces in a meaningful way.
In this article, we will discuss Built-in Packages in Java
Java Built-in Packages
Packages that come with JDK or JRD you download are known as built-in packages. The built-in packages have come in the form of JAR files and when we unzip the JAR files we can easily see the packages in JAR files, for example, lang, io, util, SQL, etc. Java provides various built-in packages for example java.awt
Hierarchical Structure of Java Packages
The image below demonstrates the hierarchical structure of Java packages and their related class files.

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: In the above example,
- You 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);
}
}
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: In the above-mentioned example,
- 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.Â
Similar Reads
Java.io Package in Java Java.io Package in JavaThis package provides for system input and output through data streams, serialization and the file system. Unless otherwise noted, passing a null argument to a constructor or method in any class or interface in this package will cause a NullPointerException to be thrown. Follo
1 min read
User-Defined Packages in Java Packages in Java are a mechanism to encapsulate a group of classes, interfaces, and sub-packages. In Java, it is used for making the search/locating and usage of classes, interfaces, enumerations, and annotations easier. It can also be considered data encapsulation. In other words, we can say a pack
3 min read
Java Packages Packages in Java are a mechanism that encapsulates a group of classes, sub-packages, and interfaces. Packages are used for: Prevent naming conflicts by allowing classes with the same name to exist in different packages, like college.staff.cse.Employee and college.staff.ee.Employee.They make it easie
8 min read
Java.lang package in Java Java.lang package in JavaProvides classes that are fundamental to the design of the Java programming language. The most important classes are Object, which is the root of the class hierarchy, and Class, instances of which represent classes at run time. Following are the Important Classes in Java.lan
3 min read
Java.util Package in Java Java.util PackageIt contains the collections framework, legacy collection classes, event model, date and time facilities, internationalization, and miscellaneous utility classes (a string tokenizer, a random-number generator, and a bit array).Following are the Important Classes in Java.util package
4 min read