๐Ÿš€ Java Standard Libraries โ€” Core Classes Every Java Developer Must Know

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.lang
  • java.util
  • java.io
  • java.time
  • java.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:

  • String
  • Math
  • System
  • 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 characters
  • toUpperCase() โ†’ converts to uppercase
  • substring() โ†’ 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 root
  • Math.pow() โ†’ power
  • Math.random() โ†’ random value between 0.0 and 1.0

๐ŸŸข 2๏ธโƒฃ java.util Package (Very Important)

This package provides utility classes.

Common beginner classes:

  • Scanner
  • ArrayList
  • HashMap
  • Arrays

๐Ÿ”น 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

  • Scanner is used to take input
  • nextLine() 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() inserts
  • get() 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

  • FileWriter writes to file
  • close() is mandatory
  • IOException must be handled

๐ŸŸข 4๏ธโƒฃ Wrapper Classes

Used to convert primitive types into objects.

Example:

  • int โ†’ Integer
  • double โ†’ 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

PackageImportant Classes
java.langString, Math, System
java.utilScanner, ArrayList, HashMap
java.ioFileWriter
java.timeLocalDate, LocalTime
Wrapper ClassesInteger, 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:

  • String operations
  • Scanner input
  • ArrayList and HashMap
  • Math utilities
  • 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.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *