sushil kumar gupta
sushil kumar gupta

Reputation: 1

why there is no issue when anonymous array reference to Object class object

Below expression is having no issue-

Object obj = new int[] { 1, 2, 3 };

but below two have compile time issue-

Integer i = new int[] { 1, 2, 3 };
        int j = new int[] {1,2,3};

why?

Upvotes: -2

Views: 36

Answers (1)

Brian Agnew
Brian Agnew

Reputation: 272307

var array = new int[]{1,2,3}

is of type int[], not an integer.

In Java, an array has a superclass of Object. Hence assigning that array to an Object is correct (if practically useless)

Upvotes: 2

Related Questions