Check if int is intialized

Is there a way to check if a int value is intialized like strings
when i try this i get an error. Is there any other way to check if an int value is intialized?
int a;
if (a==null)
{
System.out.println("a not intialized");
}

Open in new window

angelblade27Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

contactkarthiCommented:
in your case the program will not compile saying a is not initialised if you try using it any where. if a==null also will throw error saying a is not initialised
0
angelblade27Author Commented:
yes i realise that is there any check that i can do to see if it is intialized?
0
contactkarthiCommented:
I dont think so...

but generally when the compiler throws an error it points out the exact line where you have the problem
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

angelblade27Author Commented:
is there a java practice for determining if a value is set or not?
The issue is that i need to do one statement if this variable has not been set but i don't want to have a default value to check for
0
contactkarthiCommented:
can you rephrase this
>> The issue is that i need to do one statement if this variable has not been set but i don't want to have a default value to check for

im not able to get your question
0
angelblade27Author Commented:
I have a class which has variables
public class first{
int a;
int b;
}

public class second{
public void trial (first cool){
if (cool.a==??){
System.out.println("This is not intialized");
}
}

}

the ?? is what should check for
0
contactkarthiCommented:
in this case a will be automatically initialized to zero
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
m00003643Commented:
In Java all primitives (int, bool, char, etc.) have an initial value.  Only Objects (Integer, Boolean, etc...) can be null.  As contactkarthi stated, the initial value for an int is 0.  If you really want to have uninitialized values, try this:

public class first{
Integer a;
Integer b;
}

Since Integer (as opposed to int) is an actual Object, it will initially be null.  You can then check if (null == a) from your Second class.

Bonus tip:  it is actually faster to compare (null == ??) than to compare (?? == null).  
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.