Lesson 18 of 36Article15 min
Packages & API
Package groups related classes: com.rishtaara.models. First line of file: package com.rishtaara.models;
Packages — organizing classes
Package groups related classes: com.rishtaara.models. First line of file: package com.rishtaara.models;
import brings classes from other packages: import java.util.ArrayList;
Real-life example: Packages are folders in a filing cabinet — billing, HR, inventory — each drawer holds related files.
Package and import
package com.knowvora.demo;
import java.util.ArrayList;
import java.util.List;
public class App {
public static void main(String[] args) {
List<String> items = new ArrayList<>();
items.add("Java");
}
}API — using built-in libraries
Java API is the huge library of ready classes — java.lang, java.util, java.io, java.time.
You do not rewrite ArrayList or Scanner — you import and use them.
Real-life example: API is like a toolbox in a workshop — hammer, screwdriver ready. You pick the tool, not forge metal from scratch.
- java.lang — String, Math, System (auto-imported)
- java.util — collections, Scanner, Random
- java.io — files and streams
- java.time — modern date/time