Pages

Friday, December 15, 2006

lesson no 1

I've got a lesson today.

If you compared two variable with different type, example BigDecimal with int. Then use the larger type comparator operand.

here's the code snippet

BigDecimal zeroInBigDecimal = new BigDecimal(0);

if (txtCloseLevel.getValue().intValue() < 0) {
JOptionPane.showMessageDialog(this, "Close Level Must Be Greater Or Equal Zero",
"Error", JOptionPane.ERROR_MESSAGE);
return false;
}
the getValue method returned a BigDecimal instant, so the comparation should be
if (txtCloseLevel.getValue().compareTo(zeroInBigDecimal) < 0) {

why?
Because i expect the intValue() method to return maximum integer value when the txtCloseLevel.getValue() return a value bigger than the maximum integer value. But instead it return a negative integer number. A lesson to be learned :P

No comments: