I have noticed that there are two ways to cast objects (the difference is the placement of the outer parenthesis):
1. SimpleType simpleType = ((SimpleType) (property.getType()));
2. SimpleType simpleType = ((SimpleType) property).getType();
Are they doing the same thing ?
No they are not.
property.getType()toSimpleType. (Invocation is done before Casting)propertytoSimpleTypeandthen invoking the
getType()method on it. (Casting is done before Invocation).You can also understand it from the precedence of parenthesis. Since it has the highest precedence, it will be evaluated first.
First Case: –
So, in
((SimpleType) (property.getType()));: –is evaluated first, then the casting is performed. In fact you don’t really need a parenthesis around that. (
propertybinds tighter to thedot (.)operator than thecastoperator). So, invocation will always be done before casting. Unless you force it to reverse as in the below case: –Second Case : –
In
((SimpleType) property).getType(): –is evaluated first, then the invocation is done. As, now you have enclosed
propertyinside the brackets, due to which it binds tighter to thecastoperator, due to higher precedence enforced by parenthesis.