Java array is a data structure that contains elements of a similar data type. We can store only a fixed set of elements in a Java array. Array in java can access randomly based on the index.

Array in java
Array in java

Array in java is also known as static data structure because the size of an array must be specified at the time of its declaration.

An array can contain primitives data types as well as Objects of a Class. Java Array primitives data types, the actual values are stored in contiguous memory locations. Java Array objects of a class, the actual objects are stored in heap memory.

Advantage of Java Array:
Code Optimization: It makes the code optimized, we can easily retrieve or sort the data.
Random access: We can get data easily based on index position in java array.

Array declaration in java
 //Declaring Arrays In Java
    int[] intarray;
    int intarray[];

    int[] intarray,intarray2;
//both are Java array

   // Wrong Java Array declarations –
    int intarray{};
    int[5] intarray;


Java Array Instantiation
int[] intarray = new int[10];

 int intarray[] = new int[10];

 //Not to mention the size in the box bracket direct Instantiation
 int[] intarray = {4,5,6};

 int[] intarray = new int[] {4,5,6};

 int[] intarray;
 intarray = new int[] {4,5,6};
     int[] intarray,intarray2,intarray3=new int[10];
//Array Declared with size 10

Java 8 Array Declare and initialize in java.
int[] intarray = IntStream.range(0, 10).toArray();

// Range from 0 to 10
// OUTPUT: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

int[] intarray = IntStream.of(2, 4, 6, 8, 10, 12, 14).toArray();

// Custom Array instantiation
// OUTPUT:[2, 4, 6, 8, 10, 12, 14]

int[] intarray = IntStream.of(10, 5, 6, 20, 30, 40, 7).sorted().toArray();

// Java Array Sort
// OUTPUT:[5, 6, 7, 10, 20, 30, 40]

Array declaration in java using primitive,Class, Custom class.
boolean booleanarray[];//right way declaring array
  char[] chararry;
  byte[] bytearry;
  short[] shortarry;
  int[] intarry;
  float[] floatarry;
  long[] longarray;
  double[] doublearray;

                 //class array
  Boolean[] booleanarray;
  Character[] chararry;
  Double[] doublearray;
  //custom class
  JavaArray customarray[];

Array Instantiation in java using primitive,Class, Custom class.
boolean booleanarray[]= {true,false,true,true};
  char[] chararry=new char[] {'a','c','z'};
  double[] doublearray= {20.3,50.4,20.5};
  //class array initialization
  Boolean booleanarray[]= {true,false,true,true};
  Character[] chararry=new Character[] {'a','c','z'};
  Double[] doublearray;
  doublearray=new Double[]{20.3,50.4,20.5};
  //custom class array initialization
      JavaArray customarray[]= {new JavaArray(),new JavaArray()};

Java Arrays are Covariant:

A feature which allows you to subtype with supertype.
You can store a subclass object array to a type of its superclass.

  Number[] numberarray=new Number[5];
  numberarray[0]=new Integer(10);
  numberarray[1]=new Float(27.5f);
  numberarray[2]=new Long(28l);
  numberarray[3]=new Double(24.5d);
  Number arraynumber= numberarray[1];
  System.out.println("Number : "+arraynumber);
  Float floatnumber=(Float) numberarray[1];
  System.out.println("Float : "+floatnumber);
  //3rd Case wrong cast end with Exception
  //Integer arrayinteger=(Integer) numberarray[1];
  //System.out.println("Integer : "+arrayinteger);

Number : 27.5
Float : 27.5

In the third case, the float value is cast with Integer so it will give an exception of java.lang.ClassCastException, here we are trying to improperly convert a class from one type to another.

Post a Comment

Previous Post Next Post