Java Intermediate

0% completed

Previous
Next
Utility Classes in Java

Java provides several utility classes that contain useful static methods for performing common operations such as working with collections, handling strings, generating random numbers, and more. These classes help in reducing boilerplate code and improving efficiency by providing ready-to-use functionalities.

Commonly Used Utility Classes

Below is a list of essential utility classes in Java, their purpose, key methods, and simple examples.

Image
Utility ClassPackagePurpose
Arraysjava.utilUtility methods for working with arrays
Collectionsjava.utilUtility methods for working with collections (lists, sets, etc.)
Mathjava.langMathematical functions like rounding, trigonometry, etc.
Randomjava.utilGenerating random numbers
Stringjava.langString manipulation methods
StringBuilderjava.langEfficient string operations for mutable strings

Arrays Class

The Arrays class provides static methods for array operations such as sorting, searching, and conversion.

MethodDescription
sort(array)Sorts the array in ascending order
binarySearch(array, key)Searches for a key in a sorted array and returns its index
asList(array)Converts an array into a List
copyOf(array, newLength)Copies an array into a new array of given length

Example: Using "Arrays" Class

Java
Java

. . . .

Collections Class

The Collections class provides utility methods for working with collections like lists and sets.

MethodDescription
sort(List)Sorts a list in ascending order
reverse(List)Reverses the elements of a list
max(Collection)Returns the maximum element
min(Collection)Returns the minimum element
shuffle(List)Randomly shuffles the elements of a list

Example: Using Collections Class

Java
Java

. . . .

Math Class

The Math class provides mathematical functions like exponentiation, rounding, and trigonometry.

MethodDescription
abs(value)Returns absolute value
pow(base, exp)Returns base raised to the power of exp (base^exp)
sqrt(value)Returns the square root
ceil(value)Rounds up to the nearest integer
floor(value)Rounds down to the nearest integer

Example: Using Math Class

Java
Java

. . . .

Random Class

The Random class is used for generating random numbers.

MethodDescription
nextInt(bound)Returns a random integer within the given bound
nextDouble()Returns a random double between 0.0 and 1.0
nextBoolean()Returns a random boolean value

Example: Using Random Class

Java
Java

. . . .

String Class

The String class provides various methods for manipulating strings.

MethodDescription
length()Returns the length of the string
toUpperCase()Converts all characters to uppercase
toLowerCase()Converts all characters to lowercase
substring(start, end)Extracts a substring
replace(old, new)Replaces occurrences of a character or string

Example: Using String Class

Java
Java

. . . .

StringBuilder Class

The StringBuilder class is used for efficient string modifications.

MethodDescription
append(text)Appends text to the string
insert(index, text)Inserts text at a given index
replace(start, end, text)Replaces part of the string
reverse()Reverses the string

Example: Using StringBuilder Class

Java
Java

. . . .

Mastering these utility classes enhances productivity and enables efficient coding in Java.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next