Common Built-in Classes
Overview of Built-in Classes
Java provides a rich set of built-in classes that developers can use to perform various operations efficiently. These classes are part of the Java Standard Library and cover a wide range of functionalities.
String Manipulation Classes
String Class
The most commonly used built-in class for text manipulation:
import java.lang.String;
public class StringDemo {
public static void main(String[] args) {
String message = "Hello, LabEx!";
System.out.println(message.length());
System.out.println(message.toUpperCase());
}
}
StringBuilder and StringBuffer
Efficient for string concatenation:
import java.lang.StringBuilder;
public class StringBuilderDemo {
public static void main(String[] args) {
StringBuilder builder = new StringBuilder();
builder.append("Java ");
builder.append("Programming");
System.out.println(builder.toString());
}
}
Collection Classes
List Implementations
Common list classes for storing collections:
import java.util.ArrayList;
import java.util.LinkedList;
public class ListDemo {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<>();
LinkedList<Integer> linkedList = new LinkedList<>();
arrayList.add("LabEx");
linkedList.add(42);
}
}
Map Implementations
Key-value pair storage classes:
import java.util.HashMap;
import java.util.TreeMap;
public class MapDemo {
public static void main(String[] args) {
HashMap<String, Integer> hashMap = new HashMap<>();
TreeMap<String, String> treeMap = new TreeMap<>();
hashMap.put("Java", 25);
treeMap.put("Course", "Programming");
}
}
Date and Time Classes
java.time Package
Modern date and time manipulation:
import java.time.LocalDate;
import java.time.LocalDateTime;
public class DateTimeDemo {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
LocalDateTime currentTime = LocalDateTime.now();
System.out.println(today);
System.out.println(currentTime);
}
}
Mathematical Operations
Math Class
Provides advanced mathematical functions:
import java.lang.Math;
public class MathDemo {
public static void main(String[] args) {
double randomNumber = Math.random();
int roundedValue = Math.round(3.7f);
System.out.println(randomNumber);
System.out.println(roundedValue);
}
}
Common Built-in Classes Overview
graph TD
A[Java Built-in Classes] --> B[String Manipulation]
A --> C[Collection Classes]
A --> D[Date and Time]
A --> E[Mathematical Operations]
Comparison of Common Collection Classes
Class |
Type |
Ordered |
Allows Duplicates |
Thread-Safe |
ArrayList |
List |
Yes |
Yes |
No |
LinkedList |
List |
Yes |
Yes |
No |
HashSet |
Set |
No |
No |
No |
TreeSet |
Set |
Yes |
No |
No |
HashMap |
Map |
No |
No |
No |
Best Practices
- Import only the classes you need
- Prefer interface types over concrete implementations
- Use appropriate collection classes based on your requirements
- Leverage built-in methods to write more concise code
By mastering these common built-in classes, you can write more efficient and readable Java code in the LabEx learning environment.