Loading...
Java

Array Variable Assignments

  • Element level promotions are not applicable at array object level.
    Example: A char value can be promoted to int type but char array cannot be promoted to int array.
    Example:
    int[] x={5, 10, 15, 20};
    int[] y=a;
    char[] ch={‘a’, ‘b’, ‘c’, ‘d’};
  • When we assign one array to another array internal elements won’t be copy just reference variables will be reassigned hence sizes are not important but type must be matched.
    Example:
    int[] x={1, 2, 3, 4, 5, 6};
    int[] y={7, 8, 9};
    x=y;
    y=x;
  • when we assign one array to another array dimensions must be matched that is in the place of one dimensional array we should provide the same type only otherwise we will get compiler time error.
    Example:
    int[] [] a=new int[5] [4];
    a[0]=new int[5];
    a[1]=new int[4];
    a=new int[3] [4];

Leave a Reply

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