请问在Kotlin中,Int类实例化和直接赋值一个整形有什么区别呢?

public class Int private constructor() : Number(), Comparable<Int> {
    companion object {
        /**
         * A constant holding the minimum value an instance of Int can have.
         */
        public const val MIN_VALUE: Int = -2147483648

        /**
         * A constant holding the maximum value an instance of Int can have.
         */
        public const val MAX_VALUE: Int = 2147483647

        /**
         * The number of bytes used to represent an instance of Int in a binary form.
         */
        @SinceKotlin("1.3")
        public const val SIZE_BYTES: Int = 4

        /**
         * The number of bits used to represent an instance of Int in a binary form.
         */
        @SinceKotlin("1.3")
        public const val SIZE_BITS: Int = 32
    }
}

以Int为例,既然类里没有定义属性或者方法,那么实例化一个Int类,这个对象的值该如何访问呢。
换言之,我没太搞懂这两种方式:类实例化和直接赋值有什么区别,以及它们是如何运作的

阅读 1.5k
1 个回答
val i: Int = Int(1)
val j: Int = 1

你想问这两者的区别吗?

你没发现你贴的代码里的构造器是 private 的么……前者压根不能编译通过,这就是区别。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进