This commit is related to BAEL-6920 (#15371)

This commit aims to add a class "StringMaxLengthMain" showing the string max length.
This commit is contained in:
Mo Helmy 2023-12-06 23:08:38 +02:00 committed by GitHub
parent cab75d510a
commit 4e3e693f71
1 changed files with 35 additions and 0 deletions

View File

@ -0,0 +1,35 @@
package com.baeldung.stringmaxlength;
public class StringMaxLengthMain {
public static void main(String[] args) {
displayRuntimeMaxStringLength();
displayMaxStringLength();
simulateStringOverflow();
}
public static void simulateStringOverflow() {
try {
int maxLength = Integer.MAX_VALUE;
char[] charArray = new char[maxLength];
for (int i = 0; i < maxLength; i++) {
charArray[i] = 'a';
}
String longString = new String(charArray);
System.out.println("Successfully created a string of length: " + longString.length());
} catch (OutOfMemoryError e) {
System.err.println("Overflow error: Attempting to create a string longer than Integer.MAX_VALUE");
e.printStackTrace();
}
}
public static void displayRuntimeMaxStringLength() {
long maxMemory = Runtime.getRuntime().maxMemory();
System.out.println("Maximum String length based on available memory: " + (maxMemory));
}
public static void displayMaxStringLength() {
int maxStringLength = Integer.MAX_VALUE;
System.out.println("Maximum String length based on Integer.MAX_VALUE: " + maxStringLength);
}
}