When learning Java, writing logic is important โ but knowing how to use Javaโs built-in libraries is equally important โ
Java provides a massive collection of pre-written classes and packages called the Java Standard Library. These libraries save time, reduce effort, and make development much easier.
Instead of writing everything from scratch, Java allows developers to use powerful built-in classes for:
- Input & Output ๐ฅ
- Strings manipulation ๐งต
- Mathematical operations ๐ข
- Collections (ArrayList, HashMap, etc.) ๐ฆ
- Date and Time handling โฐ
- Wrapper classes ๐
For beginners, it is important to focus on the most essential and commonly used standard libraries. This article covers exactly that โ nothing overwhelming, just what you truly need to know at the beginning ๐
๐ฆ What Is Java Standard Library?
Java Standard Library is a collection of ready-made classes grouped inside packages like:
java.langjava.utiljava.iojava.timejava.math
Some packages are imported automatically, while others must be imported explicitly.
๐ข 1๏ธโฃ java.lang Package (Automatically Imported)
This is the most fundamental package in Java.
It is imported automatically โ no need to write import java.lang.*;
It includes:
StringMathSystem- Wrapper classes (
Integer,Double, etc.) Object
๐น Example: String Class
public class StringDemo {
public static void main(String[] args) {
String name = "Java Programming";
System.out.println("Length: " + name.length());
System.out.println("Uppercase: " + name.toUpperCase());
System.out.println("Substring: " + name.substring(0, 4));
}
}
๐ง Explanation
length()โ returns number of characterstoUpperCase()โ converts to uppercasesubstring()โ extracts part of string
๐จ Output
Length: 16
Uppercase: JAVA PROGRAMMING
Substring: Java
๐น Example: Math Class
public class MathDemo {
public static void main(String[] args) {
System.out.println("Square Root: " + Math.sqrt(25));
System.out.println("Power: " + Math.pow(2, 3));
System.out.println("Random Number: " + Math.random());
}
}
๐ง Explanation
Math.sqrt()โ square rootMath.pow()โ powerMath.random()โ random value between 0.0 and 1.0
๐ข 2๏ธโฃ java.util Package (Very Important)
This package provides utility classes.
Common beginner classes:
ScannerArrayListHashMapArrays
๐น Example: Scanner (User Input)
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.println("Hello " + name);
sc.close();
}
}
๐ง Explanation
Scanneris used to take inputnextLine()reads full line- Always close scanner
โ Common mistake:
Forgetting sc.close();
๐น Example: ArrayList
import java.util.ArrayList;
public class ArrayListDemo {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Java");
list.add("Python");
list.add("C++");
System.out.println(list);
}
}
๐ง Explanation
- Dynamic array
- No fixed size
add()method inserts element
๐น Example: HashMap
import java.util.HashMap;
public class HashMapDemo {
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<>();
map.put(1, "Ajay");
map.put(2, "Rahul");
System.out.println(map.get(1));
}
}
๐ง Explanation
- Stores key-value pairs
put()insertsget()retrieves
๐ข 3๏ธโฃ java.io Package (Basic File Handling)
Used for file operations.
๐น Example: File Writing
import java.io.FileWriter;
import java.io.IOException;
public class FileDemo {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("sample.txt");
fw.write("Hello Java");
fw.close();
System.out.println("File Written Successfully");
}
}
๐ง Explanation
FileWriterwrites to fileclose()is mandatory- IOException must be handled
๐ข 4๏ธโฃ Wrapper Classes
Used to convert primitive types into objects.
Example:
intโIntegerdoubleโDouble
๐น Example: Wrapper Class
public class WrapperDemo {
public static void main(String[] args) {
int num = 10;
Integer obj = Integer.valueOf(num);
System.out.println(obj);
}
}
๐ง Why Important?
Collections work with objects, not primitives.
๐ข 5๏ธโฃ java.time Package (Date and Time)
Modern way to handle dates.
๐น Example
import java.time.LocalDate;
import java.time.LocalTime;
public class DateTimeDemo {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
LocalTime time = LocalTime.now();
System.out.println("Date: " + date);
System.out.println("Time: " + time);
}
}
โ Common Confusions
โ Why don’t we import java.lang?
Because it’s automatically imported.
โ Why use ArrayList instead of array?
ArrayList is dynamic.
โ Why use Scanner instead of System.in directly?
Scanner provides convenient methods.
โ Common Mistakes
โ Forgetting import statement
ArrayList list = new ArrayList(); // โ Error without import
โ Forgetting to close Scanner or FileWriter
Can cause memory leaks.
โ Using == for String comparison
if(str1 == str2) // โ wrong
Correct:
if(str1.equals(str2)) // โ
๐ Essential Libraries Every Beginner Must Know
| Package | Important Classes |
|---|---|
| java.lang | String, Math, System |
| java.util | Scanner, ArrayList, HashMap |
| java.io | FileWriter |
| java.time | LocalDate, LocalTime |
| Wrapper Classes | Integer, Double |
๐ฏ Why Standard Libraries Matter
Using standard libraries:
- Saves development time โณ
- Improves reliability โ
- Reduces bugs ๐
- Makes code professional ๐ผ
โ Conclusion
Java Standard Libraries are the backbone of practical Java programming ๐
For beginners, mastering:
StringoperationsScannerinputArrayListandHashMapMathutilities- Basic file handling
- Date and time APIs
is essential for building real-world applications ๐ช
Once these core libraries are comfortable, learning advanced frameworks and APIs becomes much easier.