1. StringTokenizer
Used to split strings into tokens based on delimiters.
(Note: It’s a legacy class; String.split()
or Scanner
is usually preferred.)
import java.util.StringTokenizer;
public class Example {
public static void main(String[] args) {
String text = "Java,Python,C++";
StringTokenizer st = new StringTokenizer(text, ",");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
}
}
2. Random
Used to generate random numbers.
import java.util.Random;
public class Example {
public static void main(String[] args) {
Random rand = new Random();
int randomInt = rand.nextInt(100); // random number between 0–99
double randomDouble = rand.nextDouble(); // 0.0 – 1.0
boolean randomBool = rand.nextBoolean();
System.out.println(randomInt);
System.out.println(randomDouble);
System.out.println(randomBool);
}
}
3. Scanner
Used for input (keyboard, file, etc.).
import java.util.Scanner;
public class Example {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.print("Enter your age: ");
int age = sc.nextInt();
System.out.println("Hello " + name + ", you are " + age + " years old.");
sc.close();
}
}
4. Properties
Used to store key-value pairs (often for configuration files).
import java.util.Properties;
public class Example {
public static void main(String[] args) {
Properties props = new Properties();
props.setProperty("username", "admin");
props.setProperty("password", "12345");
System.out.println("Username: " + props.getProperty("username"));
System.out.println("Password: " + props.getProperty("password"));
}
}
5. UUID
Generates unique identifiers.
import java.util.UUID;
public class Example {
public static void main(String[] args) {
UUID id1 = UUID.randomUUID(); // random UUID
UUID id2 = UUID.randomUUID();
System.out.println(id1.toString());
System.out.println(id2.toString());
}
}