r/learnjava • u/ajihkrish • May 28 '24
How in Java it is feasible?
Hi
How in Java below is feasible?
Object array = new Long[10]; // ValidBut not,
Long array = new Long[10]; // Not validWhat 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
2
u/Mortomes May 28 '24
Every class in java (including arrays) implicity inherits the Object class. Since a Long[] inherits from Object, it can be assigned to an Object.
A Long and a Long[] are not the same class, nor do they inherit from each other, so a Long[] can not be assigned to a Long.