Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Maximum length of a string

  1. #1
    Member
    Join Date
    Jan 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Maximum length of a string

    I need to store a string of length approx 2 60 000 characters. I get an error saying that it is too large. Searching on Google, I found different answers and each one has a reason. Now, what is the maximum length of a String and what do I do if I wish to store strings larger than that maximum size?


  2. #2
    Junior Member HandgunSally's Avatar
    Join Date
    Mar 2012
    Location
    Cleveland, OH
    Posts
    16
    My Mood
    Lurking
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Maximum length of a string

    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.

    I HOPE THIS HELPS

  3. #3
    Member
    Join Date
    Jan 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Re: Maximum length of a string

    Quote Originally Posted by HandgunSally View Post
    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.

    I HOPE THIS HELPS
    Thanks for your reply but I couldn't understand much of it. This is what I actually want to type in the editor:
    String str="000000000 1"+
    "000000001 1"+
    "000000002 1"+
    "000000010 1"+
    "000000011 1"+
    "000000012 1"+
    // and around 20000 lines in this way
    +"222222221 1"+
    "2222222222 1";
    I am getting a compilation error that the String literal is too large. As per the maximum value stated by you, I shouldn't be getting this error. Is there any other alternative way to store this large String literal?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Maximum length of a string

    Hardcoding through string addition is possibly not the best practice anyway, depending upon what your needs are. Store the String in a File, or use a StringBuilder or StringBuffer.

Similar Threads

  1. [SOLVED] Problem using the length of a string to bound the number of iterations of a for loop
    By dtitt3 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 3rd, 2011, 01:44 PM
  2. execute a method over a maximum period of time
    By PedroCosta in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 24th, 2011, 02:06 PM
  3. max length of an array?
    By qsbladey in forum Collections and Generics
    Replies: 4
    Last Post: April 3rd, 2011, 11:17 AM
  4. Setting JFrame maximum size that pack() can not violate
    By Javabeginner in forum AWT / Java Swing
    Replies: 3
    Last Post: September 3rd, 2010, 05:53 AM
  5. Run Length Encoding Problem
    By Scottj996 in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 7th, 2010, 07:24 AM