Overview:
In Lesson 1, we introduce the Java Standard Library, also known as the Java API (Application Programming Interface). The Java API provides a rich set of classes and packages that form the foundation for Java development. This lesson focuses on exploring commonly used packages, such as java.lang
and java.util
, and understanding their significance in Java programming.
Key Concepts:
- Java API (Application Programming Interface):
- Definition: The Java API is a collection of classes and packages provided by Java for software development.
- Purpose: It offers a standardized way for Java developers to interact with the core functionality of the Java platform.
- java.lang Package:
- Description: The
java.lang
package is automatically imported into every Java program. It contains fundamental classes that are essential for basic Java programming. - Key Classes:
Object
: The root class for all Java classes.String
: Represents a sequence of characters.System
: Provides access to the standard input, output, and error streams.
- Description: The
- java.util Package:
- Description: The
java.util
package contains utility classes and data structures that are commonly used in Java programs. - Key Classes:
ArrayList
: A dynamic array that can grow or shrink in size.HashMap
: An implementation of a hash table for key-value pairs.Scanner
: Used for reading input from various sources.
- Description: The
Example:
Let’s explore the java.lang
and java.util
packages through a simple program. We’ll use the String
class from java.lang
and the ArrayList
class from java.util
.
import java.util.ArrayList;
public class JavaAPIDemo {
public static void main(String[] args) {
// Example from java.lang package
String greeting = "Hello, Java!";
System.out.println(greeting);
// Example from java.util package
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
System.out.println("Fruits: " + fruits);
}
}
In this example, we use the String
class from java.lang
to work with a text string and the ArrayList
class from java.util
to create and manipulate a dynamic list of fruits. This demonstrates how fundamental classes from these packages are used in a typical Java program.