I have a very basic, or shall I say primitive question.
I want to use an if statement to say "if there is no INT variable contained in this object, insert a new object". I think my code does not work because primitives cannot by null.
My question is, how would you implement this kind of logic when using a primitive?
If your business logic can accept 0 == unset its OK. Are there any occasions where you can set the value to zero after the reservation has been inserted?
Adam Confino
Ranch Hand
Joined: Sep 03, 2009
Posts: 48
posted
0
Paul,
I'm not aware of any situations where the the value would be set to zero. Is there a safeguard you could create?
i.e. you cannot setID() to zero after the object is created
You could add logic to your set method to throw an exception if someone tried. Kind of overkill. It does sound like an object rather than a primitive makes more sense if the value can be unset.
Adam Confino wrote:Paul,I'm not aware of any situations where the the value would be set to zero. Is there a safeguard you could create?
If you really want to know if it's been initialized, use Integer and not a primitive. Then, by definition, if the reference is null you'll know it hasn't been initialized. Using an int is problematic since your requirements may change and 0 may become a valid initial value. If for some reason you must use an int, I'd choose a better "uninitialized" value, like -1 (if the value must be a positive value) or Integer.MIN_VALUE.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.