Arrays → In → Java

Saleemuddin Khan
3 min readOct 12, 2021

Array introduction

  • An arrays is indexed collection of fix no of homogeneous elements.
  • The main advantage of arrays is we can represent usage no of values by using a single variable.so that read of the code ability improved. But the main disadvantage of arrays is fixed in size. That is once we create an arrays there is no chance of increasing or decreasing base on our requirement hence to use arrays concept compulsory we should know size in advance, which may not possible always.

Array declaration in java

One dimensional arrays

Two dimensional arrays

Three dimensional arrays

Array Creations in java

One dimensional array creations

Two dimensional array creations

Three dimensional array creations

Every array in java is an object only. Hence we can create arrays by using new operator.

a ,b,c,p,q,r,x,y,z all is reference variable.

for every array type of corresponding class are available and these class are part of java.lang package and not available to programmer level.

Array Types → Corresponding Class Names

int[] → [I
int[][] → [[I
double[] → [D
short[] → [S
byte[] → [B
boolean[] → [Z
String[] → [java.lang.string;

At the time of array creation compulsory we should specify the size. otherwise we will get compile time error.

It is legal to have an array with size zero in java.

If you are trying to specify array size with some Negative int value then we will get Runtime exception “NagativeArraySizeException

To specify array size allowed data type are byte,short,char,int.

Because byte → to ← short conversion is possible. short → to ← int conversion is possible. char → to ← int conversion is also possible.

long →to ← int conversion is not possible so when we try then it show compile time error.

conclusion if you are try to specify any other type then we will get compile time error.

Array Maximum Size Allowed In Java

First Case →

The maximum allowed array size in java is “2147483647”.

which is the maximum value of int datatype.

Even in the first case we may get Runtime Exception If Sufficient memory not available.

Second Case →

If we are try to assign 2147483648 size of our array then we get Compiletime Exception Integer Number is too large.

Because In Java Array Support Maximum Integer Value As Size.

In Java Maximum Int datatype value is “2147483647”.

Happy Coding….

--

--

Saleemuddin Khan

👨‍💻 Android Native Application Developer | Java | Kotlin. 🎯 Post About 🧲 Android | Java | Kotlin