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:
parent
cab75d510a
commit
4e3e693f71
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue