Lesson 3: What Does “Import” Mean in Java?

When you start learning Java programming, you’ll often come across the phrase “import libraries.” But what does it actually mean? Let’s break it down in simple terms.

What Is a Library?

A library is like a toolbox filled with pre-written solutions: classes, methods, and functions created by other developers. These tools make your programming life easier by providing reliable and time-tested functionalities. Whether you need to work with text, numbers, dates, or files, Java libraries have you covered, so you don’t have to write everything from scratch.

Importing Libraries

Java organizes these tools into packages, which are collections of related classes. To use a specific tool, you “import” the package it belongs to. Think of it as telling Java, “I want to use this particular tool from the toolbox.”

Example 1: Working with User Input

Let’s say you want to use the Scanner class to read user input. The Scanner class is part of the java.util package, so you’ll need to import it.

import java.util.Scanner; // Import the Scanner class

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // Create a Scanner object (it's essentially a variable, but more "powerful"we'll explore this in a future lesson)
System.out.println("What is your name?");
String name = scanner.nextLine(); // Read a line of text input
System.out.println("Hello, " + name + "!");
}
}

Why Not Import Everything Automatically?

Java automatically loads a few essential classes from the java.lang package, like String, Math, and System. These are used so often that it makes sense to include them by default. However, loading everything else would take up unnecessary memory and potentially slow down your program. That’s why you need to manually import other packages.

Example 2: Working with Dates

To work with dates, Java provides the LocalDate class in the java.time package. Here’s how you import and use it:

import java.time.LocalDate; // Import the LocalDate class

public class Main {
public static void main(String[] args) {
LocalDate today = LocalDate.now(); // Get the current date
System.out.println("Today's date is: " + today);
}
}

Commonly Used Libraries in Java

Here are a few popular Java libraries and their uses:

  1. java.util — For collections (like lists and maps), random numbers, dates, and scanners.
  2. java.io — For reading and writing data.
  3. java.time — For working with dates and times.

Why Is Importing Libraries Useful?

When you import a library, you’re saying, “Hey Java, I need access to these ready-made tools.” For example, instead of creating a complex date-handling system from scratch, you can rely on the LocalDate class, which is already built and optimized.

Example with Multiple Libraries

Here’s a program that uses both Scanner and LocalDate:

import java.util.Scanner;
import java.time.LocalDate;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your birth year:");
int birthYear = scanner.nextInt();

LocalDate today = LocalDate.now();
int currentYear = today.getYear();

int age = currentYear - birthYear;
System.out.println("You are " + age + " years old.");
}
}

Wrapping It Up

Importing libraries in Java is your gateway to faster and more efficient programming. Instead of reinventing the wheel, you can focus on building your program using powerful, pre-existing tools created by experts. Don’t hesitate to explore, experiment, and learn what these libraries can do—your code will thank you!

Leave a Reply