That's not a cast. Java's cast syntax is, and always has been, preceding the value with a parenthesized type. findViewById
has signature
public <T> T findViewById (int id)
i.e. you can specify the return type T
as part of the generic type of findViewById
. Really, all that that does is force findViewbyId
to do the traditional (T)
cast for you (which is equally unsafe and still a downcast internally). In standard Java, there's really no reason to do that as opposed to an explicit cast. However,
Note: In most cases -- depending on compiler support -- the resulting view is automatically cast to the target class type. If the target class type is unconstrained, an explicit cast may be necessary.
So it sounds like some IDEs, probably Android Studio, have special inference rules that can infer (or validate) the type of T
in certain cases.