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.
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./*
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./*
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/*.
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 → RuntimeException → If Sufficient memory not available.
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”.
Happy Coding...
Top comments (0)