Java provides a rich set of pre-defined packages that contain classes and interfaces to handle various tasks such as input/output, networking, utilities, data structures, and more. These packages are grouped into libraries to promote code reusability and modular programming.
Understanding Pre-Defined Packages
A package in Java is a collection of classes, interfaces, and sub-packages that are logically grouped together. The java.lang
package is automatically imported in every Java program, but other packages must be explicitly imported using the import
statement.
Commonly Used Pre-Defined Packages:
- java.lang – Provides core classes such as
String
,Math
,Integer
,Double
, etc. It is automatically imported. - java.util – Contains utility classes like collections framework, date and time facilities, etc.
- java.io – Handles input and output operations such as reading and writing files.
- java.net – Provides classes for networking, such as
Socket
,URL
,InetAddress
. - java.awt – Abstract Window Toolkit, contains classes for GUI components.
- javax.swing – Provides lightweight GUI components such as buttons, labels, and frames.
- java.sql – Contains classes for JDBC, allowing database access and operations.
- java.time – Includes classes for date and time operations.
Syntax for Importing Packages
import java.util.*; // Imports all classes in java.util
import java.io.File; // Imports only the File class
Code language: JavaScript (javascript)
You can also use the fully qualified name to access a class without importing it:
java.util.Scanner scanner = new java.util.Scanner(System.in);
Code language: JavaScript (javascript)
Example Program – Demonstrating Pre-Defined Packages
In this example, we will use classes from different packages to demonstrate how to use pre-defined packages in Java.
Problem Statement: LotusJavaPrince is creating a program to manage data input and output, perform mathematical calculations, and format dates. He wants to use predefined packages to implement these functionalities effectively.
import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; import java.text.SimpleDateFormat; import java.util.Date; import java.lang.Math; public class PackageDemo { public static void main(String[] args) { // Using java.util.Scanner for input Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); double number = scanner.nextDouble(); // Using java.lang.Math for calculations double squareRoot = Math.sqrt(number); System.out.println("Square root of " + number + " is: " + squareRoot); // Using java.util.Date and java.text.SimpleDateFormat for date formatting Date currentDate = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); String formattedDate = formatter.format(currentDate); System.out.println("Current date and time: " + formattedDate); // Using java.io.File for file handling try { File file = new File("sample.txt"); Scanner fileReader = new Scanner(file); System.out.println("File contents:"); while (fileReader.hasNextLine()) { System.out.println(fileReader.nextLine()); } fileReader.close(); } catch (FileNotFoundException e) { System.out.println("File not found: " + e.getMessage()); } scanner.close(); } }
- Packages help in organizing classes and interfaces logically.
- Importing specific classes reduces memory usage compared to importing entire packages.
- The
java.lang
package is automatically imported, while other packages must be imported explicitly.
Thus, pre-defined packages in Java provide a vast library of reusable classes and interfaces, promoting modular programming and efficient code organization.