Win a copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
master stewards:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
garden masters:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
gardeners:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Is there anyway to find out the maximum length allowed for java.lang.String?

 
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read somewhere that Integer.MAX_VALUE (2^31-1) is the way.
Is it true?

Thanks,
Guru
 
Saloon Keeper
Posts: 6179
157
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would be 2GB, so you'd have to run the JVM with more than that amount of memory, or you'd get an OutOfMemoryError before reaching that limit.
The source code of the java.lang.String class -to be found in a file called src.zip somewhere in your JDK installation- should tell you for sure.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is it really so interesting to know what the limit is to the amount of data that you can store in a String? If you are writing a program which migh approach this limit, then you are probably making a mistake in the design.
 
Marshal
Posts: 68014
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't say in the String code, but you can see that there is a char[] hidden in the String class. Now the index of an array is an int, so the largest you can declare is 2147483647, which is equal to 0x7fffffff or 2 to the 31st - 1, as you have already been told.
If you look in the Java Language Specification, you can see exactly what it says about maximum sizes of arrays. It tells you . . . nothing.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Jesper de Jong . String are not meant to be hold such a large data. String are mainly designed to hold temporary data. you should relook into your design.
 
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!