For example suppose I have
val myDouble = 25.7 val myInt = 5
How would I do something like
val divide = (Int) (myDouble / myInt)
asInstanceOf[Bar] is a type cast, which is primarily a runtime operation. It says that the compiler should be coerced into believing that foo is a Bar . This may result in an error (a ClassCastException ) if and when foo is evaluated to be something other than a Bar at runtime.
Scala some class returns some value if the object is not null, it is the child class of option. Basically, the option is a data structure which means it can return some value or None. The option has two cases with it, None and Some. We can use this with the collection.
AnyRef represents reference types. All non-value types are defined as reference types. Every user-defined type in Scala is a subtype of AnyRef . If Scala is used in the context of a Java runtime environment, AnyRef corresponds to java. lang.
Casting objects in OOP is usually the product of either class inheritance or interface inheritance. One has to always remember that class inheritance denotes an "is a" relationship, and interface inheritance denotes a "has a" relationship.
You can do (myDouble/myInt).toInt
. You can also do toDouble
, toLong
, and toFloat
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With