aspose file tools*
The moose likes Beginning Java and the fly likes Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Strings" Watch "Strings" New topic
Author

Strings

Richard West
Ranch Hand

Joined: Jan 07, 2005
Posts: 127
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



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
Ahmed Huq
Greenhorn

Joined: Aug 16, 2005
Posts: 2
As far as I understand there is NO limit to the size of String. Obviously, you have the limitation in VM's heap size.
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
No stack overflow since no stack is involved. Limit is 2 gigabytes: String length() returns an int and that's your limit. You will exhaust your memory before you exhaust the upper limit of a String. If you have more than a 10K string I suggest looking at alternative strategies to manage that memory.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14352
    
  22

If you think you need to have a String object with 5 to 50 million characters, then most likely you need to re-think the design of your program.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Richard West
Ranch Hand

Joined: Jan 07, 2005
Posts: 127
Hi everyone,

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



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



Would tis 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
 
Don't get me started about those stupid light bulbs.
 
subject: Strings