-
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
-
Have you read the doc? What does it say?
-
Hi everyone,
 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
-
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.
-
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.
-
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
-
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
-
By Miha Markic in forum .NET
Replies: 5
Last Post: 06-19-2006, 09:48 AM
-
Replies: 0
Last Post: 08-24-2002, 09:10 AM
-
By Richard Kirkcaldy in forum Java
Replies: 5
Last Post: 05-16-2002, 12:17 PM
-
By Mark Alexander Bertenshaw in forum VB Classic
Replies: 4
Last Post: 03-04-2002, 07:04 AM
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks