What value, specifically? Is this a Processing built-in object like PVector, or your own custom class object, or a string...? It depends -- for example, data primitives like int have a default value when they are declared, even if they are not initialized, so the approach you are describing wouldn't work for an int.
Thanks, maybe "null" is what i needed. It is sad that not working with int and float. What is the difference between int and Integer? I don't even see Integer in processing reference category.
For many designs it not necessary to use Integer rather than int just to have something nullable. You also have other options:
Assign value(s) to an array or ArrayList or IntList, and check the length of the list to see whether a particular value has been assigned.
For a single value, use a separate boolean as a guard field (e.g. isDefined).
Depending on the use of the variable, use a natural value in the primitive itself to indicate non-assignment. For example, if you are only using the positive range of a signed int, -1 could mean "unassigned." Or use a non-operative value -- for example, if your int is a scaling factor, 1 does nothing; if your int is additive, 0 does nothing.
I suspect that this is only true for javascript, but Daniel Shiffman is often blown away by how good javascript is to just turn everything into true or false.
I bet that in javascript you can have an undefined or defined var "man" and ask if(man) {//do something}, and it will automatically conside whether or not "man" is undefined or defined even if it holds an integer or a float.
But don't quote me on that, you'll need to try it. It might also work with java.
Answers
What value, specifically? Is this a Processing built-in object like PVector, or your own custom class object, or a string...? It depends -- for example, data primitives like int have a default value when they are declared, even if they are not initialized, so the approach you are describing wouldn't work for an int.
Sorry, maybe I got to write some code example, and then ask this question again, because now even I don't understand what I really meant : )
using null for undefined objects is a common thing in java - you often see snippets of code like
mostly this is to protect calling methods on a undefined object, which will cause a null pointer exception.
(this doesn't work for primitive types like int or float, but does for Objects like String, or indeed, Integer)
Thanks, maybe "null" is what i needed. It is sad that not working with int and float. What is the difference between int and Integer? I don't even see Integer in processing reference category.
int
value.static
:http://docs.Oracle.com/javase/8/docs/api/java/lang/Integer.html
int
. 3:-Othanks!
For many designs it not necessary to use
Integer
rather thanint
just to have something nullable. You also have other options:ArrayList
orIntList
, and check the length of the list to see whether a particular value has been assigned.boolean
as a guard field (e.g. isDefined).I've done that already. My fav value for it is MIN_INT. $-)
many useful tips, thanks, people.
I suspect that this is only true for javascript, but Daniel Shiffman is often blown away by how good javascript is to just turn everything into true or false.
I bet that in javascript you can have an undefined or defined var "man" and ask if(man) {//do something}, and it will automatically conside whether or not "man" is undefined or defined even if it holds an integer or a float.
But don't quote me on that, you'll need to try it. It might also work with java.
https://developer.Mozilla.org/en-US/docs/Glossary/Truthy