简体   繁体   English

Java Integer.MAX_VALUE 与 Kotlin Int.MAX_VALUE

[英]Java Integer.MAX_VALUE vs Kotlin Int.MAX_VALUE

I noticed, one interesting thing.我注意到,一件有趣的事情。
Java's Integer.MAX_VALUE is 0x7fffffff (2147483647) Java 的Integer.MAX_VALUE0x7fffffff (2147483647)
Kotlin's Int.MAX_VALUE is 2147483647 Kotlin 的Int.MAX_VALUE2147483647
but if you write但如果你写
in Java:在 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

Interesting right?有趣吧? So you're able to do something like new java.awt.Color(0xFFFFFFFF, true) in Java but not in Kotlin.因此,您可以在 Java 中执行类似new java.awt.Color(0xFFFFFFFF, true) ,但在 Kotlin 中则不能

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) ). Color类在“二进制”级别与该 int 一起使用,因此对于具有所有构造函数( Color(int rgba)Color(int r, int g, int b, int a) )的两个平台,一切都正常。
Only workaround which I found for kotlin is java.awt.Color(0xFFFFFFFF.toInt(), true) .我为 kotlin 找到的唯一解决方法是java.awt.Color(0xFFFFFFFF.toInt(), true)

Any idea why is it like this in Kotlin?知道为什么在 Kotlin 中会这样吗?

This is partially answered here :这是部分回答here

In Kotlin you need to prepend the - sign to denote negative Int which is not true in Java.在 Kotlin 中,您需要在前面加上-符号来表示负 Int,这在 Java 中是不正确的。

So it seems that Java will interpret hex literals as signed , whereas Kotlin will treat them as unsigned.因此,Java 似乎会将十六进制文字解释为signed ,而 Kotlin 会将它们视为 unsigned 。

The negation would have to be done manually.否定必须手动完成。

Small aside: JetBrains' Kotlin converter actually converts小题大做:JetBrains 的 Kotlin 转换器实际上可以转换

int a = 0xffffffff;

to

var a = -0x1

but this may just be it realizing exactly what you have noticed.但这可能只是它完全意识到你注意到了什么。


The part of the spec for hexadecimal literals doesn't mention this at all, however. 然而,十六进制文字的规范部分根本没有提到这一点。

我认为,这个问题应该由Kotlin 1.3UInt来解决,请在此处查看更多信息: https : //kotlinlang.org/docs/reference/whatsnew13.html#unsigned-integers

The explanation is in the reference docs :解释在参考文档中

Due to different representations, smaller types are not subtypes of bigger ones.由于不同的表示,较小的类型不是较大类型的子类型。 If they were, we would have troubles of the following sort:如果是这样,我们就会遇到以下问题:

 // Hypothetical code, does not actually compile: val a: Int? = 1 // A boxed Int (java.lang.Integer) val b: Long? = a // implicit conversion yields a boxed Long (java.lang.Long) print(a == b) // Surprise! This prints "false" as Long's equals() // check for other part to be Long as well

So not only identity, but even equality would have been lost silently all over the place.因此,不仅身份,甚至平等都会无声无息地消失在整个地方。

As a consequence, smaller types are NOT implicitly converted to bigger types.因此,较小的类型不会隐式转换为较大的类型。 This means that we cannot assign a value of type Byte to an Int variable without an explicit conversion.这意味着我们不能在没有显式转换的情况下将 Byte 类型的值分配给 Int 变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM