DEV Community

Cover image for Arrays In Java
Saleemuddin Khan
Saleemuddin Khan

Posted on

Arrays In Java

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 array declarations

int[] x;   Recommended --> Because Name is clearly Separated from Type

int []x;   These two ways also valid but first one is mostly use by java programmers
int x[];

int[6] x;  -> Invalid --> Because at the time of declaration we cant specify the size otherwise we will get compile time error.
int[]  x;  -> Valid   --> This is valid because just declare an array.

Enter fullscreen mode Exit fullscreen mode

Two dimensional arrays

Two dimensional array declarations

int[][] x; Recommended --> Because Name is clearly Separated from Type

int [][]x; 

int x[][];

int[] []x;

int[] x[];

int []x[]; 

*/Also all these declaration is valid in java but mostly first one use by Java Programmers./*

Enter fullscreen mode Exit fullscreen mode

Two dimensional array declarations mutiple variable in same line

int[] a,b;  
a-->1 both a and b is one dimensional array.
b-->1

int[] a[],b;
a-->2 a is two dimensional array.
b-->1 but b is one dimensional array.

int[] a[],b[];
a-->2 a is two dimensional array.
b-->1 b is also dimensional array.

int[] []a,b;
a-->2 a is two dimensional array.
b-->1 b is one dimensional array.

int[] []a,b[];
a-->2 a is two dimensional array.
b-->3 but b is three dimensional array.

int[] []a,[]b;     ---> Compile Time Error
int[] []a,[]b,[]c; ---> Compile Time Error

/*if you went specify dimension before the variable the facility is applicable only for first variable in a declaration. if you are try to apply remaining variable we will get compile time error./*

Enter fullscreen mode Exit fullscreen mode

Three dimensional arrays

wo dimensional array declarations

int[][][] a; Recommended --> Beacuse Name is clearly Seprated from Type

int [][][]a;

int a[][][];

int[] [][]a;

int[] a[][];

int[] []a[];

int[][] []a;

int[][] a[];

int [][]a[];

int []a[][];

*/Also all these declaration is valid in java but mostly first one use by Java Programmers/*.

Enter fullscreen mode Exit fullscreen mode

Array Creations in java

One dimensional array creations

One dimensional array creations

Two dimensional array creations

Two dimensional array creations

Three 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.

Image description

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 4

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.

array reference 3

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

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

array reference 2

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 referance

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 → RuntimeException → If Sufficient memory not available.

first case

Second Case →

If we are try to assign 2147483648 size of our array then we get Compile time Exception → Integer Number is too large.
Because → In Java Array Support Maximum Integer Value As Size.
In Java Maximum Int datatype value is → “2147483647”.

array case two

Happy Coding...

Top comments (0)