Max length of a String?

Discussion in 'Java' started by Gil, Mar 9, 2005.

  1. Gil

    Gil Guest

    Does anyone know what the maximum length of a Java String can be?
    Gil, Mar 9, 2005
    #1
    1. Advertising

  2. Gil

    Dag Sunde Guest

    "Gil" <> wrote in message
    news:...
    > Does anyone know what the maximum length of a Java String can be?


    The size of the available Heap memory?

    --
    Dag.
    Dag Sunde, Mar 9, 2005
    #2
    1. Advertising

  3. Gil

    Edwin Martin Guest

    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/
    Edwin Martin, Mar 9, 2005
    #3
  4. Gil

    Fahd Shariff Guest

    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... "
    Fahd Shariff, Mar 9, 2005
    #4
  5. 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
    John C. Bollinger, Mar 9, 2005
    #5
  6. 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, Mar 9, 2005
    #6
  7. 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).
    Paul van Rossem, Mar 9, 2005
    #7
  8. "Fahd Shariff" <> 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.
    Tor Iver Wilhelmsen, Mar 9, 2005
    #8
  9. Gil

    Fahd Shariff Guest

    I'm going to be pedantic and say that there are no pointers in Java.

    --
    Fahd Shariff
    "Let the code do the talking... "
    Fahd Shariff, Mar 9, 2005
    #9
  10. wrote in comp.lang.java.programmer:
    > "Gil" <> wrote in message
    > news:...
    >> 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.
    Antti S. Brax, Mar 10, 2005
    #10
  11. Gil

    shashi@22

    Joined:
    Nov 10, 2012
    Messages:
    1
    aAJSAs

    dnfdsfndfdfndfndfndf,mc,zccfsdf
    shashi@22, Nov 10, 2012
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sam
    Replies:
    3
    Views:
    14,081
    Karl Seguin
    Feb 17, 2005
  2. Kaidi
    Replies:
    4
    Views:
    4,365
    Michael Borgwardt
    Sep 24, 2004
  3. Replies:
    5
    Views:
    656
    John W. Kennedy
    Jan 11, 2007
  4. Summercool
    Replies:
    9
    Views:
    871
    dorayme
    Oct 23, 2007
  5. Aryeh M. Friedman

    max internal length of String

    Aryeh M. Friedman, Jul 27, 2009, in forum: Java
    Replies:
    9
    Views:
    368
    Mike Schilling
    Jul 29, 2009
Loading...

Share This Page