evaluates to false, then your variable gives is never assigned a value before it is referenced on the last line. When you declare the variable, you should initialize it to some value.
I.e.
Code:
int gives = 0;
10-24-2004, 04:30 PM
snowstorm
Ok i didn't think id make a new thread but is there a way to see if every value in an array is set? Like if i initialize an array like
int[] blah = new int[5];
and then like
if(othervar == othervar)
{
set some values of blah
}
and then can i see if all the values of blah are set?
thanks
10-24-2004, 04:57 PM
Dash Rendar
When you create the array using the new keyword, each array element is automatically initialised to the default value for the primitive. (Primitive types include bytes, chars, ints, longs, floats, etc, etc.) The defaut value for the integer type is 0, so all elements will automatically be initialised to 0. Thus, all the elements are already 'set'.
To summarise and avoid any further confusion...
If you declare a primitive variable (e.g. byte, char, int, float, etc.) as a class/instance variable (i.e. at the class level, not within a method), then the variable is automatically assigned its default value, assuming you don't assign a value yourself. For numeric primitives, this is always 0. For booleans, the default is false.
Array elements behave the same way, wherever/whenever (sorry for the Shakira link there) they are created using the new keyword.
Objects declared as class/instance members are automatically initialized to null.
If you declare a primitive variable within a method, the variable is local to whatever block the variable is declared in, and the variable is NOT initialised. This is why you have to initialize local variables before you use them. Similarly, objects declared with local scope are not initialized. To avoid compiler errors, it's often a good idea to initialize them to null at declaration time.