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
Interesting right? So you're able to do something like new java.awt.Color(0xFFFFFFFF, true)
in Java but not in 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)
).
Only workaround which I found for kotlin is java.awt.Color(0xFFFFFFFF.toInt(), true)
.
Any idea why is it like this in Kotlin?
Int
toLong
. --- See kotlinlang.org/docs/reference/basic-types.html --- and also see java2s.com/Tutorial/SCJP/0080__Type-Casting/… --- In Kotlin you have to do it explicitly, no language automatism to save you from having chosen a too narrow type.0xFFFFFFFF
is a valid two's complement representation of-1
as anint
. The difference between the two languages is that Kotlin literals don't seem to accept two's complement notation; the literal0x80000000
is an error (in Java, it'sInteger.MIN_VALUE
). In other words, you must use a unary-
operator to represent negative literals. Although I can't find any specific mention of this in the spec of either language.