1

I was just working on a quick class for an item to be sold. Here is the code for said class:

public class Item{
private int itemNumber;
private double itemCost;
private String manName;

public Item(int inum, double icost, String mname){
itemNumber = inum;
itemCost = icost;
manName = mname;
}

public int getNum(){
    return itemNumber;
    }
public double getCost(){
    return itemCost;
    }
public String getName(){
    return manName;
    }

public void setNum(int inum){
    itemNumber = inum;
    }
public void setCost(double icost){
    itemCost = icost;
    }
public void setName(String mname){
    manName = mname;
    }
}

And here is the calling method in the simple driver app I was using to test:

public static void testItem(){
    Item item = new Item(4,44.50,"Example1");
    item.setCost(50.00);
    item.setNum(3);
    item.setName("Example2");
    System.out.println(item.getNum() + " " + item.getCost() + " " + item.getName());
    }
}

My curiosity is that, originally, I had used floats for the cost variables. However, when compiling, I got an error for possible data loss as it was casting from double TO float.

I had declared the variables as floats, but they were considered doubles, it would seem.

I'm only a very junior Java man, and am currently using TextPad as my editor. I'm just curious about this fact so that I can keep the answer in mind. Thanks, guys!

user3527961
  • 35
  • 2
  • 5
  • 2
    This will help: http://www.javapractices.com/topic/TopicAction.do?Id=13 basically use [BigDecimal](http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html) – Jason Sperske Sep 29 '14 at 21:22
  • You're always safest using `BigDecimal` when dealing with money in java code. Otherwise, `double`. – kolossus Sep 29 '14 at 21:23
  • it is not a good idea to use double or float to represent money – evanwong Sep 29 '14 at 21:23
  • Thanks for pointing me in this direction! I will try it out. – user3527961 Sep 29 '14 at 21:27
  • In my opinion there is almost no reason ever to use `float` for anything unless some API demands it. `BigDecimal` or `double`, depending on the situation, is better. – ajb Sep 29 '14 at 21:30

1 Answers1

4

The compiler type errors are caused because 1.2 (eg.) is a double literal, while 1.2f is a float literal.

Changing all the types to double "fixed" this because there was no need to go convert from a double to a float. Other corrections would have been to use floating-literals (e.g. 50.0f) or to explicitly cast.

double d = 1.2;        // GOOD, double
float f = 1.2;         // BAD, double -> float ("error: possible loss of precision")
float f = 1.2f;        // GOOD, float
float f = (float)1.2;  // OK, double -> float by explicit cast
float f = 1;           // OK, int -> float

Of course, neither double or float are "correct" for money, which should be dealt with using a fixed precision type, such as that provided by BigDecimal.

See also:

Community
  • 1
  • 1
user2864740
  • 60,010
  • 15
  • 145
  • 220