aspose file tools*
The moose likes Beginning Java and the fly likes primitive question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "primitive question" Watch "primitive question" New topic
Author

primitive question

Adam Confino
Ranch Hand

Joined: Sep 03, 2009
Posts: 48
Hey Java Gurus,

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?

I appreciate your time and help. Thanks.


Just Another Guy Hooked on Java
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


"if there is no INT variable contained in this object, insert a new object".

This is untestable. Java doesn't have the concept of undefined primitives. You would need change your type to its wrapper type and test that.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
If your getId() method ties to an instance variable of type int, then it has a default value of 0.


JDBCSupport - An easy to use, light-weight JDBC framework -
Zandis Murāns
Ranch Hand

Joined: Aug 18, 2009
Posts: 174

Well. Your class "Reservation" will contain that object (field) "id" all the time.

Just check the value of that ID object:

Adam Confino
Ranch Hand

Joined: Sep 03, 2009
Posts: 48
Every time this object is created, it will be assigned a int ID variable.

Are there any pitfalls to coding my logic this way?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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
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
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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
Ranch Hand

Joined: Sep 03, 2009
Posts: 48
Thanks everyone.
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
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.
 
 
subject: primitive question