Loading...
Java

Array Element Assignments

  • In primitive arrays as array elements is allowed which can be promoted to declare type.
    Example:
    For int arrays allowed array elements types are byte, short, char, int.
    int[] a=new int[10];
    a[0]=86;
    a[1]=’x’;
    byte b=10;
    a[2]=b;
    short s=30;
    a[3]=s;
  • Object type arrays, array elements can be either declared type objects or its child class objects.
    Example1:
    Number[] n=new Number[10];
    a[0]=new Double(5.5);
    a[1]= new Integer(5);

    Example2:
    Object[] a=new Object[10];
    a[0]=new String(“WebnoidSchools”);
    a[1]=new Integer(10);
    a[2]=new Object();
  • Interface type arrays as array elements can provide its implemented class objects.
    Example:
    Runnable[] r=new Runnable[10];
    r[0]=new Thread();
    r[1]=new String(“Webnoidschool”);
Array TypeAllowed element Type
Primitive arraysAny type which can be promoted to declared type.
Object type arrayEither declared type or its child class objects allowed.
Interface type arrayIts implemented class objects allowed.
Abstract class type arrayIts child class objects are allowed.

Leave a Reply

Your email address will not be published. Required fields are marked *