r/learnjava • • May 28 '24

How in Java it is feasible?

Hi

How in Java below is feasible?

Object array = new Long[10]; // Valid

But not,

Long array = new Long[10]; // Not valid

What is going on here, Technically?

My doubt is, how Object Type holds array value, what’s going on here?

Thanks in advance

Edit : It is not specific to Long Type but we can also provide other Types, have given Long Type for example purpose

1 Upvotes

15 comments sorted by

View all comments

3

u/nekokattt May 28 '24

A long is not an array of longs, in the same way a string is not an array of strings

An array of longs is an objects, because all arrays are objects in Java. That is just how it works.

System.out.println(new Long[3] instanceof Object);
// true
System.out.println(new Long[3] instanceof Long);
// false