Understanding Java Built-in Packages
When working with Java, one of the biggest advantages is the rich set of built-in packages that make development smoother. These packages provide essential functionalities like data structures, input/output handling, utility functions, and more. Instead of reinventing the wheel, Java developers can leverage these to write cleaner and more efficient code.
Get Kosha Dangarwala Gandhi, PMP®’s stories in your inbox
Join Medium for free to get updates from this writer.
Let’s dive into some of the most commonly used built-in packages and understand how they simplify our work.
1. java.lang (Core Java Package)
This is the most fundamental package, and it’s automatically imported in every Java program. It contains essential classes like String, Math, Object, System, and Thread.
Example: Using Math and String classes
public class JavaLangExample {
public static void main(String[] args) {
// Math operations
double squareRoot = Math.sqrt(25);
System.out.println("Square Root of 25: " + squareRoot); // String manipulations
String message = "Java is powerful!";
System.out.println("Uppercase: " + message.toUpperCase());
}
}2. java.util (Utility Package)
The java.util package provides collection classes like ArrayList, HashMap, Date, and other utilities that simplify data handling.
Example: Using ArrayList and HashMap
import java.util.ArrayList;
import java.util.HashMap;public class JavaUtilExample {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Java");
list.add("Python");
System.out.println("ArrayList: " + list); HashMap<Integer, String> map = new HashMap<>();
map.put(1, "One");
map.put(2, "Two");
System.out.println("HashMap: " + map);
}
}
3. java.io (Input/Output Package)
Handling files and streams is made easy with the java.io package. This package provides classes like FileReader, BufferedReader, and PrintWriter.
Example: Reading a File
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;public class JavaIOExample {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader("example.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}4. java.text (Text Formatting Package)
This package is useful for formatting dates, numbers, and messages. It includes classes like SimpleDateFormat, DecimalFormat, and MessageFormat.
Example: Formatting Dates and Numbers
import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.DecimalFormat;public class JavaTextExample {
public static void main(String[] args) {
// Date formatting
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
String formattedDate = sdf.format(new Date());
System.out.println("Formatted Date: " + formattedDate); // Number formatting
DecimalFormat df = new DecimalFormat("#,###.00");
System.out.println("Formatted Number: " + df.format(12345.6789));
}
}
5. java.util.function (Functional Interfaces Package)
Introduced in Java 8, this package provides built-in functional interfaces like Predicate, Function, Consumer, and Supplier. These are useful in lambda expressions and stream processing.
Example: Using Predicate and Function
import java.util.function.Predicate;
import java.util.function.Function;public class JavaUtilFunctionExample {
public static void main(String[] args) {
// Predicate: Checks if a number is even
Predicate<Integer> isEven = num -> num % 2 == 0;
System.out.println("Is 10 even? " + isEven.test(10)); // Function: Converts string to uppercase
Function<String, String> toUpperCase = str -> str.toUpperCase();
System.out.println("Uppercase: " + toUpperCase.apply("hello"));
}
}
6. java.util.stream (Stream API Package)
Another powerful feature from Java 8, the java.util.stream package allows developers to process collections efficiently using functional programming concepts.
Example: Using Streams to Filter and Transform Data
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;public class JavaStreamExample {
public static void main(String[] args) {
List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David"); // Filtering and mapping using Stream
List<String> filteredNames = names.stream()
.filter(name -> name.startsWith("A"))
.map(String::toUpperCase)
.collect(Collectors.toList()); System.out.println("Filtered Names: " + filteredNames);
}
}
Conclusion
Java’s built-in packages provide a powerful set of tools that make development faster and more efficient. Whether you need to manipulate text, handle collections, work with files, or leverage modern functional programming, there’s a package ready for the job.
By mastering these packages, you’ll write cleaner, more maintainable Java code while leveraging the full potential of the Java ecosystem. Happy coding!








