Is 'literal' an 'Object'?


#1

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

fun main() {
println(1.toString())
println(1.0.toString())
}
/*
1
1.0

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?’


#2

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.


#3

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.’


#4

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.)


#5

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.