简体   繁体   English

java.lang.Integer不能使用Scala中的任何类型转换为java.lang.Byte错误

[英]java.lang.Integer cannot be cast to java.lang.Byte error with Any type in Scala

I can cast Int data to Byte. 我可以将Int数据转换为Byte。

scala> 10.asInstanceOf[Byte]
res8: Byte = 10

However with the same value in Any type, the cast raises an error. 但是,如果使用任何类型的相同值,则转换会引发错误。

scala> val x : Any = 10
x: Any = 10

scala> x.asInstanceOf[Byte]
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Byte
    at scala.runtime.BoxesRunTime.unboxToByte(BoxesRunTime.java:98)
    at .<init>(<console>:10)

I can cast twice. 我可以投两次。

scala> val y = x.asInstanceOf[Int]
y: Int = 10

scala> y.asInstanceOf[Byte]
res11: Byte = 10

Are there better ways than this? 还有比这更好的方法吗?

In Scala, compiler tries to hide the distinction between primitive types and reference ones (boxed), defaulting to primitives. 在Scala中,编译器试图隐藏基本类型和引用类型(盒装)之间的区别,默认为基元。 Sometimes, abstractions leak and you see that kind of problems. 有时,抽象泄漏,你会看到那种问题。

Here, you're pretending that value is Any, which require compiler to fallback to boxed values: 在这里,您假装该值为Any,这需要编译器回退到盒装值:

override def set(value:Any) = {
    if (check(value.asInstanceOf[Byte])) {

And here, you're not limiting value to be referential, so such casting will be done on primitive types: 在这里,你并没有将值限制为引用,所以这样的转换将在原始类型上完成:

10.asInstanceOf[Byte]

In other words: 换一种说法:

scala> val x: Any = 10
x: Any = 10

scala> x.asInstanceOf[Byte]
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Byte
  at scala.runtime.BoxesRunTime.unboxToByte(BoxesRunTime.java:97)
  ... 32 elided

scala> val y: Int = 10
y: Int = 10

scala> y.asInstanceOf[Byte]
res4: Byte = 10

To overcome this problem, you probably have to go through an extra conversion to, say, String: 要解决这个问题,您可能需要经过额外的转换,比如String:

scala> x.toString.toInt
res6: Int = 10

scala> x.toString.toByte
res7: Byte = 10

Try converting to int and then to Byte: 尝试转换为int然后转换为Byte:

scala> val x : Any = 10
x: Any = 10

scala> x.asInstanceOf[Int].asInstanceOf[Byte]
res1: Byte = 10

Or as Ionuț G. Stan suggested: 或者如IonuţG。Stan所说:

scala> x.asInstanceOf[Int].toByte
res4: Byte = 10

Although I cannot explain why this work. 虽然我无法解释为什么这项工作。

An integer is 32 bits in Java, while a byte is obviously 8 bits. Java中的整数是32位,而字节显然是8位。 The problem is what bits do you truncate to make an integer a byte? 问题是你截断什么位以使整数成为一个字节? The least significant 24 bits or the most significant 24 bits? 最低有效24位还是最高有效24位? The correct answer is in the context of your problem. 正确的答案是在您的问题的背景下。

暂无
暂无

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

相关问题 如何修复 Spark (Scala) 中的“java.lang.Integer cannot be cast to java.lang.Double”错误? - How to fix "java.lang.Integer cannot be cast to java.lang.Double" Error in Spark (Scala)? Spark scala: java.lang.ClassCastException: java.lang.Integer cannot be cast to scala.collection.Seq - Spark scala: java.lang.ClassCastException: java.lang.Integer cannot be cast to scala.collection.Seq Spark:GenericMutableRow无法转换为java.lang.Byte和 <none> 不是一个名词 - Spark: GenericMutableRow cannot be cast to java.lang.Byte and <none> is not a term Scala / Play ClassCastException:无法将java.lang.Integer转换为java.lang.Long] - Scala/Play ClassCastException: Cannot cast java.lang.Integer to java.lang.Long] Apache spark Row getAs[String]: java.lang.Byte 不能转换为 java.lang.String - Apache spark Row getAs[String] : java.lang.Byte cannot be cast to java.lang.String 编译错误? java.lang.ClassCastException:scala.collection.mutable.WrappedArray $ ofRef无法强制转换为java.lang.Integer - Compiler bug? java.lang.ClassCastException: scala.collection.mutable.WrappedArray$ofRef cannot be cast to java.lang.Integer Scala.Byte与java.lang.Byte-建议使用哪个? - Scala.Byte vs java.lang.Byte -Which one is recomended to use? 我如何在scala中使用java.lang.Integer - How do I use java.lang.Integer inside scala MongoDB的。 从数据库中提取整数。 java.lang.Integer无法强制转换为java.lang.String - MongoDb. Extracting integer from database. java.lang.Integer cannot be cast to java.lang.String 播放2.2 for Scala:Anorm和java.lang.Integer大小限制 - Play 2.2 for Scala: Anorm and java.lang.Integer size limits
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM