Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Max length of a String? (http://www.velocityreviews.com/forums/t141739-max-length-of-a-string.html)

Gil 03-09-2005 01:04 PM

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

Dag Sunde 03-09-2005 01:17 PM

Re: Max length of a String?
 
"Gil" <brightoceanlight@hotmail.com> wrote in message
news:adc2ca29.0503090504.1c8af2fd@posting.google.c om...
> Does anyone know what the maximum length of a Java String can be?


The size of the available Heap memory?

--
Dag.



Edwin Martin 03-09-2005 01:22 PM

Re: Max length of a String?
 
Gil wrote:
> 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

--
http://www.bitstorm.org/

Fahd Shariff 03-09-2005 02:08 PM

Re: Max length of a String?
 
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...

--
Fahd Shariff
"Let the code do the talking... "


John C. Bollinger 03-09-2005 02:25 PM

Re: Max length of a String?
 
Gil wrote:

> 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.

--
John Bollinger
jobollin@indiana.edu

Paul van Rossem 03-09-2005 02:50 PM

Re: Max length of a String?
 
On 09-03-2005 14:04, Gil wrote:
> 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.

Paul van Rossem 03-09-2005 02:54 PM

Re: Max length of a String?
 
On 09-03-2005 15:50, Paul van Rossem wrote:
> On 09-03-2005 14:04, Gil wrote:
>
>> 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.

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

Tor Iver Wilhelmsen 03-09-2005 06:48 PM

Re: Max length of a String?
 
"Fahd Shariff" <fahdshariff@yahoo.com> writes:

> 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.

Fahd Shariff 03-09-2005 10:39 PM

Re: Max length of a String?
 
I'm going to be pedantic and say that there are no pointers in Java.

--
Fahd Shariff
"Let the code do the talking... "


Antti S. Brax 03-10-2005 08:34 AM

Re: Max length of a String?
 
me@dagsunde.com wrote in comp.lang.java.programmer:
> "Gil" <brightoceanlight@hotmail.com> wrote in message
> news:adc2ca29.0503090504.1c8af2fd@posting.google.c om...
>> Does anyone know what the maximum length of a Java String can be?

>
> 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.

--
Antti S. Brax Rullalautailu pitää lapset poissa ladulta
http://www.iki.fi/asb/ http://www.cs.helsinki.fi/u/abrax/hlb/
"Disconnect this cable to shorten, re-connect to lengthen."
-- Instructions on Logitech's USB mouse extension cord.


All times are GMT. The time now is 10:09 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.