0% found this document useful (0 votes)
3 views2 pages

Java Utility Classes Notes

The document provides an overview of several Java utility classes from the java.util package, including StringTokenizer for breaking strings into tokens, BitSet for managing collections of bits, Calendar for date and time manipulation, Random for generating pseudo-random numbers, Formatter for string formatting, and Scanner for reading input. Each class is accompanied by a brief description of its purpose and a code example demonstrating its usage. These utility classes are essential for various programming tasks in Java.

Uploaded by

charanmekala17
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)
3 views2 pages

Java Utility Classes Notes

The document provides an overview of several Java utility classes from the java.util package, including StringTokenizer for breaking strings into tokens, BitSet for managing collections of bits, Calendar for date and time manipulation, Random for generating pseudo-random numbers, Formatter for string formatting, and Scanner for reading input. Each class is accompanied by a brief description of its purpose and a code example demonstrating its usage. These utility classes are essential for various programming tasks in Java.

Uploaded by

charanmekala17
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/ 2

Java Utility Classes

1. StringTokenizer

Package: java.util

Purpose: Breaks a string into tokens based on a delimiter.

Example:

StringTokenizer st = new StringTokenizer("Java,is,fun", ",");

while (st.hasMoreTokens()) {

System.out.println(st.nextToken());

Output:

Java

is

fun

2. BitSet

Package: java.util

Purpose: Represents a collection of bits that grows as needed.

Example:

BitSet bits = new BitSet();

bits.set(2);

bits.set(4);

bits.set(6);

System.out.println(bits); // {2, 4, 6}

3. Calendar

Package: java.util

Purpose: Abstract class for manipulating date and time fields.

Example:

Calendar cal = Calendar.getInstance();


Java Utility Classes

System.out.println("Year: " + cal.get(Calendar.YEAR));

System.out.println("Month: " + (cal.get(Calendar.MONTH) + 1));

System.out.println("Day: " + cal.get(Calendar.DAY_OF_MONTH));

4. Random

Package: java.util

Purpose: Generates pseudo-random numbers.

Example:

Random rand = new Random();

System.out.println(rand.nextInt(100)); // Random number 0-99

System.out.println(rand.nextBoolean()); // true or false

5. Formatter

Package: java.util

Purpose: Formats strings using format specifiers.

Example:

Formatter fmt = new Formatter();

fmt.format("Name: %s, Age: %d", "Charan", 20);

System.out.println(fmt);

6. Scanner

Package: java.util

Purpose: Reads input from various sources like keyboard, file, or string.

Example:

Scanner sc = new Scanner(System.in);

System.out.print("Enter name: ");

String name = sc.nextLine();

System.out.println("Hello " + name);

You might also like