wood burning stoves*
The moose likes Beginning Java and the fly likes String size Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String size" Watch "String size" New topic
Author

String size

Barry Brashear
Ranch Hand

Joined: Jun 05, 2001
Posts: 303
Can someone tell me how large a String can be?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

The length of a String is an int, which is 32 bits; the maximum positive value of an int is about 2 billion, and so the theoretical max is about 2 billion characters before things start to go haywire. Now, the practical limit may be much smaller, because a 2 billion character string will take up 4GB of memory, and not all JVMs can use that much RAM for the Java heap.


[Jess in Action][AskingGoodQuestions]
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
I remember answering the same question before...wait let me search for it.

Got It!


Mani
Quaerendo Invenietis
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
If you examine the language and API documentation, it looks like the only hard limit is the size of an int, 2^31-1

Since I'm really a system programmer from way back, I researched your question in the jvm spec:
http://java.sun.com/docs/books/vmspec/2nd-edition/html/VMSpecTOC.doc.html

Remember that all String variables are references into the constant pool. String constants are in modified UTF-8 format, so most English-language strings require one byte per character. The length of the constant is kept in bytes, not characters. The length field is in u2 format, 16 bit unsigned, so the final answer to your question is 65767 ASCII characters, less if you use international characters.

Question for the Java pros on this BB: Is this kind of jvm-based analysis breaking encapsulaton? Said differently, can I write code relying on the jvm spec and counting on Sun to try to maintain backward compatibility in future releases?
[ October 07, 2004: Message edited by: Mike Gershman ]

Mike Gershman
SCJP 1.4, SCWCD in process
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

The length() method returns an int, so do other methods like indexOf() etc


Groovy
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
I remember answering the same question before...wait let me search for it.
http://www.coderanch.com/t/373221/java/java/Length-String


The posters in the referenced thread answer Barry's question by trying increasingly larger strings. When the compiler rejects a large string, they try concatenating to create even larger strings.

I object strongly to this approach in producing code for more than personal use. The results obtained are only valid for a particular Java compiler and a specific jvm running on a particular platform. The Sun Java code can't check for every possible violation of the spec, so they focus on errors a programmer is likely make unwittingly (unlike the SCJP exam). Sun cannot know about deliberate circumventions of the checks and won't protect your code in future releases unless so many people do the same thing that it shows up in beta testing.

My points: stay within Sun's published specs and don't circumvent compiler error messages where the spec is incomplete.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Mike Gershman:

Question for the Java pros on this BB: Is this kind of jvm-based analysis breaking encapsulaton? Said differently, can I write code relying on the jvm spec and counting on Sun to try to maintain backward compatibility in future releases?


That's certainly the idea of having the formal JVM spec and JLS.

Now, one thing I need to point out here is that two different questions are being answered, and I don't know which one the poster actually asked. He said "String", so I assumed he meant the runtime limit on the length of a String. As defined, the String API will support String objects of 2^31-1 characters in length, so this is the limit.

Now, the other question, which is actually a completely separate question, is "What is the maximum length of a String literal?" In other words, how many characters can I type between double quotes in a Java program? Mike has correctly answered this based on the limitations of the JVM and the class file format: it's 65535 characters.

These are both legitimate questions, but they're different questions, and the answers are quite different. Mike's comment about not probing a particular implementation to answer questions is really correct, but he gives, perhaps, the mistaken impression that one should assume that the answers to these two questions are supposed to be the same: they're not. They can both be answered from the specifications, and the answers differ.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String size