Packages & API π¶
Mentor's Note: A package is like a Folder on your computer. If you have 1000 files in one big folder, you can't find anything! Packages allow us to group "Like with Like." It keeps your code organized and prevents name collisions. π‘
π The Scenario: The Personal Library π¶
Imagine you have a huge collection of books.
- The Packages (The Sections): You don't just pile them in the living room. You create sections:
science.physics: Books on space and atoms.history.india: Books on the Indian freedom struggle.coding.java: Books on Java syntax.
- The API (The Public Library): Some books you write yourself (Custom Packages), but most technical books you "Borrow" from the Public Library (Java API).
- The Import (Bringing it Home): When you need a book on "Scanner," you Import it into your home study. β
π¨ Visual Logic: The Folder Structure¶
graph TD
subgraph com.vishnudigital.docs ["Project Root π"]
A[oops]
B[foundations]
C[practice]
end
A --> D[Inheritance.class]
A --> E[Polymorphism.class]
B --> F[Variables.class]
π Concept Explanation¶
1. What is a Package?¶
A package in Java is used to group related classes. It is essentially a folder in your project directory.
2. Built-in Packages (Java API)¶
Java comes with thousands of pre-written classes (the API) that are free to use.
- Example: java.util (Utility classes like Scanner, ArrayList).
- Example: java.lang (Core classes like String, Math - Automatically imported!).
3. The import Keyword π₯¶
To use a class from the API or another package, you must "Tell Java where to find it" using the import keyword.
π» Implementation: The Library Lab¶
// π Scenario: Getting user input
// π Action: Importing a class from the built-in utility package
import java.util.Scanner; // Import specific class
public class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
System.out.println("Enter your name: ");
String userName = myObj.nextLine();
System.out.println("Hello, " + userName + "! π");
}
}
π Sample Dry Run (Logic)¶
| Step | Instruction | Computer's Logic | Result |
|---|---|---|---|
| 1 | import java.util.Scanner |
Locate the util folder in the Java SDK |
Code is "Linked" π |
| 2 | Scanner myObj |
Use the linked blueprint | Space for a scanner ποΈ |
| 3 | package mypack |
Ensure this file is saved in src/mypack/ |
Organization complete β |
π Technical Analysis¶
- Why use Packages?:
- Avoid Collisions: You can have two classes named
Userif one is inadminand one is incustomer. - Maintainability: Large projects are much easier to navigate.
- Avoid Collisions: You can have two classes named
- Naming Convention: In professional coding, we use reversed domains:
com.google.searchorcom.vishnudigital.docs. π
π― Practice Lab π§ͺ¶
Task: The Math Master
Task: The java.lang.Math package is automatically imported. Use Math.max(5, 10) to find the bigger number and print it.
Goal: Understand that core packages don't need the import keyword! π‘
π‘ Interview Tip π¶
"Interviewers love asking: 'What is the difference between
import java.util.Scannerandimport java.util.*?' Answer: The first imports only the Scanner. The second imports every class in theutilpackage. Be carefulβimporting everything can make your code slightly harder to read for others!"
π‘ Pro Tip: "A package is your project's geography. Map it out clearly before you start building!" - Vishnu Damwala
β Back: Abstraction & Interface | Next: Inner Classes, Anonymous & Enum β