Issue about casting object brackets

前端 未结 6 591
無奈伤痛
無奈伤痛 2020-12-21 01:13

I have noticed that there are two ways to cast objects (the difference is the placement of the outer parenthesis):

 1. SimpleType simpleType = ((SimpleType)          


        
相关标签:
6条回答
  • 2020-12-21 01:32

    Are they doing the same thing ?

    No they are not.

    • The first one is casting your value returned from property.getType() to SimpleType. (Invocation is done before Casting)
    • The second one is first casting your property to SimpleType and then 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()));: -

    (property.getType())
    

    is evaluated first, then the casting is performed. In fact you don't really need a parenthesis around that. (property binds tighter to the dot (.) operator than the cast operator). 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(): -

    ((SimpleType) property)
    

    is evaluated first, then the invocation is done. As, now you have enclosed property inside the brackets, due to which it binds tighter to the cast operator, due to higher precedence enforced by parenthesis.

    0 讨论(0)
  • 2020-12-21 01:40

    No they are not doing the same thing.

    SimpleType simpleTypee = ((SimpleType) (property.getType()));
    

    First the property.getType() will be called and the object returned by property.getType() will be casted to SimpleType

    and in the second case

    SimpleType simpletype = ((SimpleType) property).getType();
    

    first the object property will be casted to SimpleType and then getType() will be invoked on that newly casted object

    0 讨论(0)
  • 2020-12-21 01:49

    They are entirely doing diffrent things. they are not same at all.

     1. SimpleType simpleTypee = ((SimpleType) (property.getType()));
    

    This first invokes getType of property and then casts the returned Object to SimpleType

     2. SimpleType simpletype = ((SimpleType) property).getType();
    

    This first casts the property to SimpleType and then invokes getType on the SimpleType

    0 讨论(0)
  • 2020-12-21 01:51

    They are doing two totally unrelated things: first is dowcasting the result of getType() and the second is downcasting the property variable. The first one looks like the one you need, given the type of the left-hand side. Note that in the first example you have extra parentheses, this would be enough, and is how this is idiomatically written:

    SimpleType simpleType = (SimpleType) property.getType();
    
    0 讨论(0)
  • 2020-12-21 01:58

    They are doing different things as detailed in the other answers. You can also cast by doing this

    SimpleType.class.cast(property.getType());
    SimpleType.class.cast(property).getType();
    

    Depending on what you actaully want to cast. Either property or the result of getType(). I prefer this syntax as it is more explicit and easier on the eye...

    0 讨论(0)
  • 2020-12-21 01:58

    They are not the same.

    • The first casts the result of getType to SimpleType.
    • The second casts property to SimpleType and calls getType on it, yet the result of getType is not casted to SimpleType.
    0 讨论(0)
提交回复
热议问题