In PHP, there is a isset function. What about Java? I think I can use == null with objects, but what about value types like int
In PHP, there is a isset function. What about Java? I think I can
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Java’s compiler won’t let you define variables and use them before they were assigned a value, so the problem doesn’t exist in the same form as it exists in php.
EDIT
If in your case the compiler didn’t stop you already (because this is eg an instance variable) the best solution is probably to initialize the variable to some "special" value as suggested by Guest11239193. Like this:
Of course, what a "safe, reasonable" initialization value is depends on the application.
Afterwards, you could
Or you could access x via a setter that inhibits changing more than once (probably overkill for most cases):
You could also use an
Integer, int’s object brother, which could be initialized to nullHowever, the fact that you think you need that knowledge may hide a deeper logic flaw in your program, so I’d suggest you explore the reason why you want to know if a primitive value (primitive as opposed to objects,
intvsInteger) wasn’t touched.