Generate Random Strings in Java
Complete code tutorial with examples and best practices
[ Code Example - Quick Summary ]
Language: Java
What: Generate random strings in Java using <code>SecureRandom</code> for cryptographically secure random generation. Suitable for enterprise applications and security-critical systems.
Try it: Use our interactive Strings generator or integrate this code into your Java application.
Generate random strings in Java using SecureRandom for cryptographically secure random generation. Suitable for enterprise applications and security-critical systems.
Looking for other languages? Check our code examples in
PHP
,
JavaScript
,
Python
,
C#
,
C++
,
Ruby
and
Go
or use our interactive web generator.
Java Code Example
import java.security.SecureRandom;
public class RandomStringGenerator {
private static final String ALPHANUMERIC =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
private static final String HEX_CHARS = "0123456789abcdef";
public static String generateRandomString(int length, String customChars) {
SecureRandom random = new SecureRandom();
String chars = (customChars != null) ? customChars : ALPHANUMERIC;
StringBuilder result = new StringBuilder(length);
for (int i = 0; i < length; i++) {
int index = random.nextInt(chars.length());
result.append(chars.charAt(index));
}
return result.toString();
}
public static void main(String[] args) {
// Generate a 16-character alphanumeric string
String randomString = generateRandomString(16, null);
System.out.println(randomString); // Example: aB3xY7mN2pQ9zR5t
// Generate a 12-character hex string
String hexString = generateRandomString(12, HEX_CHARS);
System.out.println(hexString); // Example: a3f7b2e9c4d1
// Generate a 10-character lowercase string
String lowercaseString = generateRandomString(10, "abcdefghijklmnopqrstuvwxyz");
System.out.println(lowercaseString); // Example: xmkpqrstuv
}
}
[EXPLANATION]
SecureRandom provides cryptographically strong random number generation. StringBuilder is used for efficient string construction. The method accepts custom character sets for flexible string generation.
Expected Output
aB3xY7mN2pQ9zR5t a3f7b2e9c4d1 xmkpqrstuv
Common Use Cases
- Spring Boot session ID generation
- Android app unique identifiers
- RESTful API token generation
- Database record IDs
- File upload temporary names
- Microservices correlation IDs
Important Notes
-
Always use
SecureRandomfor security tokens -
StringBuilderimproves performance over concatenation -
Consider Apache Commons Lang
RandomStringUtils -
For UUID-like strings, use
UUID.randomUUID()
Try Our Interactive Generator
Don't want to write code? Use our free web-based Strings generator with instant results.
TRY STRINGS GENERATOR →Other Programming Languages
View Strings generation code examples in PHP
View Strings generation code examples in JavaScript
View Strings generation code examples in Python
View Strings generation code examples in C#
View Strings generation code examples in C++
View Strings generation code examples in Ruby
View Strings generation code examples in Go