📖 What is a Package?
A package is a namespace that organizes related classes and interfaces. It prevents naming conflicts, provides access control, and makes code easier to maintain.
java.lang - Language Package (Automatically Imported):
📖 java.lang:
Automatically imported into every Java program. Contains core classes: String, Math, System, Integer, Thread, Object, Exception.
Key Point: No need to write import java.lang.* - it's automatic!
// java.lang is auto-imported - no import needed!
// String class
String name = "Ankush";
int length = name.length();
// Math class
double result = Math.sqrt(25); // 5.0
int maximum = Math.max(10, 20); // 20
// System class
System.out.println("Hello World");
// Wrapper classes
Integer num = Integer.parseInt("123");
Double decimal = Double.parseDouble("45.67");
// Object class (parent of all classes)
Object obj = new String("test");
Output:
Hello World
java.util - Utility Package:
📖 java.util:
Contains utility classes for collections, date/time, random numbers. Must be imported before use.
Common classes: ArrayList, HashMap, Scanner, Date, Random.
import java.util.*;
// ArrayList - dynamic array
ArrayList fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
System.out.println(fruits); // [Apple, Banana, Orange]
// HashMap - key-value pairs
HashMap marks = new HashMap<>();
marks.put("Raj", 85);
marks.put("Priya", 92);
System.out.println(marks.get("Raj")); // 85
// Scanner - user input
Scanner sc = new Scanner(System.in);
System.out.print("Enter name: ");
String name = sc.nextLine();
// Date
Date today = new Date();
System.out.println(today);
// Random
Random rand = new Random();
int randomNum = rand.nextInt(100); // 0 to 99
java.io - Input/Output Package:
📖 java.io:
Provides classes for input/output through data streams and file handling.
Common classes: File, FileReader, FileWriter, BufferedReader, PrintWriter.
import java.io.*;
// Reading from file
try {
FileReader fr = new FileReader("data.txt");
BufferedReader br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
// Writing to file
try {
FileWriter fw = new FileWriter("output.txt");
PrintWriter pw = new PrintWriter(fw);
pw.println("Hello File!");
pw.println("Java is awesome");
pw.close();
System.out.println("File written successfully");
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
java.awt - Abstract Window Toolkit:
📖 java.awt:
Contains classes for creating GUI and handling graphics.
Common classes: Frame, Button, Label, TextField, Panel, Color, Font.
import java.awt.*;
// Creating a simple window
Frame f = new Frame("My First Window");
Button b = new Button("Click Me");
Label l = new Label("Welcome!");
f.add(b);
f.add(l);
f.setSize(400, 300);
f.setLayout(new FlowLayout());
f.setVisible(true);
// Output: A window appears with a button and label
java.math - Mathematics Package:
📖 java.math:
Provides classes for arbitrary-precision arithmetic beyond primitive type ranges.
Common classes: BigInteger (large integers), BigDecimal (precise decimals).
💡 Why BigInteger/BigDecimal?
• long max: 9,223,372,036,854,775,807
• BigInteger max: Unlimited! Can store numbers with millions of digits
• BigDecimal: Precise money calculations (no rounding errors)
import java.math.*;
// BigInteger - very large numbers
BigInteger big1 = new BigInteger("123456789012345678901234567890");
BigInteger big2 = new BigInteger("987654321098765432109876543210");
BigInteger sum = big1.add(big2);
BigInteger product = big1.multiply(big2);
System.out.println("Sum: " + sum);
System.out.println("Product: " + product);
// BigDecimal - precise decimal math
BigDecimal price1 = new BigDecimal("19.99");
BigDecimal price2 = new BigDecimal("5.01");
BigDecimal total = price1.add(price2);
System.out.println("Total: $" + total); // $25.00 (exact!)
java.sql - SQL Package:
📖 java.sql:
Provides classes and interfaces for database access via JDBC.
Common interfaces: Connection, Statement, ResultSet, DriverManager.
import java.sql.*;
// Database connection and query
try {
// 1. Load driver (automatic in modern Java)
// 2. Establish connection
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/college",
"root",
"password"
);
// 3. Create statement
Statement stmt = con.createStatement();
// 4. Execute query
ResultSet rs = stmt.executeQuery("SELECT * FROM students");
// 5. Process results
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
int marks = rs.getInt("marks");
System.out.println(id + " | " + name + " | " + marks);
}
// 6. Close connection
con.close();
} catch (SQLException e) {
System.out.println("Database Error: " + e.getMessage());
}
Output:
1 | Raj | 85
2 | Priya | 92
3 | Amit | 78
💡 Package Summary:
• java.lang - Core (String, Math, System) - AUTO IMPORTED
• java.util - Utilities (ArrayList, HashMap, Scanner)
• java.io - File I/O (FileReader, FileWriter)
• java.awt - GUI (Frame, Button, Label)
• java.math - Big Numbers (BigInteger, BigDecimal)
• java.sql - Database (Connection, Statement, ResultSet)