Java – what is the difference between asinstanceof [x] and tox for value types?

My ability to use IntelliJ to convert java code into Scala code, which usually works well

It seems that IntelliJ replaces all transformations with asinstanceof

What are useful asinstanceof [int], asinstanceof [long] and so on for value types that cannot be replaced by toint, tolong?

resolvent

Solution

I didn't know there was such a situation You can compile the class to check whether the bytecode issued by yourself is the same

class Conv {
  def b(i: Int) = i.toByte
  def B(i: Int) = i.asInstanceOf[Byte]
  def s(i: Int) = i.toShort
  def S(i: Int) = i.asInstanceOf[Short]
  def f(i: Int) = i.toFloat 
  def F(i: Int) = i.asInstanceOf[Float]  
  def d(i: Int) = i.toDouble
  def D(i: Int) = i.asInstanceOf[Double]
}

And use javap - C conv to get

public byte b(int);
  Code:
   0:   iload_1
   1:   i2b
   2:   ireturn

public byte B(int);
  Code:
   0:   iload_1
   1:   i2b
   2:   ireturn

...

Here you can see that the exact bytecode is issued in each case

The above is the Java collected by programming house for you - what is the difference between asinstanceof [x] and tox for value types? I hope this article can help you solve the problem of Java - what is the difference between asinstanceof [x] and tox for value types? Program development problems encountered.

If you think the content of the programming home website is good, you are welcome to recommend the programming home website to programmers and friends.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>