In Java,
someMethod(arg)
is a call of a method named someMethod
with a single parameter arg
.
All methods (except void
ones) return a value. The type of the return value is part of method definition. In your case, we don't know what the someMethod
returns (because you didn't provide its definition), but we can assume it is not void
.
The expression
(MyClass) obj
is a type cast, which tells the compiler to treat obj
as if it was of type MyClass
. During compilation, compiler checks that the definition of obj
is compatible with MyClass
, and if it is, it allows you to treat it like one. During run time, Java virtual machine checks that the actual object obj
is actually instance of class MyClass
. If it is not, it will throw a ClassCastException
.
Typically, type casts are used to "upgrade" how the compiler sees an object into something more specific. For example, if a pet shop by definition sells Animal
s, and you ordered a Hamster
, then you can reasonably assume thet what you get is not just an Animal
, but a Hamster
(assuming Hamster extends Animal
).
Now, put this together and you get a type cast applied to a method return value.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…