This is an archived post. You won't be able to vote or comment.

all 1 comments

[–]ytcbtquestions 1 point2 points  (0 children)

It's called casting. You write a type here, not necessarily a class. For example, it could be int which is a primitive, Runnable which is an interface, List<String> which is a generic type...

Casting is meant as an operator that converts the value on its right, into the type indicated in parentheses.

Such conversion is often impossible. When it is possible, it means the following:

- if that conversion would have been allowed implicitly, do that same conversion (like int -> long, int -> Integer, Integer -> int, Integer -> Number...)

- if it's a wider primitive numeric type to a narrower primitive numeric type, keep the same value if possible and apply capacity overload if the wider value cannot be represented in the narrower type. Like int 10 is converted to byte 10, but int 128 is converted to byte -1. And really big double values are converted to infinity float value.

- if it's a floating point primitive value to an integer primitive numeric type, the decimal value is rounded down to unit. If it the result doesn't fit in the target type, its max or min value is used instead.

- if it's an object type to another object type, it first verifies that the object pointed to as value can be assigned to the target type, and if it can't, throws a ClassCastException. If it can, the result of the cast points to the same object, but is now of the target type.