Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

primitive question

 
Adam Confino
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

"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.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your getId() method ties to an instance variable of type int, then it has a default value of 0.
 
Zandis Murāns
Ranch Hand
Posts: 174
Java Java ME Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone.
 
Max Rahder
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic