• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

primitive question

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 174
Java ME Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone.
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I have a knack for fixing things like this ... um ... sorry ... here is a consilitory tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic