Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strings

 
Richard West
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15274
38
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Richard West
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic