File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strings

 
Dan Lund
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How much is it possible to store in a string object? I tried to store a not so large text in one string object and I got a lot of errors. Is it any other way that is better to store texts that are as big as book reviews for example?
 
Bosun Bello
Ranch Hand
Posts: 1510
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no limit as to how large what you can store in a string object can be. Any limitations will depend on your PC's memory. Please post some code so that we can see what's going on.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dan Lund:
How much is it possible to store in a string object? I tried to store a not so large text in one string object and I got a lot of errors. Is it any other way that is better to store texts that are as big as book reviews for example?

As far as I know, the only thing limiting the size of a String is how much memory your machine has. I could be wrong about this - someone please correct me if I am. Based on that, though, you should be able to hold some awfully long strings in a String object.
What kind of errors were you getting? If you show me those, perhaps I can be of more help.
If you're dealing with a lot of long text, you might want to think about writing it to file and then dealing with it there. Hard disk space is cheap (even though it's slow), so you might want to think about accessing it with a RandomAccessFile object.
Corey
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strings are implicitly limited to a maximum length of about 2 gigabytes (Integer.MAX_VALUE if you want it exactly.) This is due to the methods of the string class rather than how java works.
Allocating a string is also dependent upon having enough java heap space for it to fit. So you will never get 2 gigabytes anyways (Sun JVM and OS limits reduce it below that.)
In addition there is a least one bug in the implementation of serialization which can reduce this significantly more - although that has nothing to do with String. A java String literal can contain up to 65K bytes in UTF-8 format if it is going to be serialized. This means that it can have at least 20,000 or so characters and still be successfully serialized.
[ April 02, 2002: Message edited by: Marilyn deQueiroz ]
 
Shubhrajit Chatterjee
Ranch Hand
Posts: 356
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marilyn deQueiroz:
Strings are implicitly limited to a maximum length of about 2 gigabytes (Integer.MAX_VALUE if you want it exactly.) This is due to the methods of the string class rather than how java works.
Allocating a string is also dependent upon having enough java heap space for it to fit. So you will never get 2 gigabytes anyways (Sun JVM and OS limits reduce it below that.)
In addition there is a least one bug in the implementation of serialization which can reduce this significantly more - although that has nothing to do with String. A java String literal can contain up to 65K bytes in UTF-8 format if it is going to be serialized. This means that it can have at least 20,000 or so characters and still be successfully serialized.
[ April 02, 2002: Message edited by: Marilyn deQueiroz ]


Well everything has been said by Marilyn ... even I learnt a few things.
However, I would like to add that using very large Strings might not be advisable, as every time you change your String , a new String object is created in the String pool and your reference points to the new String. Thus you will eat up heap memory very very fast.
Use StringBuffer instead.
 
Dan Lund
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the first error I get is an error "unclosed string literal", in the middel of a sentence, "...put the fire ^ out....".
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A String literal must be given on one line.
This is not legal:
String s = "some very very very
very very long string";
In short, you cannot have a carriage return (CR)and/or linefeed (LF) and/or character '\u000a' within a string literal. The compiler won't like it.
 
Dan Lund
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, thanx
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course, you might be able to get away using the escape character '\' with 'n' to get the desired effect.
String string = "hello, \nthere.";
displays as:
hello,
there.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dirk Schreckmann:
Of course, you might be able to get away using the escape character '\' with 'n' to get the desired effect.
String string = "hello, \nthere.";
displays as:
hello,
there.

If you're going to put a new-line character in a String, it's best to use the System-defined constant for it. The new-line character is different depending upon the underlying OS, so this makes your code more portable:

This should display:
Hello,
World!
on any system that you run this application on.
Corey
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the spirit of nitpicking...
If you're wanting to display the String on a Canvas using something akin to g.drawString from the paint method in an Applet, then neither of the two above examples will work to create the effect of a line break - both will display some ugly block character surrounded by text on one line. You'd have to invoke drawString for each new line desired, providing the appropriate parameters (String and coordinates).
Good Luck.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic