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

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.

1

u/ajihkrish May 28 '24

Understood

But you say Long[] inherits from Object, and Long also inherits Object, right, then Long should hold values?(I know it is not valid, but wanting explanations)

Thanks

4

u/josephblade May 28 '24
Object longArrayAsObject = new Long[3];

here you create an array and then put it in a container that says "this is an object'. this is correct, right since an Array (of longs) is an Object

Object longAsObject = new Long(33);

here, we create a single Long object and then put it in a container that says "this is an object". this too is correct since a Long is an Object.

Long longArrayAsLong = new Long[3];

here we create an array and then try to store it in a container that says "this is a Long". since an Array is not a Long (it is an array) this is not correct.

a variable type is a description of what object sits inside of it. A variable of type Object can hold any object (Long, Long[], String, etc). A variable of type Long can only hold Longs (and if it was possible, any subclasses of Long, but nothing else)

one last explanation that may help: when you give a variable a type it declares to the programming universe: this variable can be used in this way. It's memory layout follows this pattern. the compiler will then safeguard you against making mistakes and will only allow objects that follow that layout / provide the method calls that that type provides.

for instance:

public class A {
    int one; int two; byte three; byte four;
}

an object of type A would look like this in memory:

[4 bytes][4 bytes][byte][byte]

now imagine I do this:

public class B {
    int other;
}

with B looking like:

[4 bytes]

and we call the following code:

A a = new B();

if I try to access B as an A and the compiler doesn't stop me, then when I do: a.one it might be kind of ok since the first 4 bytes in both cases are the same. but if you did a.two what would happen? you might access memory that's not in use. or worse, memory in use by another object.

And what would happen if you call a method that the other object doesn't support.

so for this reason you cannot assign one type of variable to another unless they literally are that type. this is also part of what makes subclasses work. subclasses wrap like onion layers around previous layers.