Description
Bx: Primitive value is boxed and then immediately unboxed
把原始型態如int,宣告為boxed的物件如Interger,並透過它提供的intValue去取得原始型態的值。
Solution
A primitive is boxed, and then immediately unboxed. This probably is due to a manual boxing in a place where an unboxed value is required, thus forcing the compiler to immediately undo the work of the boxing.
這是不必要的動作,所以根本不用做boxed的動作。
Example
Before:
public boolean checkResult(){ return getResult() == new Double(0); }
getResult取得為double型態,而比較的值為0,因此只需要將new Double(0)改為0.0即可。
After:
public boolean checkResult(){ return getResult() == 0.0; }
留言
張貼留言