Casting variables in Java

前端 未结 5 1745
野性不改
野性不改 2020-11-21 23:50

I wonder if anyone could tell me how casting works? I understand when I should do it, but not really how it works. On primitive data types I understand partially bu

5条回答
  •  滥情空心
    2020-11-22 00:40

    The right way is this:

    Integer i = Integer.class.cast(obj);
    

    The method cast() is a much safer alternative to compile-time casting.

提交回复
热议问题