Java API
In Java, API stands for Application Programming Interface.
It’s basically a collection of pre-written classes, interfaces, and packages provided by
Java so you can use ready-made functionality instead of writing everything from scratch.
What is the Java API?
● The Java API is part of the Java Development Kit (JDK).
● It contains thousands of classes organized into packages.
● Each class contains methods and fields that help you perform common
programming tasks.
● Example: Reading files, working with dates, creating GUI apps, connecting to
databases, etc.
Structure of the Java API
Java API is organized into packages.
A package is like a folder containing related classes and interfaces.
Example packages:
Package Purpose
[Link] Core classes (Strings, Math, Object, Wrappers) — imported automatically.
[Link] Utility classes (Collections, Dates, Random, Scanner).
[Link] Input/Output classes (File handling, Streams).
[Link] Networking classes (Sockets, URLs).
[Link] Database connectivity (JDBC).
[Link] GUI components (buttons, frames).
Benefits of Java API
Reusability → No need to reinvent common functionalities.
Reliability → Well-tested by Oracle and the community.
Time-saving → Write less code, achieve more.
Consistency → Standardized method names and behavior.
Example
import [Link]; // Import from [Link] package
public class Example {
public static void main(String[] args) {
Random rand = new Random(); // Create Random object
int number = [Link](100); // Get a random number (0–99)
[Link]("Random number: " + number);
}
}
Here:
[Link] is a class from the Java API.
We didn't write the logic for generating random numbers — Java API gave it to us.