• 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
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

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: 6027
154
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: 67330
256
  • 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.
 
Hang a left on main. Then read this tiny ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!