changed
changed

Reputation: 2143

java array assignment question

While using method Arrays.deepToString(Object [] a) I am facing this problem which I can put down in this way.

 Object [] not_allowed = new  int[]{7, 9, 8};
 Object [] allowed  = new int[][]{{1, 2, 3}, {6, 5, 4}};

Why i am not allowed to assign int[] to Object[] in single dimension case but i am allowed to do such in multidimension case.

Upvotes: 1

Views: 791

Answers (6)

JaakkoK
JaakkoK

Reputation: 8387

One additional point to mention is that the line that is allowed

Object [] allowed  = new int[][]{{1, 2, 3}, {6, 5, 4}};

actually shouldn't be either because you should be able to follow it up with

allowed[0] = "Surprise!";

and so manage to put a String into an array that should contain only int[]s. This is why Java has to check each assignment to an array element at run time to make sure that the assigned element is compatible with the real type of the array, and not just let compile-time type checking handle it. So the above code compiles but will cause an exception on trying to assign the String.

This problem also affects only arrays. When generics were added to Java, the designers knew better and in generic collections, a collection of int[] is not a subtype of a collection of Object, so if, in your example, you switch from arrays to generic ArrayLists, say, neither of the lines will compile.

Upvotes: 0

fastcodejava
fastcodejava

Reputation: 41117

Your first line is invalid :

Object [] not_allowed = new  int[]{7, 9, 8};

This is because int is not an Object. This will be valid :

Object allowed = new  int[]{7, 9, 8};

Upvotes: 1

Chandra Patni
Chandra Patni

Reputation: 17587

Primitive types like int are not Objects while an array is an Object-- you can assign any array to an Object reference

Object o = new  int[]{7, 9, 8};

new int[][] is an array of objects and thus can be assigned to Object[]. You may want to write a utility method like this to do what you want:

public static String arrayToString(Object o) {
    if(o instanceof Object[])  {
        return Arrays.deepToString((Object[]) o);
    }
    else if(o instanceof long[]) {
        return Arrays.toString((long[]) o);
    }
    else if(o instanceof int[]) {
        return Arrays.toString((int[]) o);
    }
    else if(o instanceof short[]) {
        return Arrays.toString((short[]) o);
    }
    else if(o instanceof byte[]) {
        return Arrays.toString((byte[]) o);
    }
    else if(o instanceof float[]) {
        return Arrays.toString((float[]) o);
    }
    else if(o instanceof double[]) {
        return Arrays.toString((double[]) o);
    }
    else if(o instanceof boolean[]) {
        return Arrays.toString((boolean[]) o);
    }
    throw new IllegalArgumentException("input is not an array");
}

Example:

Object intArray = new  int[]{7, 9, 8};
Object[] intintArray  = new int[][]{{1, 2, 3}, {6, 5, 4}};
Object[] intintintArray  = new int[][][]{{{1, 2, 3}, {6, 5, 4}},
               {{1, 2, 3}, {6, 5, 4}}};
System.out.println(arrayToString(intArray));
System.out.println(arrayToString(intintArray));
System.out.println(arrayToString(intintintArray));

Output:

[7, 9, 8]
[[1, 2, 3], [6, 5, 4]]
[[[1, 2, 3], [6, 5, 4]], [[1, 2, 3], [6, 5, 4]]]

Upvotes: 7

Roland Bouman
Roland Bouman

Reputation: 31981

The first assignment does not work because the element type of int[] is int, not Object. Since (you are trying to assign to Object[], the element type should be an Object, not int)

The second assignment does work, because int[] is an Object, therefore int[][] is an Object[].

Upvotes: 2

MattGrommes
MattGrommes

Reputation: 12354

Lower-case int is a primitive and does not inherit from Object. Try using Integer instead.

Upvotes: 5

Carl Smotricz
Carl Smotricz

Reputation: 67800

ints are not Objects, that's why the first assignment is illegal: An array of ints is not an array of Objects.

But arrays, no matter of what, are objects. You're successfully assigning the outer data structure, which is an array of arrays, to an array of Objects.

Upvotes: 5

Related Questions