Strings


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Strings

  1. #1
    Join Date
    Oct 2004
    Posts
    151

    Question Strings

    Hi everyone,

    I have a rather silly question but what is the maximum amount of characters the String object can hold?

    This is what i mean

    Code:
    String str1 = some buffer that contains about 5 million characters as a string
    What i am afraid is that if the String object may throw an exception if i return a huge string object say something down the lines of 5-50 million characters as a string. Will there be stack overflow?

    I hope someone can help me with this

    Thank You

    Yours Sincerely

    Richard West
    What type of softwares do you like and what would you pay for it?

    http://www.wizardsolutionsusa.com/fo...hread.php?t=12

  2. #2
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    Have you read the doc? What does it say?

  3. #3
    Join Date
    Oct 2004
    Posts
    151

    Question

    Hi everyone,

    Quote Originally Posted by Norm
    Have you read the doc? What does it say?
    If you are talking about the String.length() where the maximum is about 2gigs then i know that but what is also an issue for me is the problem of immutable and mutable.

    The reason why i am using the string in this way is because the object that is returning the huge string is a document.

    Code:
    String str1 = JTextPane.getText();
    Why i need this is because i use this method to search the document for a certain string. Everything works but i am afraid that if the document returns a huge string from the document, i may actually exceed the limit.

    On the mutability issue i also felt that because the String object is immutable would it be better if i use the StringBuffer class as that does not create a copy of the string something like that

    Code:
    StringBuffer str1 = new StringBuffer(JTextPane.getText());
    Would this in your opinion norm actually make a difference in that the String object is immutable and the StringBuffer object is mutable?

    Any help is greatly appreciated

    Thank You

    Yours Sincerely

    Richard West
    What type of softwares do you like and what would you pay for it?

    http://www.wizardsolutionsusa.com/fo...hread.php?t=12

  4. #4
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    Write a test program to see how large a string you can put in a String object. Be sure to give the java.exe enough space by using the -Xms option.

    Please post the results here so we'll all know.
    Last edited by Norm; 08-24-2005 at 03:46 PM.

  5. #5
    Join Date
    Jul 2005
    Location
    the Netherlands
    Posts
    128
    Hi Richard,

    Did you check the rest of the Java forums where you posted this question? I saw that some guys on Daniweb gave some interesting answers, you'd check it out. If the answer is there, could you please leave a note here that your question has been aswered? Otherwise other people here might put some time in it while you've already been answered.

    Cheers.

  6. #6
    Join Date
    Oct 2004
    Posts
    151
    Hi everyone,

    Here are the results. There does not seem to be striaght answer for this but this is far the best so far.

    You see since the getText returns a string, i'm stuck with the string immutable object. Now this is what i did

    This can hold a maximum of nearly 5 million characters under 64M of Ram

    Code:
    String str1 = JTextPane.getText();
    This can hold a maximum of nearly 10 million characters under 64M of Ram

    Code:
    StringBuffer str1 = new StringBuffer(JTextPane.getText());
    I tried digging further and found out that this is due to the fact that the StringBuffer is mutable and the String object is immutable, that's why i got nearly twice the characters on the same amount of memory for the StringBuffer.

    Please note that i tried this calculation by trial and error so the figures are not very accurate but the factor by which they differ seems fairly consistant

    So i think the only solution is to use the StringBuffer and hope the people that use my application keep their characters under 2Gig(which i think is fair)

    Yours Sincerely

    Richard West
    What type of softwares do you like and what would you pay for it?

    http://www.wizardsolutionsusa.com/fo...hread.php?t=12

  7. #7
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    A reason that the Stringbuffer allows more is probably because there is only one copy of the chars in memory. In trying to create a String from a StringBuffer there will be two copies: one in each.
    So if you want to have LOTS of chars in memory, you need a suite of methods for StringBuffer to do the functions of those done by the String methods.

Similar Threads

  1. Replies: 5
    Last Post: 06-19-2006, 09:48 AM
  2. search strings in XML
    By Alina in forum XML
    Replies: 0
    Last Post: 08-24-2002, 09:10 AM
  3. comparing strings
    By Richard Kirkcaldy in forum Java
    Replies: 5
    Last Post: 05-16-2002, 12:17 PM
  4. Allocating memory in VB (Custom strings)
    By Mark Alexander Bertenshaw in forum VB Classic
    Replies: 4
    Last Post: 03-04-2002, 07:04 AM
  5. Strings and memory management
    By Brian Leung in forum VB Classic
    Replies: 36
    Last Post: 09-18-2000, 09:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center