Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

How comes kotlin.Char is Serializable? (it is not explicitly defined)

kotlin.Char is defined in the following way:

public class Char private constructor() : Comparable<Char> {
...
}

Where Comparable<T> is defined as:

public interface Comparable<in T> {
    public operator fun compareTo(other: T): Int
}

So how comes a Char is Serializable?

fun main(args: Array<String>) {
  println('A'::class) // class kotlin.Char
  println('A'::class.java) // char
  println('A' is java.io.Serializable) // true
}
  • Is it something added on compilation to Byte code?
  • Is it documented anywhere?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The basic types such as Int, Double, Char, etc. are represented on the JVM in one of two ways (documentation):

  • whenever possible, as primitives (int, double, char),
  • when required to be nullable or when used as generic type parameters, as the respective wrapper classes (Integer, Double, Character).

Both of these representations are serializable. Primitive types are serializable by default as they are, and their wrappers all implement Serializable as well, for example, as you can see in the documentation of Character.

This sort of mapping between Kotlin and Java types is also mentioned here in the Java interop documentation.

So the question is, why don't the Kotlin representations have Serializable as a supertype on the source code level? My guess is so that they're kept platform independent, as having them explicitly implement java.io.Serializable would make them depend directly on a JVM type.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...