Java Integer.MAX_VALUE与Kotlin Int.MAX_VALUE [英] Java Integer.MAX_VALUE vs Kotlin Int.MAX_VALUE
问题描述
我注意到了一件有趣的事情.
Java的Integer.MAX_VALUE
是0x7fffffff
(2147483647)
Kotlin的Int.MAX_VALUE
是2147483647
但是如果你写
在Java中:
int value = 0xFFFFFFFF;
//everything is fine (but printed value is '-1')
在Kotlin:
val value: Int = 0xFFFFFFFF //You get exception
The integer literal does not conform to the expected type Int
I noticed, one interesting thing.
Java's Integer.MAX_VALUE
is 0x7fffffff
(2147483647)
Kotlin's Int.MAX_VALUE
is 2147483647
but if you write
in Java:
int value = 0xFFFFFFFF;
//everything is fine (but printed value is '-1')
in Kotlin:
val value: Int = 0xFFFFFFFF //You get exception
The integer literal does not conform to the expected type Int
有趣吗?因此,您可以在Java中执行new java.awt.Color(0xFFFFFFFF, true)
之类的功能,但在Kotlin中执行不能的功能.
Interesting right? So you're able to do something like new java.awt.Color(0xFFFFFFFF, true)
in Java but not in Kotlin.
Color
类在二进制"级别上与该int一起使用,因此对于具有所有构造函数(Color(int rgba)
或Color(int r, int g, int b, int a)
)的两个平台,一切都可以正常工作.
我为kotlin找到的唯一解决方法是java.awt.Color(0xFFFFFFFF.toInt(), true)
.
Color
class works with that int on "binary" level, so everything works fine for both platforms with all constructors (Color(int rgba)
or Color(int r, int g, int b, int a)
).
Only workaround which I found for kotlin is java.awt.Color(0xFFFFFFFF.toInt(), true)
.
有人知道为什么在Kotlin会这样吗?
Any idea why is it like this in Kotlin?
推荐答案
我认为,此问题应由 Kotlin 1.3 和UInt
解决
在此处查看更多信息: https://kotlinlang.org/docs/reference/whatsnew13. html#unsigned-integers
I think, this problem should be solved by Kotlin 1.3 and UInt
See more here: https://kotlinlang.org/docs/reference/whatsnew13.html#unsigned-integers
这篇关于Java Integer.MAX_VALUE与Kotlin Int.MAX_VALUE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!