Is 'literal' an 'Object'?


Is ‘literal’ an ‘Object’?
I tried this code as follow.

fun main() {

Process finished with exit code 0

I know Kotlin’s variables are all ‘Object.’
So I tried if ‘literals’ are ‘Object’ or not.

Should I consider ‘literals’ are ‘Object?’


First of all, please use the code highlight. Secondly, if you asking if all classes in Kotlin inherit Any then yes, they are. Even numbers. In the bytecode, they are not guaranteed to be reference types though.


I’m sorry.
I will take care of parts of codes.

And I was lack of studying.
‘Any class’ is fundamental to express Kotlin’s system.

Even literals/numbers are inherited from ’Any.’

I will read the book I have.
I should have read all the parts of this book.
The book illustrates ‘Any.’


To expand on the earlier answer:

As far as the Kotlin language is concerned, everything is an object: all values inherit from Any?. (That is, they are either an instance of Any, or null.) This includes numeric literals, as the code in the question shows, along with string literals, character literals, boolean literals, lambdas, numeric variables, &c.

Now, in Kotlin/JVM (if you’re compiling to Java bytecode), the compiler will implement some of those using Java primitives where it can, for efficiency. But that’s completely transparent; the effect is always the same as if full objects were always used.

(If you’re interoperating with Java code, that may need to know. But it’s transparent to Kotlin code.)


Thanks for adding this topic.
This story is impressive and interesting.

By the way, I’m going to quit Java as it has complicated codes.
I will continue to learn Kotlin.