diff --git a/core-java-modules/core-java-string-operations-7/src/main/java/com/baeldung/stringmaxlength/StringMaxLengthMain.java b/core-java-modules/core-java-string-operations-7/src/main/java/com/baeldung/stringmaxlength/StringMaxLengthMain.java new file mode 100644 index 0000000000..42d0c05582 --- /dev/null +++ b/core-java-modules/core-java-string-operations-7/src/main/java/com/baeldung/stringmaxlength/StringMaxLengthMain.java @@ -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); + } +} \ No newline at end of file