Max length of a String?

E

Edwin Martin

Gil said:
Does anyone know what the maximum length of a Java String can be?

class String implements java.io.Serializable {
private char value[]; // 4 bytes + 12 bytes of array header
private int offset; // 4 bytes
private int count; // 4 bytes
}


The largest number a four-byte signed integer can hold is 2,147,483,647.

So I guess that's the max length of a String.

Edwin Martin
 
F

Fahd Shariff

1 char = 2 bytes not 4 bytes since Java uses 16 bit unicode.

I guess it would be Integer.MAX_VALUE but also depends on your
available RAM...
 
J

John C. Bollinger

Gil said:
Does anyone know what the maximum length of a Java String can be?

There is no explicit maximum, but the String API won't support Strings
longer than Integer.MAX_VALUE (in excess of two billion) characters.
Since characters are two bytes in size, a maximal String would require
more than 4GB of storage. A 32-bit computer cannot even address enough
space for such a behemoth. (It could barely address all the characters,
but not the object overhead. And it couldn't at the same time address
such trivialities as, say, the operating system kernel.) Thus in
practice, you will run into other limits long before you hit the
hypothetical maximum String size.

All of that is beside the point however. Note well: *anything you are
contemplating that would make use of very large Strings is a BAD IDEA*.
I say that with confidence despite having no other information about
your plan.
 
P

Paul van Rossem

Does anyone know what the maximum length of a Java String can be?

In theory: 2^31 - 1 = 2147483647 (~2 GigaByte).
In practice: end of virtual memory.

Paul.
 
P

Paul van Rossem

In theory: 2^31 - 1 = 2147483647 (~2 GigaByte).
In practice: end of virtual memory.

Paul.
With 2 bytes per unicode character that is 4 GB of course (not 2 GB).
 
T

Tor Iver Wilhelmsen

Fahd Shariff said:
1 char = 2 bytes not 4 bytes since Java uses 16 bit unicode.

The article you responded to were taling about how a pointer (in this
case to the char array) is four bytes.
 
A

Antti S. Brax

The size of the available Heap memory?

Java character takes two bytes, so correct answer would be
half of the available heap memory. If available heap exceeds
Integer.MAX_VALUE twice then maximum length would Integer.
MAX_VALUE.

And in case you are constructing the string on the fly using a
StringBuffer and do not know the final length of the string the
theoretical maximum is 1/3 of the size of the total heap memory
that is available to the JVM. When StringBuffer overflows it's
internal buffer it doubles the size of the old buffer. Thus, the
last reallocation that succeeds can allocate no more than 2/3 of
the total heap memory. In practise it fails much sooner. :)

And the absolute hard limit is Integer.MAX_VALUE in any case.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top