aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Max No of Elements in Array? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Max No of Elements in Array?" Watch "Max No of Elements in Array?" New topic
Author

Max No of Elements in Array?

Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Hi ,
can anyone tell me..that is there any limit of adding elements in array..like as if i cannont add more that X elements in an array?

also what is the data type of the length field of the array...?
i didnt got anything relating to this anywhere..

Thanx in advance

Sandy


Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
anand phulwani
Ranch Hand

Joined: Sep 10, 2005
Posts: 242
I think i solved one of your problem
length field returns an int.

This predicts the output


main.java:9: possible loss of precision
found : int
required: byte
byte x=a.length;
^
1 error


Thanks and Regards, Anand
SCJP 5.0 310-055 73%, SCWCD 1.4 310-081 78%, IBM DB2 9 Fundamentals 000-730 62%
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
when i initilize an array with Integer.MAX_VALUE i get an error.



What is this error?
anand phulwani
Ranch Hand

Joined: Sep 10, 2005
Posts: 242
I have tried to look out for the error which says that its VM dependent, that is virtual memory dependent,i tried this code which gave me the following error


Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit

so i decided to run a series of program code with the help of loop and find out ,the final code i ran was this which would be a bit clear



which gave me the following constant output when i ran this 4-5 times

15475321
15475322
15475323
15475324
15475325
15475326
15475327
15475328
15475329
15475330
15475331
15475332
15475333
15475334
15475335
15475336
15475337
15475338
15475339
15475340
15475341
15475342
15475343
15475344
15475345
15475346
15475347
15475348
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

and mentioning that my virtual memory is set to 1536-3072
if the program give same output on your machine that note that u can have it as 15475348 which i think will not be true,otherwise u can have this as VM dependent.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Yes, I think it is int too. As far as I can see, the Java Language Specification mentions only a public final member length with no type specified. But in an example showing an equivalent class definition of an array, the length field is of type int. So that would mean a maximum length of an array is 2147483647 elements.

As an example, an array can be created as in the following:



The resultant initialized array of 42 zero (the default value) elements is fixed in length. You cannot add a 43rd element to it.

Of course there is a limit to the amount of memory available to the Java Virtual Machine, so for very large arrays you will probably get an OutOfMemoryError before the theoretical limit is reached.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
You can play with the following "switches" with the java command:
-Xms256m (here the starting heap size is 256 Megabytes)
-Xmx512m (here maximum heap size is 512 Megabytes)

I got an array of 20 000 000 int elements using -Xmx512m.
[ September 11, 2005: Message edited by: Barry Gaunt ]
anand phulwani
Ranch Hand

Joined: Sep 10, 2005
Posts: 242
So How Can We Know The Default Heap Size used By Our JVM.Is it size machine dependent or its constant for all.
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Nice explination...

Thanx for your replies guys.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Max No of Elements in Array?