I have a question for a long time and I haven't found any answer for it. The question is: How Kotlin defined Its own built-in classes like Int and String. I mean there is a class named Int in Primitives.kt. How it is possible. Even it has some Int variables in itself. I some languages like C++ has created string class by itself, but C++ has pointers which even I can do create a string class by it. But in kotlin the way is different which all types in Kotlin are pointers. I want to know how the compiler does that.
Asked
Active
Viewed 61 times
0
-
2does this help? https://stackoverflow.com/a/57408889/12506486 – Stachu Jul 02 '21 at 21:40
-
1Forget about the distinction between pointers and values. Kotlin is a high-level language, so you don't have to worry about that. The fact that you have to worry about it in a language like Java (i.e. with primitives) is a major wart on the language, and Kotlin allows us to forget all of that and treat all objects the same. The fact that some of them are primitive is a compiler optimization technique that we programmers never see. – Silvio Mayolo Jul 02 '21 at 21:53
-
See https://kotlinlang.org/docs/java-interop.html#mapped-types – Михаил Нафталь Jul 02 '21 at 22:32
-
@SilvioMayolo “we programmers never see” is perhaps an exaggeration because there is `IntArray` vs. `Array
`, and it’s useful to know `Int` is more performant than `Int?`, and now there are `val class`es too. – Tenfour04 Jul 03 '21 at 04:53 -
This is too big a question for this site because you’re basically asking for an explanation for the entire workings of a VM. – Tenfour04 Jul 03 '21 at 04:55
-
@Stachu thank you, your comment was much better than others, and I found the way which Kotlin does, I think Kotlin does like i.e. std::string in C++, we can't see data member of std::string in the string header but we see some methods which contain constructors and ... so I guess Kotlin does the same – Ahmad Mahmoudi Jul 03 '21 at 08:22