BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java String Max Size

Last Updated: June 1, 2024 by Chaitanya Singh | Filed Under: java

In Java, a string is internally stored as an array of characters. This means the max size of a string is equal to the max size of an array, which is Integer.MAX_VALUE (or 2^31 – 1) = 2147483647. However, in practice, the actual maximum size of a String is limited by the available memory on the system.

Although, you are unlikely to face any issues as the number 2147483647 is huge, however in case, if you are dealing with such huge amount of data then it is better to switch to a different data structure or file based storage rather than consuming the whole memory of system.

Program to print max length of String

Let’s say, If you want to write a Java program to find the max length of a string that contains only char ‘a’. Note: Running this program might consume a large amount of memory and could potentially crash your JVM if the available memory is insufficient. Use caution when running it, especially on systems with limited memory.

public class MaxStringSize {
public static void main(String[] args) {
// Try to create a String with the maximum size
try {
int maxSize = Integer.MAX_VALUE;
StringBuilder builder = new StringBuilder(maxSize);
for (int i = 0; i < maxSize; i++) {
builder.append('a'); // filling the string with 'a' chars
}
String maxString = builder.toString();
System.out.println("String created successfully with size: " + maxString.length());
} catch (OutOfMemoryError e) {
System.err.println("Failed to create string. Out of memory.");
e.printStackTrace();
}
}
}

In this program, we attempt to create a string with the max size possible in Java. We are using StringBuilder to create a string by appending ‘characters’a’ repeatedly until it reaches the maximum size. If the program runs out of memory during this process, the program catches it using OutOfMemoryError exception and prints the error message.

❮ Java Tutorial

Top Related Articles:

  1. Java StringBuilder delete()
  2. how to copy one hashmap content to another hashmap
  3. How to take array input in Java
  4. Java Math.abs() Method
  5. Java StringBuilder toString()

Tags: Java-Strings

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap