Here's the code and result:
python2.7 >>> 0xcafebabe
3405691582
scala2.10.4> 0xcafebabe
res0: Int = -889275714
Why is the hex number '0xcafebabe' a negative number in Scala?
P.S.: I find it related to the overflow of Int. But when I specify a variable as Long, why does it still not as expected?
scala> val aa = 0xcafebabeL
aa: Long = 3405691582
scala> val aa1:Long = 0xcafebabe
aa1: Long = -889275714
P.S. 2: When I try to evaluate a variable as Int with 3405691582, the Scala prompt complains error: integer number too large
, whereas if 0xcafebabe is applied, no error is thrown. Why is that?
scala> val aa:Int = 3405691582
<console>:1: error: integer number too large
val aa:Int = 3405691582
^
scala> val aa:Int = 0xcafebabe
aa: Int = -889275714