The Java Standard Library, also known as the Java Standard API (Application Programming Interface), is a collection of pre-written classes and packages that provide a wide range of functionalities for Java developers. These classes and packages are organized into different categories and are an integral part of the Java programming language. In this Core Java tutorial, we'll explore various aspects of the Java Standard Library, highlighting key packages and classes along with examples.
Key Java Standard Library Packages and Classes:
'java.lang'
'Object': The root class for all Java classes. All classes implicitly inherit from 'Object'.
'String': Represents a sequence of characters and provides various string manipulation methods.
'System': Provides access to system-level operations, such as I/O and environment variables.
'Math': Contains mathematical functions and constants.
'StringBuilder' and 'StringBuffer': Mutable versions of 'String' for efficient string manipulation.
Example:
String message = "Hello, ";
message += "world!";
System.out.println(message);
int result = Math.max(10, 20);
System.out.println("Max: " + result);
'java.util'
Collections Framework: Provides classes and interfaces for working with collections, including 'List', 'Set', 'Map', and utility classes like 'Collections'.
Example:
List list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
Set set = new HashSet<>();
set.add(1);
set.add(2);
set.add(3);
Map map = new HashMap<>();
map.put("Alice", 25);
map.put("Bob", 30);
map.put("Eve", 22);
'Date' and 'Calendar': Used for working with dates and times.
Example:
Date now = new Date();
System.out.println("Current date and time: " + now);
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
System.out.println("Current year: " + year);
'java.io'
'File': Provides methods for working with files and directories.
Input/Output Streams: Classes like 'InputStream', 'OutputStream', 'Reader', and 'Writer' for reading and writing data.
Example:
File file = new File("myfile.txt");
boolean exists = file.exists();
System.out.println("File exists: " + exists);
try (FileInputStream inputStream = new FileInputStream(file)) {
int data;
while ((data = inputStream.read()) != -1) {
System.out.print((char) data);
}
} catch (IOException e) {
e.printStackTrace();
}
'java.net'
Classes for networking, including 'Socket', 'ServerSocket', and 'URL'.
Example:
try (Socket socket = new Socket("www.example.com", 80);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
out.println("GET / HTTP/1.1");
out.println("Host: www.example.com");
out.println();
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
'java.util.concurrent'
Provides classes and interfaces for building concurrent and multithreaded applications.
Example:
ExecutorService executor = Executors.newFixedThreadPool(3);
for (int i = 0; i < 5; i++) {
Runnable task = () -> {
System.out.println("Thread executing task.");
};
executor.submit(task);
}
executor.shutdown();
Conclusion:
The Java Standard Library is a vast and versatile collection of packages and classes that simplify many common programming tasks. As a Java developer, mastering the use of these classes and understanding their capabilities is crucial for building efficient and reliable Java applications. This tutorial has provided an overview of some key packages and classes, but there are many more resources and functionalities available within the Java Standard Library to explore and utilize in your Java programming journey.