nerd
nerd

Reputation: 369

maximum size of String Array in Java

What is the maximum number of strings the String Array can hold in String Array in java? I am trying to fill a String array in java with as many as around 9000 entries and it is crashing

Thanks, Nerd

Process: com.example.Nerd.sample_app, PID: 3729
java.lang.VerifyError: Rejecting class com.example.Nerd.sample_app.MainActivity2 because it failed compile-time verification (declaration of 'com.example.Nerd.sample_app.MainActivity2' appears in /data/app/com.example.Nerd.sample_app-2/base.apk)
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at java.lang.Class.newInstance(Class.java:1572)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

Upvotes: 2

Views: 4437

Answers (2)

When you create a array you must specify the size of the array as a int. So in that case if you are having enough memory the maximum length of a array is Integer.MAX_VALUE which is equals to 2^31-1 But in real world scenarios this limit can be lot less than the actual maximum.

Upvotes: 0

Brett Okken
Brett Okken

Reputation: 6306

The max size of any array (Object or primitive) is bound by the max size of an int -> 2^32 - 1. Approximately 2 billion.

Upvotes: 1

Related Questions