0% found this document useful (0 votes)
9 views3 pages

Java Utility Classes Explained

Utility Classes in Java provide reusable functionalities through static methods and constants, simplifying code for common operations. Three examples include java.lang.Math for mathematical operations, java.util.Arrays for array manipulation, and java.util.Collections for handling collections. These classes are not meant to be instantiated and are designed to enhance code efficiency.

Uploaded by

Satwik Saxena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Java Utility Classes Explained

Utility Classes in Java provide reusable functionalities through static methods and constants, simplifying code for common operations. Three examples include java.lang.Math for mathematical operations, java.util.Arrays for array manipulation, and java.util.Collections for handling collections. These classes are not meant to be instantiated and are designed to enhance code efficiency.

Uploaded by

Satwik Saxena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Java Utility Classes Explained

What are Utility Classes in Java? Explain any three with


examples.

In Java, Utility Classes are classes that provide common functionalities or helper methods
which are reusable across different parts of a program. These classes typically contain static
methods and constants, and they are not meant to be instantiated (you usually don’t
create objects from them).

They help simplify code by providing ready-made operations for things like string
manipulation, date/time operations, math functions, collections handling, etc.

Here are three commonly used utility classes in Java:

1. java.lang.Math
This class provides methods for performing basic numeric operations like exponentiation,
logarithms, square roots, trigonometry, etc.

Example:

java

public class MathExample {


public static void main(String[] args) {
int a = 10, b = 20;

System.out.println("Max: " + Math.max(a, b)); // 20


System.out.println("Square Root of 25: " + Math.sqrt(25)); // 5.0
System.out.println("Power: " + Math.pow(2, 3)); // 8.0
System.out.println("Random number: " + Math.random()); // Random number
between 0.0 and 1.0
}
}

1/3
2. java.util.Arrays
This class contains methods for manipulating arrays, like sorting, searching, and comparing.

Example:

java

import java.util.Arrays;

public class ArraysExample {


public static void main(String[] args) {
int[] numbers = {5, 2, 8, 1};

Arrays.sort(numbers); // Sorts the array


System.out.println("Sorted: " + Arrays.toString(numbers)); // [1, 2, 5, 8]

int index = Arrays.binarySearch(numbers, 5); // Searches for value 5


System.out.println("Index of 5: " + index); // 2
}
}

3. java.util.Collections
This class provides static methods to operate on or return collections such as lists and sets.
Useful methods include sorting, reversing, finding min/max, etc.

Example:

java

import java.util.*;

public class CollectionsExample {


public static void main(String[] args) {
List<String> fruits = new ArrayList<>(Arrays.asList("Banana", "Apple",
"Mango"));

Collections.sort(fruits); // Sorts alphabetically


System.out.println("Sorted: " + fruits); // [Apple, Banana, Mango]

2/3
Collections.reverse(fruits); // Reverses the list
System.out.println("Reversed: " + fruits); // [Mango, Banana, Apple]

System.out.println("Max: " + Collections.max(fruits)); // Mango


(lexicographically)
}
}

Summary:

Utility Class Purpose

Math Performs mathematical operations

Arrays Works with arrays (sort, search, etc.)

Collections Works with collections (lists, sets, etc.)

Let me know if you want real-world scenarios for these or want to explore custom utility
classes too!

3/3

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy