Description
Bx: Primitive value is boxed then unboxed to perform primitive coercion.
簡單而言,就是將原始型態的值封裝再直接解封裝,這是沒有必要且使效能降低。
Solution
A primitive boxed value constructed and then immediately converted into a different primitive type (e.g., new Double(d).intValue()). Just perform direct primitive coercion (e.g., (int) d).
強制轉型即可,以Double而言,它的intValue也只是做強制轉型。
Example
Before:
private int convertMiliUnit(double aReading){ return new Double(aReading*1000).intValue(); }
After:
private int convertMiliUnit(double aReading){ return (int)aReading*1000; }
留言
張貼留言