Built-in Packages in Java


Java comes with a large number of built-in packages that provide a wide range of functionality. Some of the most commonly used built-in packages in Java are:

  • java.lang: This package contains the fundamental classes and interfaces that are used throughout the Java API. It includes classes such as String, Object, and Math.

  • java.util: This package contains a collection of utility classes and interfaces that provide useful functionality for working with collections, dates, and other common tasks. It includes classes such as ArrayList, HashMap, and Date.

  • java.io: This package provides classes for working with input and output streams, files, and other I/O operations. It includes classes such as FileInputStream, FileOutputStream, and BufferedReader.

  • java.net: This package provides classes for working with network connections and protocols. It includes classes such as URL, HttpURLConnection, and Socket.

  • java.awt: This package provides classes for creating graphical user interfaces (GUIs). It includes classes such as Frame, Button, and TextField.

  • java.swing: This package provides a more advanced set of GUI components than the AWT package. It includes classes such as JFrame, JButton, and JTextField.

  • java.math: This package provides classes for working with mathematical operations and functions. It includes classes such as BigInteger and BigDecimal.

  • java.security: This package provides classes for implementing security features in Java applications. It includes classes such as MessageDigest, KeyPairGenerator, and Cipher.

Example:

import java.util.ArrayList;

public class Example {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<String>();
        list.add("Hello");
        list.add("World");
        System.out.println(list);
    }
}

In this example, we are using the java.util package to create an ArrayList and add two strings to it. We then print out the contents of the list using the System.out.println method.



About the author

William Pham is the Admin and primary author of Howto-Code.com. With over 10 years of experience in programming. William Pham is fluent in several programming languages, including Python, PHP, JavaScript, Java, C++.