File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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

Dan Lund
Greenhorn

Joined: Jul 17, 2001
Posts: 26
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

Joined: Nov 06, 2000
Posts: 1510
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.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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


SCJP Tipline, etc.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
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 ]

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Shubhrajit Chatterjee
Ranch Hand

Joined: Aug 23, 2001
Posts: 356
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.


Shubhrajit
Dan Lund
Greenhorn

Joined: Jul 17, 2001
Posts: 26
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

Joined: Aug 26, 2001
Posts: 7610
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.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Dan Lund
Greenhorn

Joined: Jul 17, 2001
Posts: 26
ok, thanx
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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

Joined: Dec 10, 2001
Posts: 7023
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strings