Understanding Java Packages
Java packages are a fundamental mechanism for organizing classes and interfaces into logical groups. This partitioning of the class namespace prevents naming conflicts and provides a way to control access to members. In essence, packages serve a similar purpose to folders in a file system, helping you manage and categorize your code.
User-Defined Packages
Java packages are organized using the package statement, which is followed by the package name. For example, the following code creates a package named "pkg":
package pkg;
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
public int subtract(int a, int b) {
return a - b;
}
public int multiply(int a, int b) {
return a * b;
}
public double divide(int a, int b) {
if (b == 0) {
throw new IllegalArgumentException("Division by zero is not allowed.");
}
return (double)a / b;
}
}
package shapes;
public class Rectangle {
public int area(int length, int width) {
return length * width;
}
public int perimeter(int length, int width) {
return 2 * (length + width);
}
}
package shapes;
public class Square {
public int area(int side) {
return side * side;
}
public int perimeter(int side) {
return 4 * side;
}
}
package com.example.pkgdemo;
public class PkgDemo {
public int add(int a, int b) {
return a + b;
}
}
Using the Packages
Once your classes are organized into packages, you can import them into other classes using the import statement. The example below demonstrates how to use classes from the pkg and shapes packages.
Note: While importing all classes (e.g., import shapes.*) can be convenient, it is generally considered better practice to import only the specific classes you need. This makes your code more readable and can prevent potential naming conflicts if two different packages contain classes with the same name.
import com.example.pkgdemo.PkgDemo;
import pkg.Calculator;
import shapes.*;
public class PackageTester {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println("Addition of 5 and 3: " + calc.add(5, 3));
System.out.println("Subtraction of 5 and 3: " + calc.subtract(5, 3));
System.out.println("Multiplication of 5 and 3: " + calc.multiply(5, 3));
System.out.println("Division of 5 by 2: " + calc.divide(5, 2));
Rectangle rect = new Rectangle();
System.out.println("Area of Rectangle (5x3): " + rect.area(5, 3));
System.out.println("Perimeter of Rectangle (5x3): " + rect.perimeter(5, 3));
Square square = new Square();
System.out.println("Area of Square (side 4): " + square.area(4));
System.out.println("Perimeter of Square (side 4): " + square.perimeter(4));
PkgDemo pd = new PkgDemo();
System.out.println("Addition using PkgDemo of 10 and 20: " + pd.add(10, 20));
}
}
Predefined Packages
The concept of "predefined packages" refers to the extensive set of classes that are included with the Java Development Kit (JDK) and are available for developers to use right out of the box. These packages contain ready-to-use classes for common programming tasks, which saves developers from having to write everything from scratch.
Note: Classes in the java.lang package (e.g., String, System) are automatically imported, so you do not need to explicitly import them.
In the following file, the lines shown demonstrate the use of predefined packages:
import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;
public class LegacyDateTime {
public static void main(String[] args) {
System.out.println("Using java.util.Date:");
Date currentDate = new Date();
System.out.println("Current Date: " + currentDate);
System.out.println("\nUsing java.util.Calendar:");
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
System.out.println("Formatted Date: " + year + "-" + month + "-" + day);
System.out.println("Formatted Time: " + hour + ":" + minute + ":" + second);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = sdf.format(calendar.getTime());
System.out.println("Formatted Date & Time (via SimpleDateFormat): " + formattedDateTime);
}
}
The benefit of these predefined packages is that they provide a structured, organized way to access a vast library of reusable code, promoting efficiency and consistency in programming.