How can I check if an Integer has never been initialised?
for an object it works like
if (object==null) {}
how do I do this for an integer?
Printable View
How can I check if an Integer has never been initialised?
for an object it works like
if (object==null) {}
how do I do this for an integer?
I don't think you'll ever see a time where a primitive datatype hasn't been initialized, mainly because the compiler will ***** at you. Generally, I set all numeric primitives to 0 when I declare them.
:)
no that's wrong. You do not have to initialize one if you declare it out of a function!
I used this for my solution (which I don't like that's why I ask here) and declared a second empty one so I asked if they are equal and if so I know it hasn't been declared yet...
Failure to initialize a local variable will result in an error if the variable has not been initialized and you are in the process of querying it for a result.
If the member is declared static and is being accessed from a static context then the default value of the un-initialized member will be displayed. This is because static variables or "class members" are initialzed when the class is loaded by a hidden internal method. If one was to disassemble the byte codes in a java class file you would see the class initialization code in a method named <clinit>.Code:// will produce a warning "variable might not have been initialized"
class T{
public static void main(String[] args){
int i;
System.out.println("The value of i is" + i);
}
}
The following code would run fine even though our integer variable has not been initialized. This is because we haveCode:class T{
static int i;
public static void main(String[] args){
System.out.println("The value of i is" + i);
}
}
already created an instance of the class X.
Code:class T{
public static void main(String[] args){
new X().test();
}
}
class X{
int i;
public void test(){
System.out.println("The value of i is" + i);
}
}
/\/\isanThr0p why don't you just test for the variables default value and if it matches just assume that the variable has yet to have been initialized. :p
alright. what I am doing now is I have a second no initialized variable and than I check if they are the same. That works.
I was thinking about setting the unused var to 666666 or something so I know if its that value it's still to be used. But in my program userinput is required so in case the user would input 666666 it would not work correct anymore...
and if you wanna know, what I am doing is a little task for university. I have to implement a Searchtree and have to program some functions for it.
thanks guys...
nein schweiss ;)
deutscher?
ja aber unglucklicherweise mein deutscher bedurfnis arbeit. :( :D
well that sounds like an internet translation...
so what about this?
What about what? :confused: :p
so do you know some german or did you just use internet translation?
I know a little german. But unfortunately i don't get to use it that much since most of the people i interact with at my job are spanish. I hoping in the near future to get to go to Liechtenstein.
that's cool
well btw I just found out that a non initialized integer is 0...
:D